【发布时间】: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