Search code examples
pythonkerasneural-networklstmbidirectional

What's the sequence length of a Keras Bidirectional layer?


If I have:

        self.model.add(LSTM(lstm1_size, input_shape=(seq_length, feature_dim), return_sequences=True))
        self.model.add(BatchNormalization())
        self.model.add(Dropout(0.2))

then my seq_length specifies how many slices of data I want to process at once. If it matters, my model is a sequence-to-sequence (same size).

But if I have:

        self.model.add(Bidirectional(LSTM(lstm1_size, input_shape=(seq_length, feature_dim), return_sequences=True)))
        self.model.add(BatchNormalization())
        self.model.add(Dropout(0.2))

then is that doubling the sequence size? Or at each time step, is it getting seq_length / 2 before and after that timestep?


Solution

  • Using a bidirectional LSTM layer has no effect on the sequence length. I tested this with the following code:

    from keras.models import Sequential
    from keras.layers import Bidirectional,LSTM,BatchNormalization,Dropout,Input
    
    model = Sequential()
    lstm1_size = 50
    seq_length = 128
    feature_dim = 20
    model.add(Bidirectional(LSTM(lstm1_size, input_shape=(seq_length, feature_dim), return_sequences=True)))
    model.add(BatchNormalization())
    model.add(Dropout(0.2))
    
    batch_size = 32
    
    model.build(input_shape=(batch_size,seq_length, feature_dim))
    
    model.summary()
    

    This resulted in the following output for bidirectional

    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    bidirectional_1 (Bidirection (32, 128, 100)            28400     
    _________________________________________________________________
    batch_normalization_1 (Batch (32, 128, 100)            400       
    _________________________________________________________________
    dropout_1 (Dropout)          (32, 128, 100)            0         
    =================================================================
    Total params: 28,800
    Trainable params: 28,600
    Non-trainable params: 200
    

    No bidirectional layer:

    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    lstm_1 (LSTM)                (None, 128, 50)           14200     
    _________________________________________________________________
    batch_normalization_1 (Batch (None, 128, 50)           200       
    _________________________________________________________________
    dropout_1 (Dropout)          (None, 128, 50)           0         
    =================================================================
    Total params: 14,400
    Trainable params: 14,300
    Non-trainable params: 100
    _________________________________________________________________