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}
);
}
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.
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:
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:
const predicted = Array.from(unX).map((val, i) => {
return {x: val, y: unY[i]}
});
// Plot the Result
tfPlot([values, predicted], surface1)
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!