Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 模型预测NaN_Tensorflow_Tensorflow.js - Fatal编程技术网

Tensorflow 模型预测NaN

Tensorflow 模型预测NaN,tensorflow,tensorflow.js,Tensorflow,Tensorflow.js,我正在尝试在Tensorflow.js上学习和练习。 因此,我尝试在[,2]形状的数组上训练一个神经网络,作为x(据我所知,这将模拟一个问题,其中我有x个样本,每个样本有2个变量)和一个[,1]数组作为y(如果我是正确的,这意味着我的2个变量的组合产生1个输出) 我试着编写代码: const model = tf.sequential(); model.add(tf.layers.dense({ units: 2, inputShape: [2] })); mod

我正在尝试在Tensorflow.js上学习和练习。 因此,我尝试在[,2]形状的数组上训练一个神经网络,作为x(据我所知,这将模拟一个问题,其中我有x个样本,每个样本有2个变量)和一个[,1]数组作为y(如果我是正确的,这意味着我的2个变量的组合产生1个输出)

我试着编写代码:

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]输入时,我有以下控制台输出:

张量 [NaN],]

所以,我认为我的问题可能很简单,但我真的被这个问题困扰着,可能是因为我缺少一些背景知识


谢谢大家!

第一层采用输入数据的形状

model.add(tf.layers.dense({ units: 2, inputShape: [2] }))
inputShape为[2],这意味着您的输入x为[2]形。 最后一层
unit
值给出了输出y的尺寸

 model.add(tf.layers.dense({ units: 1, inputShape: [64] }));
所以y的形状应该是[1]

在这种情况下,
NaN
预测与您的训练记录数相关。如果将其减少到2或3,它将返回一个数值。实际上,错误与优化器更新权重的方式有关。或者,您可以将优化器更改为
adam
,这样就可以了