JSFiddle - React, Tailwind, and code Playground

by Laxmikant Dange

HTML

<script src="https://unpkg.com/[email protected]/dist/ml5.min.js"></script>

JavaScript

// Initialize the the neural network
const neuralNetwork = ml5.neuralNetwork({
  inputs: 1,
  outputs: 1,
  task: 'regression',
  batchSize: 5,
	epochs: 5,
});

// add in some data
for (let i = 0; i < 100; i += 1) {
  const x = i;
  const y = i * 2;
  neuralNetwork.data.addData([x], [y]);
}

// normalize your data
neuralNetwork.data.normalize();
// train your model
neuralNetwork.train(finishedTraining);

// when it is done training, run .predict()
function finishedTraining() {
  neuralNetwork.predict([25], (err, results) => {
    console.log(results);
  });
}