Home > other >  convert RNN to biRNN
convert RNN to biRNN

Time:04-30

I want to convert this below RNN into bidirectional RNN, how can I do that?

code for RNN

def RNN():
    inputs = Input(name='inputs',shape=[max_len])
    layer = Embedding(max_words,100,input_length=max_len, weights=[embedding_matrix_vocab])(inputs)
    layer = LSTM(64)(layer)
    layer = Dense(356,name='FC1')(layer)
    layer = Activation('relu')(layer)
    layer = Dense(356,name='FC2')(layer)
    layer = Activation('relu')(layer)
    layer = Dropout(0.5)(layer)
    layer = Dense(6,name='out_layer')(layer)
    layer = Activation('softmax')(layer)
    model = Model(inputs=inputs,outputs=layer)
    return model
#Call the function and compile the model.

model = RNN()
model.summary()
model.compile(loss='categorical_crossentropy',optimizer=RMSprop(),metrics=['accuracy'])
model.fit(X_train,Y_train,batch_size=10,epochs=20,
          validation_split=0.1)

CodePudding user response:

You can use Bidirectional layer:

def RNN():
    inputs = Input(name='inputs',shape=[max_len])
    layer = Embedding(max_words, 100, input_length=max_len, 
                      weights=[embedding_matrix_vocab])(inputs)
    layer = Bidirectional(LSTM(64))(layer)
    layer = Dense(356,name='FC1')(layer)
    layer = Activation('relu')(layer)
    layer = Dense(356,name='FC2')(layer)
    layer = Activation('relu')(layer)
    layer = Dropout(0.5)(layer)
    layer = Dense(6,name='out_layer')(layer)
    layer = Activation('softmax')(layer)
    model = Model(inputs=inputs,outputs=layer)
    return model
  • Related