Public
Edited
Jul 27, 2023
Insert cell
Insert cell
{
const shape = [2, 3]; // 2 rows, 3 columns
const a = tf.tensor([1.0, 2.0, 3.0, 10.0, 20.0, 30.0], shape);
return a; // print Tensor values
}
Insert cell
Insert cell
{
const shape = [2, 3]; // 2 rows, 3 columns
const a = tf.tensor([1.0, 2.0, 3.0, 10.0, 20.0, 30.0], shape);
return a.toString(); // print Tensor values
}
Insert cell
Insert cell
viewof t = pt(tf.tensor([1.0, 2.0, 3.0, 10.0, 20.0, 30.0], [2, 3]))
Insert cell
Insert cell
t
Insert cell
Insert cell
// A single dimensional tensor
viewof i = pt(tf.scalar(3.14))
Insert cell
// .toString() automatically truncates long tensors
viewof j = pt(tf.tensor1d(Array.from({ length: 100 }).map((_, i) => i)))
Insert cell
Insert cell
pt = tensor => {
let verbose = false; // toggles on click
// this is the part that varies depending on the verbose state
let render = () => html`<div style='cursor:zoom-${verbose ? 'out' : 'in'}'>
<pre class='hljs-tfjs'>${hljs.highlight('tfjs', tensor.toString(verbose)).value}</pre>
</div>`;
const elem = html`<div>${render()}</div>`; // initial render
// click interaction for toggling verbose
elem.addEventListener('click', () => {
verbose = !verbose;
elem.innerHTML = '';
elem.appendChild(render());
// for browsers that don’t support ResizeObserver, let them
// know that the cell height has changed.
elem.dispatchEvent(new CustomEvent('load'));
});
// expose the raw value to other cells using viewof
elem.value = tensor;
return elem;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more