我试图运行这个线性回归模型,它基本上会给予我一个基于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);
}
2条答案
按热度按时间vq8itlhq1#
您忘记指定模型应该跟踪的指标。
eivnm1vs2#
对于那些在未来可能会遇到这种错误的人。
对我有用的是将损失函数从
"meanSquaredError"
更改为(loss: "meanSquaredError"
到loss: "meanAbsoluteError"
):meanAbsoluteError损失函数是什么?
计算我们的模型预测输出距离目标有多远(即取它们的差值),然后取差值的绝对值,使其为正,最后返回差值的平均值。
数学上: