Public
Edited
Mar 5, 2023
Insert cell
Insert cell
Insert cell
model = ml5.neuralNetwork({
inputs: ['x', 'y'],
outputs: ['label'],
task: 'classification',
debug: false,
learningRate: undefined
})
Insert cell
function onCanvasPressed(e) {
const input = { x: state.mouse.x, y: state.mouse.y }

if (state.modelTrained) {
// Model has been trained - classify the input
model.classify(input, (error, results = []) => {
if (error) console.error(error)
else mergeState({ circles: [...state.circles, { ...input, ...results[0] }] })
})
} else {
// Model has not been trained - add data
const target = { label: state.label }
model.addData(input, target)
mergeState({ circles: [...state.circles, { ...input, ...target }] })
}
}
Insert cell
function onTrainModelButtonPressed() {
const onTraining = (e, l) => mergeState({ loss: l.loss });
const onComplete = () => mergeState({ modelTrained: true });
model.normalizeData();
model.train({ epochs: 200 }, onTraining, onComplete);
}
Insert cell
function onSaveDataButtonPressed() {
model.saveData('model_data')
}
Insert cell
function onSaveModelButtonPressed() {
model.save()
}
Insert cell
async function onLoadDataButtonPressed() {
const { data } = await dataFile.json()
data.forEach(({ xs: input, ys: target }) => {
model.addData(input, target)
mergeState({ circles: [...state.circles, { ...input, ...target }] })
})
}
Insert cell
async function onLoadModelButtonPressed() {
const onLoaded = () => mergeState({ modelTrained: true });
model.load({
model: await modelFile.url(),
metadata: await modelMetaFile.url(),
weights: await weightsFile.url()
}, onLoaded)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof modelFile = Inputs.file({ label: "Model File", accept: ".json", required: false })
Insert cell
viewof modelMetaFile = Inputs.file({ label: "Model Meta File", accept: ".json", required: false })
Insert cell
viewof weightsFile = Inputs.file({ label: "Weights File", accept: ".bin", required: false })
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { createML5, listenMousedown, listenMouseover, listenKeyup, createButton } from "@bberak/utils"
Insert cell
ml5 = createML5('0.4.2')
Insert cell
mutable state = ({ label: 'blue', mouse: { x: 0, y: 0 }, circles: [], modelTrained: false, loss: undefined })
Insert cell
function mergeState(other) {
mutable state = { ...state, ...other }
}
Insert cell
listenMousedown(canvas, onCanvasPressed)
Insert cell
listenMouseover(canvas, e => {
mergeState({ mouse: { x: e.offsetX, y: e.offsetY } });
})
Insert cell
listenKeyup(document, e => {
console.log(e)
switch (e.key) {
case 'r':
mergeState({ label: 'red' });
return;
case 'g':
mergeState({ label: 'green' });
return;
case 'b':
mergeState({ label: 'blue' });
return;
}
})
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