tensorflow.js , 学習model 保存のサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
tensorflow.js のテスト ,回帰モデル | |
https://www.tensorflow.org/js/guide/layers_for_keras_users | |
2019/10/09: 学習model 保存のサンプル | |
---> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>TensorFlow.js Tutorial</title> | |
<!-- Import TensorFlow.js --> | |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js"></script> | |
<!-- Import tfjs-vis --> | |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis@1.0.2/dist/tfjs-vis.umd.min.js"></script> | |
<!-- chart.js --> | |
</head> | |
<body> | |
<h1>tensorflow.js test</h1> | |
<hr /> | |
<canvas id="myChart" ></canvas> | |
</body> | |
<script> | |
console.log('Hello TensorFlow, t1-1605'); | |
async function test() { | |
// Build and compile model. | |
const model = tf.sequential(); | |
model.add(tf.layers.dense({units: 1, inputShape: [1]})); | |
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'}); | |
// Generate some synthetic data for training. | |
const xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]); | |
const ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]); | |
var num_pred = 5; | |
await model.fit(xs, ys, {epochs: 10 }); | |
// model.predict(tf.tensor2d([[5]], [1, 1])).print(); | |
var model_name = "localstorage://my-model-1" | |
await model.save(model_name ) | |
const load_model = await tf.loadLayersModel(model_name); | |
console.log('#load_model'); | |
load_model.predict(tf.tensor2d([[10]], [1, 1])).print(); | |
} | |
// | |
test(); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment