微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何保存Tensorflow.js模型?

我想创建一个用户界面来创建,保存和训练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 ()=>{
   //you can use await in here
})()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐