首页 > 解决方案 > 使用 Keras 的 LSTM,输入到第一层的尺寸误差

问题描述

如上所述,我的 LSTM 网络中的输入数据维度存在问题,这给了我下一个错误:

ValueError:输入 0 与层 lstm_1 不兼容:预期 ndim=3,发现 ndim=2

我的代码:

input_data = Input((20,))
z = Embedding(5000, input_length=20, output_dim=80, 
weights[embedding_matrix])(input_data)

#z:(?,25,100)->(batch_size?,25-sentence length,100-embedding_size)

z = BatchNormalization(axis=-1)(z)
z = LSTM(256,return_sequences=True)(inputs)
z = LSTM(128)(z)
z = Dense(64, activation='relu')(z)
out = Dense(1, activation='sigmoid')(z)

model = Model(input_data,out)

我在第一个 LSTM 层中遇到了错误。

我知道问题出"?"在 in axis=0,试图解决这个问题。这些是我尝试过的事情:

  1. 受此解决方案的启发: “https://stackoverflow.com/questions/49721810/lstm-keras-value-input-dimension-error”

    x = x.reshape(1, x.shape[0], x.shape 1 )

    但我得到了这个错误:

    IndexError:元组索引超出范围

  2. 也试过:

    x = np.expand_dims(x,axis=0)

    但没有,因为类型不匹配。

我尝试了更多解决方案,但没有运气,如果有人可以帮助我解决这个问题,我会很高兴。

谢谢!!

标签: pythonkeras

解决方案


推荐阅读