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

Error when checking input: expected dense_Dense1_input to have x dimension(s). but got array with shape y,z

总的来说,我对 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);
});

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

您的错误来自来自一方面的训练和测试数据的大小 与定义为模型输入的内容不匹配

model.add(tf.layers.dense({units: 1, inputShape: [5, 5] }));

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

model.predict(tf.tensor(5))

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

这是一个有效的 snipet