【问题标题】:tensorflow.js if exist load saved model or if not exists create and save checkpoints of model every epochEnd callbacktensorflow.js 如果存在加载保存的模型,或者如果不存在,则在每个 epochEnd 回调中创建并保存模型的检查点
【发布时间】:2020-10-31 22:01:24
【问题描述】:

我想从 localstorage 加载我的预定义模型,如果那里没有任何模型,则可能会创建它。在每个纪元之后,我想保存模型以供以后加载。

我搜索了很多示例来保存和加载模型。有一些示例(如 API 文档),但我找不到如何检查本地存储中是否有任何适用的模型,如果没有,请创建它。

注意:Tensorflow 解决方案不适用于 tensorflow.js(或者我找不到方法)

//I need to load model from localstorage. It's not needed to use a try catch block, if there is a solution to check are there any model that can be loaded as a correct model, it can be applicable.

try{

/*
try-catch block or any function to check
*/

const model = await tf.loadLayersModel('localstorage://my-model-1');

}catch(err) {

//Create new model if not exists (i don't know it is ok or not)
  const model = tf.sequential();
  model.add(tf.layers.dense({units: 1, inputShape: [1]}));
  model.add(tf.layers.dense({units: 1}));

}

const xs = tf.tensor([1,2,3,4,5]);
const ys = tf.tensor([3,5,7,9,11]);

async function trainModel(model, inputs, labels) {
    const learningRate = 0.01;
    const opt=tf.train.sgd(learningRate);
    model.compile({ loss: 'meanSquaredError', optimizer: opt});
    
    return await model.fit(xs, ys, {
        epochs: 500,
        callbacks:{
            onEpochEnd: async(epoch, logs) =>{
                document.getElementById("output").innerText="Epoch:" 
                            + epoch 
                            + " Loss:" 
                            + logs.loss;
                
        /*
        and then save model to the localstorage for it can load at top of this script for use later load
        */
        
            }
        }
    })
}

var training = trainModel(model, xs, ys)


training.then(function(args){
    var prediction = model.predict(tf.tensor([6]));
    document.getElementById("output").innerText=prediction;
    prediction.print();
})
<!DOCTYPE html>
<html lang="tr">
  <head>
    <title>tensorflow.js sofrası</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.7.0/dist/tf.min.js"></script>
  </head>

  <body>
    <div id="output"></div>
    <script src="script2.js"></script>
  </body>
</html>

【问题讨论】:

    标签: save load tensorflow.js pre-trained-model


    【解决方案1】:

    模型可以在每个 epoch 后保存,如下所示:

    onEpochEnd: async(epoch, logs) =>{
      document.getElementById("output").innerText="Epoch:" 
                                + epoch 
                                + " Loss:" 
                                + logs.loss;
                    
      await model.save('localstorage://model-name');
            
    }
    

    如果模型已经保存,我们可以检查 localStorage 是否包含以下键之一:tensorflowjs_models/model-name/info, tensorflowjs_models/my-model-1/model_topology, ... 它将如下所示:

    function createModel() {
      const model = tf.sequential();
      model.add(tf.layers.dense({units: 1, inputShape: [1]}));
      model.add(tf.layers.dense({units: 1}));
      return model
    }
    
    let myModel;
    if (localStorage['tensorflowjs_models/model-name/model_info']) {
     // the model exist
     myModel = await model.save('localstorage://model-name');
    } else {
     myModel = createModel()
    }
    

    【讨论】:

    • 非常感谢。这正是我想要的。
    • 你知道当浏览器最小化时有什么方法可以继续训练纪元吗?
    • 看来浏览器规范是这样的
    猜你喜欢
    • 2020-05-09
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2016-02-15
    • 1970-01-01
    相关资源
    最近更新 更多