我想制作一个创建、保存和训练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 :虽然等待只在异步函数中有效“.How,我能修复这个问题吗?谢谢!
发布于 2018-10-29 22:50:50
您需要处于异步环境中。或者创建一个异步函数(async function name(){...})并在需要时调用它,或者最短的方法是自调用异步箭头函数:
(async ()=>{
//you can use await in here
})()发布于 2018-10-29 23:04:02
创建一个异步函数并调用它:
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();https://stackoverflow.com/questions/53054968
复制相似问题