代码
<!DOCTYPE html>
<html>
<head>
<title>Tensor Test</title>
</head>
<body>
<script src="https://cdnjs.loli.net/ajax/libs/tensorflow/2.0.0/tf.js"></script>
<script type="text/javascript">
//配合使用async/await
(async()=>{
// 建立并编译模型
model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
// 生成一些用于训练的数据.
xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]);
ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]);
// 用 fit() 训练模型.
await model.fit(xs, ys, {epochs: 100});
// 用 predict() 推理.
model.predict(tf.tensor2d([[5]], [1, 1])).print();
})();
</script>
</body>
</html>