【问题标题】:Input dense is incompatible with the layer invalid shape输入密集与层无效形状不兼容
【发布时间】:2021-02-07 05:51:42
【问题描述】:

我的模型有这个简单的层

states = Input(shape=(len(inputFinal),))

这应该会生成一个 (328,None) 但不知道为什么当我检查倒置时

model.inputs
[<tf.Tensor 'input_1:0' shape=(None, 328) dtype=float32>]

当我尝试将数据传递给模型时,图层不正确

value.shape
(328,)

model.predict(value)

Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 328 but received input with shape [None, 1]

我找不到问题,有什么想法吗?

【问题讨论】:

    标签: python tensorflow keras keras-layer tf.keras


    【解决方案1】:

    指定输入形状时,只需要指定特征个数即可。 Keras 不想知道样本的数量,因为它可以接受任何大小。所以,当你这样做时:

    states = Input(shape=(len(inputFinal),))
    

    您告诉 Keras 您的输入有 328 列,但事实并非如此。当您输入输入时,Keras 会意识到这一点,然后崩溃。

    如果inputFinal 是二维 NumPy 数组,请尝试:

    Input(shape=inputFinal.shape[1:])
    

    【讨论】:

    • 谢谢你的答案,但以你的例子我得到了这个Error converting shape to a TensorShape: Dimension value must be integer or None or have an __index__ method, got value '(328,)' with type '&lt;class 'tuple'&gt;'.
    • 好的,我稍微改变了我的答案。或者试试shape=328
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 2023-02-07
    • 2021-09-04
    • 1970-01-01
    • 2021-10-17
    • 2021-12-28
    相关资源
    最近更新 更多