【发布时间】:2018-12-22 17:15:54
【问题描述】:
通过使用多个连接的 CNN 层提取字符级输入的三元组和四元组特征并将其传递给 BLSTM 层来进行文本分类
submodels = []
for kw in (3, 4): # kernel sizes
model = Sequential()
model.add(Embedding(vocab_size, 16,input_length=maxlen,input_shape=(maxlen,vocab_size))
model.add(Convolution1D(nb_filter=64, filter_length=kw,
border_mode='valid', activation='relu'))
submodels.append(model)
big_model = Sequential()
big_model.add(keras.layers.Concatenate(submodels))
big_model.add(Bidirectional(LSTM(100, return_sequences=False)))
big_model.add(Dense(n_out,activation='softmax'))
各个卷积层的模型总结:
Layer (type) Output Shape Param
------------ ------------ -----
embedding_49 (Embedding) (None, 1024, 16) 592
conv1d_41 (Conv1D) (None, 1024, 64) 4160
但是,我收到了这个错误:
ValueError:输入 0 与层 conv1d_22 不兼容:预期 ndim=3,发现 ndim=4
现在使用功能性 KERAS API 进行更新
x = Input(shape=(maxlen,vocab_size))
x=Embedding(vocab_size, 16, input_length=maxlen)(x)
x=Convolution1D(nb_filter=64, filter_length=3,border_mode='same',
activation='relu')(x)
x1 = Input(shape=(maxlen,vocab_size))
x1=Embedding(vocab_size, 16, input_length=maxlen)(x1)
x1=Convolution1D(nb_filter=64, filter_length=4,border_mode='same',
activation='relu')(x1)
x2 = Bidirectional(LSTM(100, return_sequences=False))
x2=Dense(n_out,activation='softmax')(x2)
big_model = Model(input=keras.layers.Concatenate([x,x1]),output=x2)
big_model.compile(loss='categorical_crossentropy', optimizer='adadelta',
metrics=['accuracy'])
还是同样的错误!
【问题讨论】:
-
所以,错误说明了答案。你必须通过
input_shape -
@RedEyed 是的,添加它后,它给出了上面编辑的 conv 不兼容错误
-
能否请您显示模型摘要。
-
@krishna 是的,krishna,我在上面添加了 model.summary
-
Embedding 的输入形状应该是 shape = (num_samples,sequence_length)。如果您不确定是否可以删除 input_shape 参数。但是它会在 big_model 处给出错误:(
标签: python conv-neural-network lstm keras-2