模型预测 NaN [英] model predicts NaN
问题描述
我正在尝试在 Tensorflow.js 上学习和练习.因此,我尝试在 [,2] 形数组上训练神经网络作为 x(据我所知,这将模拟一个问题,其中我有 x 个样本,每个样本都有 2 个变量)和一个 [,1] 数组作为 y(如果我是对的,我的 2 个变量的组合会生成 1 个输出,这意味着什么).
I am trying to learn and practice on Tensorflow.js. So, I tried to train a neural network on a [,2] shaped array as x (as I understood, this would simulate a problem where I have x samples that each one has 2 variables) and a [,1] array as y (what would mean if I'm correct, that the combination of my 2 variables generate 1 output).
我试着编码:
const model = tf.sequential();
model.add(tf.layers.dense({ units: 2, inputShape: [2] }));
model.add(tf.layers.dense({ units: 64, inputShape: [2] }));
model.add(tf.layers.dense({ units: 1, inputShape: [64] }));
// 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.tensor([[1,5], [2,10], [3,15], [4,20], [5,25], [6,30], [7,35], [8,40]], [8, 2]);
const ys = tf.tensor([1, 2, 3, 4, 5, 6, 7, 8], [8, 1]);
// Train the model using the data.
model.fit(xs, ys, { epochs: 100 }).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
model.predict(tf.tensor([10, 50], [1, 2])).print();
});
但是,我面临的是,当我尝试预测 [10,50] 输入时,我有以下控制台输出:
But, what I am facing is that when I try to predict the [10,50] input, I have the following console output:
张量[[NaN],]
所以,我认为我的问题可能非常简单,但我真的坚持这一点,可能是我缺少一些背景知识的问题.
So, I think my problem might be very simple, but I am really stuck with this and probably it is a matter of some background knowledge I'm missing.
谢谢!
推荐答案
第一层采用输入数据的形状
The first layer takes the shape of the input data
model.add(tf.layers.dense({ units: 2, inputShape: [2] }))
inputShape 为 [2],这意味着您的输入 x 的形状为 [2].最后一层 unit
值给出了输出 y 的维度.
The inputShape is [2], which means that your input x is of shape [2].
The last layer unit
value gives the dimension of the output y.
model.add(tf.layers.dense({ units: 1, inputShape: [64] }));
所以y的形状应该是[1]
So the shape of y should be [1]
在这种情况下,NaN
预测与训练的 epoch 数有关.如果将其减少到 2 或 3,它将返回一个数值.实际上,错误与优化器如何更新权重有关.或者,您可以将优化器更改为 adam
就可以了.
In this case, the NaN
prediction is related to the number of epochs for your training. If you decrease it to 2 or 3, it will return a numerical value. Actually, the error is related to how your optimizer is updating the weights. Alternatively, you can change the optimizer to adam
and it will be fine.
这篇关于模型预测 NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!