Tutorials References Exercises Bootcamp Menu
Sign Up Create Website Get Certified Pro

Example 1 Training

Training Function

async function trainModel(model, inputs, labels, surface) {
  const batchSize = 25;
  const epochs = 100;
  const callbacks = tfvis.show.fitCallbacks(surface, ['loss'], {callbacks:['onEpochEnd']})
  return await model.fit(inputs, labels,
    {batchSize, epochs, shuffle:true, callbacks:callbacks}

Try it Yourself »

epochs defines how many iterations (loops) the model will do.

model.fit is the function that runs the loops.

callbacks defines the callback function to call when the model wants to redraw the graphics.

Test the Model

When a model is trained, it is important to test and evaluate it.

We do this by inspecting what the model predicts for a range of different inputs.

But, before we can do that, we have to un-normalize the data:

Un Normalize

let unX = tf.linspace(0, 1, 100);
let unY = model.predict(unX.reshape([100, 1]));

const unNormunX = unX.mul(inputMax.sub(inputMin)).add(inputMin);
const unNormunY = unY.mul(labelMax.sub(labelMin)).add(labelMin);

unX = unNormunX.dataSync();
unY = unNormunY.dataSync();

Then we can look at the result:

Plot the Result

const predicted = Array.from(unX).map((val, i) => {
return {x: val, y: unY[i]}

// Plot the Result
tfPlot([values, predicted], surface1)

Try it Yourself »