【问题标题】:ValueError: Input 0 is incompatible with layer lstm_15: expected ndim=3, found ndim=2ValueError:输入 0 与层 lstm_15 不兼容:预期 ndim=3,发现 ndim=2
【发布时间】:2020-10-10 12:52:18
【问题描述】:

我想将 CNN 层的输出馈送到 LSTM 层,但出现错误ValueError: Input 0 is incompatible with layer lstm_15: expected ndim=3, found ndim=2,代码如下:

inp = Input(shape = (max_length,))
xe = Embedding(vocabulary_size, 300, weights = [embedding_matrix], trainable = False)(inp)
x = Conv1D(512,kernel_size = 2, activation='relu',kernel_initializer = "he_uniform")(xe)
x = GlobalMaxPooling1D()(x)
x = LSTM(128)(x)
x = Dense(11, activation = "sigmoid")(x)

输入形状:

embedding_matrix: (26441, 300)
inp : TensorShape([Dimension(None), Dimension(3146)])
X_train :(1432, 3146)
Y_train: (1432, 11)
vocabulary_size: 26441
max_length: 3146

有人可以帮助我

【问题讨论】:

    标签: python keras lstm text-classification conv-neural-network


    【解决方案1】:

    这是因为当你应用GlobalMaxPooling1D 时,它会返回形状为(batch_size, 512) 的张量。正如docs 中提到的,该层通过在时间维度上取最大值来对输入表示进行下采样。你有两个选择,要么不使用GlobalMaxPool1D(你可以使用本地池化层,例如MaxPool1D),或者你可以使用RepeatVectorGlobalMaxPool1D的输出形状从(batch_size, 512)更改为(batch_size, n, 512)其中nRepeatVector 的参数,用于定义您希望序列重复多少次。

    【讨论】:

      猜你喜欢
      • 2019-06-04
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2019-04-14
      • 2018-09-25
      • 2017-11-18
      相关资源
      最近更新 更多