amy
amy

Reputation: 352

Concatenating features of two pooling layers

I am trying to design a Bi-Directional LSTM model and I want to concatenate features after Max pooling and Average pooling layers. I have this for my model:

from keras.layers import Dense, Embedding
from keras.layers.recurrent import LSTM
from keras.layers import Bidirectional
from keras.models import Sequential
from keras.layers.core import Dropout
from features import train,embedding_matrix,words
from keras.layers import concatenate,AveragePooling1D,GlobalMaxPooling1D

model=Sequential()

model.add(Embedding(words,300,input_length=train.shape[1],weights=[embedding_matrix]))
model.add(Bidirectional(LSTM(20,activation='tanh',kernel_initializer='glorot_uniform',recurrent_dropout = 0.2, dropout = 0.2,return_sequences=True)))

model.add(concatenate([GlobalMaxPooling1D(),AveragePooling1D()]))

model.add(Dropout(0.2))

model.add(Dense(2, activation='softmax'))

print model.summary()

But I am having: ValueError: Layer concatenate_1 was called with an input that isn't a symbolic tensor which is because I believe the concatenating layer. As I am not adding the pooling in the model.

Can I add two layers in the same model? or Should I define two separate models and then add pooling layers in each of them?

Upvotes: 1

Views: 1409

Answers (1)

pitfall
pitfall

Reputation: 2621

The trick here is to use a graph model instead of a sequential model.

Before we get started, I assume

  1. your network expects a 2D input tensor of shape (B=batch_size, N=num_of_words), where N is the longest sample length of your training data. (In case you have unequal length samples, you should use keras.preprocessing.sequence.pad_sequences to achieve equal length samples)
  2. your vocabulary size is V (probably is 300 if I understand correctly)
  3. your embedding layer encodes each word to a feature of F dimension, i.e. your embedding layer's weight matrix is VxF.

    from keras.layers import Dense, Embedding, Input, Concatenate, Lambda from keras.layers.recurrent import LSTM from keras.layers import Bidirectional from keras.models import Model from keras.layers.core import Dropout from keras import backend as BKN

    from keras.layers import concatenate,AveragePooling1D,GlobalMaxPooling1D

    words = Input( shape=(N,))

    f = Embedding(input_dim=V,output_dim=F)( words ) f = Bidirectional(LSTM(20,activation='tanh', kernel_initializer='glorot_uniform', recurrent_dropout = 0.2, dropout = 0.2,return_sequences=True))(f) gpf = GlobalMaxPooling1D()(f) gpf = Lambda( lambda t : BKN.expand_dims(t, axis=1) )(gpf) apf = AveragePooling1D( pool_size=2 )(f) pf = Concatenate(axis=1)([gpf, apf]) pf = Dropout(0.2)( pf ) pred = Dense(2, activation='softmax')(pf) # <-- make sure this is correct

    model = Model( input=words, output=pred )

Finally, I fail to find that keras Embedding layer supports syntax like weights=[embedding_matrix].

Upvotes: 1

Related Questions