Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created October 10, 2019 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuc-arc-f/aec489ff13513b6ecc71778bfdba7129 to your computer and use it in GitHub Desktop.
Save kuc-arc-f/aec489ff13513b6ecc71778bfdba7129 to your computer and use it in GitHub Desktop.
tensorflow.js , 学習model 保存のサンプル
<!--
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