How to convert LSTM model to BiLSTM model in keras?

≡放荡痞女 提交于 2020-03-25 16:03:12

问题


I have the follwing two LSTM models where;

  • model 1: first LSTM layer and then CNN layer
  • model 2: first CNN layer and then LSTM layer

Code of Model 1:

model = Sequential()
model.add(LSTM(100, return_sequences=True, input_shape=(25,3)))
model.add(Conv1D(100, kernel_size=3, activation = 'relu'))
model.add(GlobalMaxPooling1D())
model.add(Dense(50))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

Code of Model 2:

model = Sequential()
model.add(Conv1D(100, kernel_size=3, activation = 'relu', input_shape=(25,3)))
model.add(LSTM(100))
model.add(Dense(50))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

Now I want to convert my LSTM to BiLSTM. I know that in keras it is done as follows Bidirectional(LSTM()).

However, I am not sure if there are any other changes that I should do when passing data to CNN.

I am happy to provide more details if needed.

EDIT:

Code of Model 1:

model = Sequential()
model.add(Bidirectional(LSTM(100, return_sequences=True), input_shape=(25,3)))
model.add(Conv1D(100, kernel_size=3, activation = 'relu'))
model.add(GlobalMaxPooling1D())
model.add(Dense(50))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

Code of Model 2:

model = Sequential()
model.add(Conv1D(100, kernel_size=3, activation = 'relu', input_shape=(25,3)))
model.add(Bidirectional(LSTM(100)))
model.add(Dense(50))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

EDIT (One further clarification):

Suppose I have the following Bidirectional LSTM model.

inputs = Input((25,3))    
side1 = Bidirectional(LSTM(100, return_sequences=True))(inputs)
side2 = Conv1D(200, activation = 'tanh', padding = 'same')(inputs)

merged = Add()([side1, side2])     
outputs = Conv1D(200)(merged)
outputs = GlobalMaxPooling1D()(outputs)
outputs = Dense(100)(outputs)
outputs = Dense(1, activation='sigmoid')(outputs)

model = Model(inputs, outputs)

If I want to convert it a normal LSTM model, is it fine if I do it as follows.

inputs = Input((25,3))    
side1 = LSTM(200, return_sequences=True)(inputs)
side2 = Conv1D(200, activation = 'tanh', padding = 'same')(inputs)

merged = Add()([side1, side2])     
outputs = Conv1D(200)(merged)
outputs = GlobalMaxPooling1D()(outputs)
outputs = Dense(100)(outputs)
outputs = Dense(1, activation='sigmoid')(outputs)

model = Model(inputs, outputs)

Please kindly let me know your thoughts.

来源:https://stackoverflow.com/questions/60670540/how-to-convert-lstm-model-to-bilstm-model-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!