【问题标题】:ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1ValueError:层顺序的输入0与层不兼容:预期轴-1
【发布时间】:2021-06-25 06:08:23
【问题描述】:

我正在尝试使用输入 dim 2499 训练模型

layers = [2499, 689, 363, 192]
activation = ["relu", "relu", "sigmoid"]
# let's build our model
model = tf.keras.Sequential()
# we add the first layer and the input layer to our network
model.add(Dense(layers[1], input_shape=(layers[0],), activation=actv[0]))
# we add the hidden layers
for (x, i) in enumerate(layers):
    if x > 1 and x != (len(layers) - 1):
        model.add(Dense(i, activation=actv[x]))
        # then add the final layer
        model.add(Dense(layers[-1], activation=actv[-1]))

当我输入 (72,2499) 时,会出现错误:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 2499 but received input with shape (None, 28)

我不明白为什么我会收到此错误,因为数组的最后一个维度 (72,2499) 与输入形状 2499 相同?

【问题讨论】:

  • 您的数据输入形状是(Batch_size, 28),而您希望它是2499。参数input_shape 是基于数据的。
  • @yudhiesh 这让我感到惊讶。输入数组的形状应该是 (x , input_shape) 不是吗?因此,如果我提供 (72 , 2499) 的数组,为什么它不起作用?我在这里错过了什么?
  • 您的输入似乎是 (None,28),而不是 (72,2499)。我建议您使用 your_dataset.element_spec 检查您的输入:tensorflow.org/guide/data#dataset_structure
  • 您的输入显然不是形状 (72, 2499),否则您不会收到此错误。这就是为什么您应该在您的问题中提供一个 complete 示例,即包含所有相关代码和变量的示例。我们不知道您的输入是什么样的,因为它不在您的问题中。
  • 我发现问题是我犯了一个愚蠢的错误,即输入 trainY 而不是 trainX 作为输入。感谢大家的努力

标签: python tensorflow neural-network


【解决方案1】:

来自 cmets

在向模型提供正确的数据(即 trainX)后问题得到解决 按预期工作(从 Bigyan Subedi 转述)

【讨论】:

    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 2021-03-11
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多