【发布时间】:2021-07-08 06:06:37
【问题描述】:
我正在尝试运行这个线性回归模型,它基本上会给我一个基于 const prediction = model.predict((tf.tensor2d([20], [1,1]))); 的输出,但不幸的是,每次我运行代码以接收预测时都会得到 NaN 值。
解决问题的最佳方法是什么?还有其他方法吗?
谢谢!
下面是代码:
async function learnLinear() {
const fontSize = document.getElementById("count").innerHTML;
const parsed = parseInt(fontSize);
const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
const learningRate = 0.0001;
const optimizer = tf.train.sgd(learningRate);
model.compile({
loss: "meanSquaredError",
optimizer: "sgd",
});
const xs = tf.tensor2d(
[
54,
20,
22,
34,
18,
47,
28,
54,
36,
51,
44,
31,
39,
19,
45,
48,
32,
27,
25,
54,
27,
38,
25,
38,
57,
49,
28,
19,
59,
28,
27,
55,
60,
49,
40,
45,
35,
45,
39,
25,
50,
58,
28,
59,
21,
37,
47,
31,
46,
18,
],
[50, 1]
);
const ys = tf.tensor2d(
[
14,
15,
15,
15,
16,
17,
15,
16,
15,
17,
17,
15,
16,
15,
15,
16,
17,
17,
17,
14,
16,
15,
15,
16,
17,
15,
16,
14,
15,
16,
14,
17,
15,
14,
14,
17,
15,
14,
14,
16,
16,
14,
14,
17,
17,
14,
17,
14,
14,
17,
],
[50, 1]
);
await model.fit(xs, ys, { epochs: 500 });
const prediction = model.predict(tf.tensor2d([20], [1, 1]));
const value = prediction.dataSync()[0];
console.log("Prediction", value);
}
【问题讨论】:
标签: javascript tensorflow linear-regression tensorflow.js