检查输入时出错:预期density_Dense1_input具有x尺寸。但是得到了形状为y,z的数组

Ewa*_*ine 5 javascript tensorflow tensorflow.js

一般来说,我对Tensorflowjs和Tensorflow还是很陌生。我有一些数据,容量使用率超出100%,因此数字介于0和100之间,并且每天有5个小时记录这些容量。所以我有一个5天的矩阵,其中100%中包含5个百分比。

我有以下模型:

const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [5, 5] }));
model.compile({ loss: 'binaryCrossentropy', optimizer: 'sgd' });

// Input data
// Array of days, and their capacity used out of 
// 100% for 5 hour period
const xs = tf.tensor([
  [11, 23, 34, 45, 96],
  [12, 23, 43, 56, 23],
  [12, 23, 56, 67, 56],
  [13, 34, 56, 45, 67],
  [12, 23, 54, 56, 78]
]);

// Labels
const ys = tf.tensor([[1], [2], [3], [4], [5]]);

// Train the model using the data.
model.fit(xs, ys).then(() => {
  model.predict(tf.tensor(5)).print();
}).catch((e) => {
  console.log(e.message);
});
Run Code Online (Sandbox Code Playgroud)

我收到返回错误:Error when checking input: expected dense_Dense1_input to have 3 dimension(s). but got array with shape 5,5。因此,我怀疑我以某种方式错误地输入或映射了我的数据。

edk*_*ked 4

您的错误一方面来自于训练数据和测试数据的大小不匹配,另一方面又来自于模型输入的定义

model.add(tf.layers.dense({units: 1, inputShape: [5, 5] }));
Run Code Online (Sandbox Code Playgroud)

inputShape您的输入尺寸。这里是 5,因为每个特征都是一个大小为 5 的数组。

model.predict(tf.tensor(5))
Run Code Online (Sandbox Code Playgroud)

另外,为了测试您的模型,您的数据应该具有与训练模型时相同的形状。您的模型无法预测任何内容tf.tensor(5)。因为你的训练数据和测试数据大小不匹配。考虑这个测试数据tf.tensor2d([5, 1, 2, 3, 4], [1, 5])

这是一个工作片段