簡體   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