如何保存Tensorflow.js模型?

Kar*_*dan 6 javascript tensorflow 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();
Run Code Online (Sandbox Code Playgroud)

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

Seb*_*tel 5

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

(async ()=>{
   //you can use await in here
})()
Run Code Online (Sandbox Code Playgroud)