Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
tensorflow.js 回帰モデルのブラウザ版 テスト
<!DOCTYPE html>
<!--
tensorflow.js 回帰モデルのブラウザ版 テスト
--->
<html lang="ja">
<!-- 日本語 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js"></script>
</head>
<body>
<h1>tensorflow.js test</h1>
<hr />
<!-- -->
<script>
const nr_epochs= 10;
// 回帰
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]} ));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
const h =model.fit(xs,ys,{epochs: nr_epochs});
var num_pred = 5;
h.then(()=> {
var v= model.predict(tf.tensor2d([num_pred],[1,1]));
// v.print();
console.log("v= " + v.dataSync() );
});
// var v= model.predict(tf.tensor2d([2 ,4 ],[2,1]));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.