【问题标题】:Dimension error for convolution2d in keras for text classificationkeras 中用于文本分类的卷积 2d 的维度错误
【发布时间】:2018-06-22 23:28:15
【问题描述】:

我的输入形状是一个 10000x500 的文本文档。 10000代表文档数,500代表单词数。

我要做的是为kera的嵌入提供文本,然后是BLSTM,然后是Conv2D,然后是2Dpooling,展平,最后是一个完全连接的密集层。

架构如下图:

inp = Input(shape=(500,))
x = Embedding(max_features=10000, embed_size=100)(inp)
x = Bidirectional(CuDNNLSTM(50, return_sequences=True))(x)
x = Conv2D(filters=128, kernel_size=(3, 3), input_shape=(100,500,1))(x)
x = MaxPooling2D()(x)
x = Flatten()(x)
x = Dense(1, activation="sigmoid")(x)

嵌入的输出形状为 (None, 500, 100) BLSTM 隐藏状态的输出形状为 (None, 500, 100)。 我想要一个 Conv2D 从 BLSTM 中提取隐藏层上的局部特征。但是,我遇到尺寸差异错误。

ValueError: Input 0 is incompatible with layer conv2d_8: expected ndim=4, found ndim=3

我在这里When bulding a CNN, I am getting complaints from Keras that do not make sense to me. 尝试了一个解决方案,但仍然出现错误。

【问题讨论】:

    标签: keras nlp lstm rnn convolutional-neural-network


    【解决方案1】:

    你有两个选择:

    a) 通过向x 添加维度,将Conv2Drows=100cols=500channels=1 一起使用:

    x = Lambda(lambda t: t[..., None])(x)
    x = Conv2D(filters=128, kernel_size=(3, 3), input_shape=(100,500,1))(x)
    

    b) 将Conv1Dsteps=100input_dim=500 一起使用,并使用MaxPooling1D

    x = Conv1D(filters=128, kernel_size=3, input_shape=(100, 500))(x)
    x = MaxPooling1D()(x)
    x = Flatten()(x)
    

    【讨论】:

    • lambda 函数实际上对形状做了什么?
    • 它在最后一个轴上添加一个维度。它相当于keras.io/backend/#expand_dims with axis=-1
    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2016-12-04
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多