Reputation: 131
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!
Upvotes: 6
Views: 1032
Reputation: 3115
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();
Upvotes: 2
Reputation: 7346
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
})()
Upvotes: 5