I would like to make a user interface that creates,saves and trains tensorflow.js models. But i can't save a model after creating it. I even copied this code from the tensorflow.js documenation but it does't work:
const model = tf.sequential(
{layers: [tf.layers.dense({units: 1, inputShape: [3]})]});
console.log('Prediction from original model:');
model.predict(tf.ones([1, 3])).print();
const saveResults = await model.save('localstorage://my-model-1');
const loadedModel = await tf.loadModel('localstorage://my-model-1');
console.log('Prediction from loaded model:');
loadedModel.predict(tf.ones([1, 3])).print();
I always get the error message "Uncaught SyntaxError: await is only valid in async function" .How can I fix this? thanks!
Save Your Neural Network Model to JSON This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification. The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.
Call save_model_tf() to save a model's architecture, weights, and training configuration in a single file/folder. This allows you to export a model so it can be used without access to the original Python code*. Since the optimizer-state is recovered, you can resume training from exactly where you left off.
You need to be in an async environment. Either create an async function (async function name(){...}
) and call it when you need to or the shortest way would be a self invoking async arrow function:
(async ()=>{
//you can use await in here
})()
Create an async function and invoke it:
async function main() {
const model = tf.sequential({
layers: [tf.layers.dense({ units: 1, inputShape: [3] })]
});
console.log("Prediction from original model:");
model.predict(tf.ones([1, 3])).print();
const saveResults = await model.save("localstorage://my-model-1");
const loadedModel = await tf.loadModel("localstorage://my-model-1");
console.log("Prediction from loaded model:");
loadedModel.predict(tf.ones([1, 3])).print();
}
main();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With