Tensorflow.js在运行线性回归模型时返回“NaN”值

h9a6wy2h  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(108)

我试图运行这个线性回归模型,它基本上会给予我一个基于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);
}
vq8itlhq

vq8itlhq1#

您忘记指定模型应该跟踪的指标。

const batchSize = 32;
const epochs = 500;

model.compile({
  loss: "meanSquaredError",
  optimizer: "sgd",
  metrics: ["mse"],
});

await model.fit(xs, ys, batchSize, epochs);

const prediction = model.predict(tf.tensor2d([20], [1, 1]));
eivnm1vs

eivnm1vs2#

对于那些在未来可能会遇到这种错误的人。
对我有用的是将损失函数从"meanSquaredError"更改为(loss: "meanSquaredError"loss: "meanAbsoluteError"):

...
model.compile({
  loss: "meanAbsoluteError",
  optimizer: "sgd",
});
...

meanAbsoluteError损失函数是什么?

计算我们的模型预测输出距离目标有多远(即取它们的差值),然后取差值的绝对值,使其为正,最后返回差值的平均值。
数学上:

meanAbsoluteError = average(absolute(modelOutput - target))

modelOutput = [2.0, 3.4, 3.3]
target = [2.1, 3.2, 3.4]

meanAbsoluteError = average(|2.0 - 2.1| + |3.4 - 3.2| + |3.3 - 3.4|)

meanAbsoluteError = 0.13

相关问题