【问题标题】:How to save a Tensorflow.js model?如何保存 Tensorflow.js 模型?
【发布时间】:2018-10-29 22:43:43
【问题描述】:

我想制作一个用户界面来创建、保存和训练 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 is only valid in async function”。我该如何解决这个问题?谢谢!

【问题讨论】:

  • 如果你在这里运行自己的代码 sn-p 你可以看到@Sebastian Speitel 的回答

标签: javascript tensorflow tensorflow.js


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 2021-11-28
      相关资源
      最近更新 更多