【问题标题】:expected input_2 to have shape (512,) but got array with shape (1,)预期 input_2 的形状为 (512,) 但得到的数组的形状为 (1,)
【发布时间】:2018-03-12 17:43:30
【问题描述】:

我用 Keras 训练了一个 GRU。跑的时候报错

nxt = model.predict([features,embedding_matrix[enc_map[cur]]])

ValueError: Error when checking : expected input_2 to have shape (512,) but got array with shape (1,)

但是 特征.形状 (512,)` 还有

embedding_matrix[enc_map[cur]].shape (50,)

以下是模型摘要:

model.summary()

________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
================================================================================
input_2 (InputLayer)            (None, 512)          0
________________________________________________________________________________
input_1 (InputLayer)            (None, 50)           0
________________________________________________________________________________
merge_1 (Merge)                 (None, 562)          0           input_2[0][0]
                                                                 input_1[0][0]
________________________________________________________________________________
reshape_1 (Reshape)             (None, 1, 562)       0           merge_1[0][0]
________________________________________________________________________________
gru_1 (GRU)                     (None, 128)          265344      reshape_1[0][0]
_______________________________________________________________________________
dense_1 (Dense)                 (None, 50)           6450        gru_1[0][0]
================================================================================
Total params: 271,794
Trainable params: 271,794
Non-trainable params: 0

【问题讨论】:

    标签: deep-learning keras keras-layer


    【解决方案1】:

    输入必须是 numpy 数组,形状为 (any, 512)

    查看您用于训练的X_train 数据的形状,它必须遵循相同的规则。

    如果这会产生正确的形状,您可以:

    input_data = np.array([features,embedding_matrix[enc_map[cur]]])
    

    但如果此数据有任何问题,并且不符合要求的(any,512),模型将无法使用它。

    【讨论】:

      【解决方案2】:

      您需要将input_data 数组重塑为(512,1) 并转置它。

      input_data = input_data.reshape(512,1).T
      

      【讨论】:

      • 这如何回答原始问题?介意详细一点吗?
      • 请提供您的建议背后的原因,以及它如何回答问题。
      猜你喜欢
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2020-05-14
      • 2019-05-09
      • 2019-03-11
      • 1970-01-01
      • 2020-03-28
      • 2019-08-10
      相关资源
      最近更新 更多