How to update TensorFlow.js saved / trained model

Viewed 1092

Hi I would like to know how to update the TensorFlow js saved model in node js,

I created a basic neural network in TensorFlow js using NodeJS and saved the trained model and it produced model.json and weights.bin file.

I've seen that we can update the trained model in keras in Python.

But there is no example for updating the model in TensorFlow js, NodeJS and JS.

Please someone help to update the trained model.

model load and update code

var tf = require('@tensorflow/tfjs-node')
async funtion load(){
    const model = await tf.loadLayersModel(url);
}
load().then(() => {
    model.predict(tf.tensor1d([5]))
    model.predict(tf.tensor1d([5]), tf.tensor1d([15]));
    model.predict(tf.tensor1d([5]))
})
1 Answers

A saved model can be loaded back and used for further training.

const model = await tf.loadLayersModel(url);
model.predict(feature) // predict with old model
await model.fit(features, labels); // this will update the weights of the model
model.predict(feature) // predict with new model

The retrained model can be saved back and this will update its weights. It is as if the model was trained on both the initial data and the current data which means that it can predict data coming from both sources.

However, if the new data differ significantly from the initial data, the model will be unable to predict well from the initial data again. This issue is discussed well here

Related