简体   繁体   中英

How to save a Tensorflow.js model?

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!

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM