2

Text classification by extracting tri-grams and quad-grams features of character level inputs using multiple concatenated CNN layers and passing it to BLSTM layer

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'))

Model summary of individual conv layers:

Layer (type)                  Output Shape              Param 
------------                  ------------              -----
embedding_49 (Embedding)      (None, 1024, 16)          592       
conv1d_41 (Conv1D)           (None, 1024, 64)           4160      

But, I am getting this error:

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

UPDATE NOW USING FUNCTIONAL 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'])

Still the same error!

Roma Jain
  • 333
  • 4
  • 13

1 Answers1

1
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'])
Krishna
  • 6,107
  • 2
  • 40
  • 43
  • Input shape will be inferred in this case.This may or may not solve your problem. I've changed few more things, if you're comfortable with functional api you can understand. – Krishna Jul 14 '18 at 16:16
  • None as shape did help! but flatten() layer was missing if we convert into functional api..anyways thank you !:) – Roma Jain Jul 14 '18 at 16:22
  • Will you explain the flow and what is happening – Golden Lion Jul 27 '21 at 15:02