Bidirectional sequence encoding
Text and other ordered sequences where information from both earlier and later positions matters.
Keras · Class
Bidirectional wraps a sequence-processing RNN or compatible layer to read inputs in forward and reverse directions. It combines the two results using a selectable merge mode, defaulting to concatenation.
Bidirectional wraps a recurrent or compatible sequence-processing layer and runs separate forward and backward copies. Their outputs are combined according to merge_mode, or returned separately when merging is disabled.
keras.layers.Bidirectional(layer, merge_mode="concat", weights=None, backward_layer=None, **kwargs)| Name | Type | Description |
|---|---|---|
layer | keras.layers.RNN or compatible keras.layers.Layer | Forward sequence-processing RNN or compatible layer. |
merge_mode | str or None | How forward and backward outputs are combined; valid values are sum, mul, concat, ave, or None. |
backward_layer | keras.layers.RNN or compatible keras.layers.Layer | Optional independently configured layer for backward processing. |
**kwargs | dict | Additional layer keyword arguments. |
A Bidirectional layer whose call output is the merged directional output, or a list when merge_mode is None.
from keras import Sequential
from keras.layers import Bidirectional, LSTM, Dense, Input
model = Sequential([
Input(shape=(5, 10)),
Bidirectional(LSTM(10, return_sequences=True)),
Dense(5, activation="softmax"),
])Wrap an LSTM so the sequence is processed in both directions, then merge the two outputs by concatenation.
Text and other ordered sequences where information from both earlier and later positions matters.
Stacked recurrent models that need richer contextual representations.
Custom sequence layers that meet the documented interface and serialization requirements.
It creates forward and backward copies of a sequence-processing layer, applies them to the input, and combines their outputs. The default combination is concatenation.
Follow-up: What happens when merge_mode is None? The two directional outputs are returned as a list without merging.
merge_mode controls how the directional results are combined: sum, multiplication, concatenation, averaging, or no merge. Its default is concat.
Follow-up: How does concatenation affect the feature width? It joins both outputs, so the merged feature dimension contains the forward and backward features.
The wrapped layer must accept 3D-or-higher sequence inputs and expose go_backwards, return_sequences, return_state, and input_spec attributes. It must also support serialization through get_config() and from_config().
Follow-up: Can a custom layer be wrapped? Yes, provided it satisfies all of those sequence-processing and serialization requirements.
keras.layers.RNNkeras.layers.LSTMkeras.layers.GRUkeras.Sequential