What's the difference between a bidirectional LSTM and an LSTM?

后端 未结 5 973
闹比i
闹比i 2021-01-29 20:10

Can someone please explain this? I know bidirectional LSTMs have a forward and backward pass but what is the advantage of this over a unidirectional LSTM?

What is each o

5条回答
  •  無奈伤痛
    2021-01-29 20:42

    Adding to Bluesummer's answer, here is how you would implement Bidirectional LSTM from scratch without calling BiLSTM module. This might better contrast the difference between a uni-directional and bi-directional LSTMs. As you see, we merge two LSTMs to create a bidirectional LSTM.

    You can merge outputs of the forward and backward LSTMs by using either {'sum', 'mul', 'concat', 'ave'}.

    left = Sequential()
    left.add(LSTM(output_dim=hidden_units, init='uniform', inner_init='uniform',
                   forget_bias_init='one', return_sequences=True, activation='tanh',
                   inner_activation='sigmoid', input_shape=(99, 13)))
    right = Sequential()
    right.add(LSTM(output_dim=hidden_units, init='uniform', inner_init='uniform',
                   forget_bias_init='one', return_sequences=True, activation='tanh',
                   inner_activation='sigmoid', input_shape=(99, 13), go_backwards=True))
    
    model = Sequential()
    model.add(Merge([left, right], mode='sum'))
    
    model.add(TimeDistributedDense(nb_classes))
    model.add(Activation('softmax'))
    
    sgd = SGD(lr=0.1, decay=1e-5, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd)
    print("Train...")
    model.fit([X_train, X_train], Y_train, batch_size=1, nb_epoch=nb_epoches, validation_data=([X_test, X_test], Y_test), verbose=1, show_accuracy=True)
    

提交回复
热议问题