繁体   English   中英

如何保存Tensorflow.js模型?

[英]How to save a Tensorflow.js model?

我想创建一个用户界面来创建,保存和训练tensorflow.js模型。 但是在创建模型后我无法保存模型。 我甚至从tensorflow.js文档复制了这段代码,但它不起作用:

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

我总是收到错误消息“ Uncaught SyntaxError:await仅在异步函数中有效”。如何修复此问题? 谢谢!

您需要处于异步环境中。 创建一个异步函数( async function name(){...} )并在需要时调用它,或者最短的方法是自调用异步箭头函数:

(async ()=>{
   //you can use await in here
})()

创建一个异步函数并调用它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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