Machine Learning is for Humans — how to do it, and teach it too

<script src=”https://unpkg.com/ml5@latest/dist/ml5.min.js"></script>
let brain;
isTrained = false;
let options = {
inputs: [“mouseX”, “mouseY”],
outputs: [‘red’,’blue’],
task: ‘classification’,
debug: true
}
brain = ml5.neuralNetwork(options);
if (mouseX > 50 && mouseX < 100 && mouseY > 50 && mouseY<100) {
// code to change formatting of button
}
20, 60  no
60, 60 yes
70, 110 no
140,110 no
80, 55 yes
20, 155 no
dropdown = createSelect();
dropdown.option(‘red’);
dropdown.option(‘blue’);
if (!isTrained) {
let inputs = {
mouseX, mouseY
}
let label = dropdown.value();
brain.addData(inputs,{label});
}
let data = brain.neuralNetworkData.data.raw;
for (let row of data) {
fill(row.ys.label);
circle(row.xs.mouseX,row.xs.mouseY,20);
}
let trainbutton = createButton(‘train’);
trainbutton.mousePressed(function() {
brain.normalizeData();
brain.train({epochs:epochs},()=>{isTrained=true;});
});
if (isTrained) {
let inputs = {mouseX,mouseY};
let outputs = brain.classifySync(inputs);
background(outputs[0].label);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store