连接多个卷积层

时间:2018-07-14 13:49:33

标签: python conv-neural-network lstm keras-2

通过使用多个串联的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'])

还是同样的错误!

1 个答案:

答案 0 :(得分:0)

from keras import Input
from keras import Model
vocab_size = 1000
maxlen = 100
n_out = 1000
input_x = Input(shape=(None,))
x=layers.Embedding(vocab_size, 16, input_length=maxlen)(input_x)
x=layers.Convolution1D(nb_filter=64, filter_length=3,border_mode='same',activation='relu')(x)
input_x1 = Input(shape=(None,))
x1=layers.Embedding(vocab_size, 16, input_length=maxlen)(input_x1)
x1=layers.Convolution1D(nb_filter=64, filter_length=4,border_mode='same', 
activation='relu')(x1)
concatenated = layers.concatenate([x,x1],axis = -1)
x2 = layers.Bidirectional(layers.LSTM(100, return_sequences=False))(concatenated)
x2=layers.Dense(n_out,activation='softmax')(x2)
big_model = Model([input_x,input_x1],output=x2)
big_model.compile(loss='categorical_crossentropy', optimizer='adadelta',
          metrics=['accuracy'])