Published
Edited
May 8, 2020
1 fork
Importers
1 star
Insert cell
Insert cell
Insert cell
{
const data = [
{ index: 0, value: 50 },
{ index: 1, value: 100 },
{ index: 2, value: 150 }
];

return render('barchart', data);
}
Insert cell
Insert cell
{
const rows = 5;
const cols = 5;
const values = [];
for (let i = 0; i < rows; i++) {
const row = [];
for (let j = 0; j < cols; j++) {
row.push(Math.round(Math.random() * 50));
}
values.push(row);
}
const data = { values };

return render('confusionMatrix', data);
}
Insert cell
Insert cell
{
const cols = 50;
const rows = 20;
const values = [];
for (let i = 0; i < cols; i++) {
const col = [];
for (let j = 0; j < rows; j++) {
col.push(i * j);
}
values.push(col);
}
const data = { values };

return render('heatmap', data);
}
Insert cell
Insert cell
{
const data = Array(100)
.fill(0)
.map(x => Math.random() * 100 - Math.random() * 50);

// Push some special values for the stats table.
data.push(Infinity);
data.push(NaN);
data.push(0);

return render('histogram', data);
}
Insert cell
Insert cell
{
const series1 = Array(100)
.fill(0)
.map(y => Math.random() * 100 - Math.random() * 50)
.map((y, x) => ({ x, y }));

const series2 = Array(100)
.fill(0)
.map(y => Math.random() * 100 - Math.random() * 150)
.map((y, x) => ({ x, y }));

const series = ['First', 'Second'];
const data = { values: [series1, series2], series };

return render('linechart', data);
}
Insert cell
Insert cell
{
const series1 = Array(100)
.fill(0)
.map(y => Math.random() * 100 - Math.random() * 50)
.map((y, x) => ({ x, y }));

const series2 = Array(100)
.fill(0)
.map(y => Math.random() * 100 - Math.random() * 150)
.map((y, x) => ({ x, y }));

const series = ['First', 'Second'];
const data = { values: [series1, series2], series };

return render('scatterplot', data);
}
Insert cell
Insert cell
{
const headers = ['Col 1', 'Col 2', 'Col 3'];

const values = [[1, 2, 3], ['4', '5', '6'], ['7', true, false]];

const surface = { name: 'Table', tab: 'Charts' };
return render('table', { headers, values }, { height: 150 });
}
Insert cell
Insert cell
{
const model = tf.sequential({
layers: [
tf.layers.dense({ inputShape: [784], units: 32, activation: 'relu' }),
tf.layers.dense({ units: 10, activation: 'softmax' })
]
});

return showLayer(model.getLayer(undefined, 1));
}
Insert cell
Insert cell
{
const model = tf.sequential({
layers: [
tf.layers.dense({ inputShape: [784], units: 32, activation: 'relu' }),
tf.layers.dense({ units: 10, activation: 'softmax' })
]
});

return showModelSummary(model);
}
Insert cell
{
const tensor = tf.tensor1d([0, 0, 0, 0, 2, 3, 4]);

return showValuesDistribution(tensor);
}
Insert cell
Insert cell
history = {
const model = tf.sequential({
layers: [
tf.layers.dense({ inputShape: [784], units: 32, activation: 'relu' }),
tf.layers.dense({ units: 10, activation: 'softmax' })
]
});

model.compile({
optimizer: 'sgd',
loss: 'categoricalCrossentropy',
metrics: ['accuracy']
});

const data = tf.randomNormal([100, 784]);
const labels = tf.randomUniform([100, 10]);

// Train for 5 epochs with batch size of 32.
const history = await model.fit(data, labels, {
epochs: 5,
batchSize: 32
});

return history;
}
Insert cell
showHistory(history, ['loss', 'acc'], { height: 200 })
Insert cell
Insert cell
Insert cell
render = async (chartType, data, options) => {
const defaultOptions = {
width: 880,
height: 400
};

if (!tfvis.render.hasOwnProperty(chartType)) {
throw new Error(`Invalid chart type: ${chartType}`);
}

options = { ...defaultOptions, ...options };

const elem = html`<div></div>`;
elem.style.height = `${options.height}px`;

await tfvis.render[chartType](elem, data, options);

return elem;
}
Insert cell
Insert cell
showLayer = async layer => {
const elem = html`<div></div>`;
tfvis.show.layer(elem, layer);

return elem;
}
Insert cell
Insert cell
showModelSummary = async model => {
const elem = html`<div></div>`;
tfvis.show.modelSummary(elem, model);

return elem;
}
Insert cell
Insert cell
showValuesDistribution = async tensor => {
const elem = html`<div></div>`;
tfvis.show.valuesDistribution(elem, tensor);

return elem;
}
Insert cell
Insert cell
showHistory = async (history, metrics, options) => {
const defaultOptions = {
width: 880,
height: 400
};

options = { ...defaultOptions, ...options };

const elem = html`<div></div>`;

await tfvis.show.history(elem, history, metrics, options);

return elem;
}
Insert cell
tf = require('@tensorflow/tfjs@1.7')
Insert cell
tfvis = require('https://bundle.run/@tensorflow/tfjs-vis@latest')
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