【问题标题】:Error when checking input: expected dense_Dense1_input to have x dimension(s). but got array with shape y,z检查输入时出错:预期 dense_Dense1_input 具有 x 维。但得到了形状为 y,z 的数组
【发布时间】:2018-07-13 13:52:12
【问题描述】:

总的来说,我对 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。所以我怀疑我以某种方式错误地输入或映射了我的数据。

【问题讨论】:

    标签: javascript tensorflow tensorflow.js


    【解决方案1】:

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-18
      • 2020-02-25
      • 2022-11-03
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      相关资源
      最近更新 更多