【发布时间】:2018-06-26 13:15:10
【问题描述】:
我正在尝试为一个项目进行一些线性回归。 由于我习惯了 Javascript,因此我决定尝试使用 TensorFlowJS。
我正在关注他们网站上的教程,并观看了一些解释其工作原理的视频,但我仍然无法理解为什么我的算法没有返回我期望的结果。
这是我正在做的事情:
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
// Generate some synthetic data for training.
const xs = tf.tensor1d([1, 2, 3, 4]);
const ys = tf.tensor1d([1, 2, 3, 4]);
// Train the model using the data.
model.fit(xs, ys).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
// Open the browser devtools to see the output
const output = model.predict(tf.tensor2d([5], [1,1]));
console.log(Array.from(output.dataSync())[0]);
});
我在这里尝试制作一个线性图,其中输入应始终等于输出。
我试图预测输入5 会得到什么,但输出似乎是随机的。
这里在 codepen 上,你可以试试:https://codepen.io/anon/pen/RJJNeO?editors=0011
【问题讨论】:
标签: javascript tensorflow tensorflow.js