Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

layers.Bidirectional

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.

How it works

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.

Syntax

keras.layers.Bidirectional(layer, merge_mode="concat", weights=None, backward_layer=None, **kwargs)

Parameters

NameTypeDescription
layerkeras.layers.RNN or compatible keras.layers.LayerForward sequence-processing RNN or compatible layer.
merge_modestr or NoneHow forward and backward outputs are combined; valid values are sum, mul, concat, ave, or None.
backward_layerkeras.layers.RNN or compatible keras.layers.LayerOptional independently configured layer for backward processing.
**kwargsdictAdditional layer keyword arguments.

Returns

A Bidirectional layer whose call output is the merged directional output, or a list when merge_mode is None.

Key points

  • Bidirectional is a wrapper layer for recurrent neural networks.
  • merge_mode accepts sum, multiplication, concatenation, averaging, or None.
  • When unspecified, directional outputs are concatenated.
  • Calling the wrapper uses the same call arguments as its wrapped recurrent layer.
  • A backward layer is generated automatically when one is not supplied.
  • Disabling merging returns the forward and backward results in a list.

Example: Bidirectional LSTM

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.

When to use it

Bidirectional sequence encoding

Text and other ordered sequences where information from both earlier and later positions matters.

Bidirectional recurrent stacks

Stacked recurrent models that need richer contextual representations.

Wrapping compatible custom layers

Custom sequence layers that meet the documented interface and serialization requirements.

Interview questions

QWhat does Bidirectional do?

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.

QWhich merge modes are supported?

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.

QWhat must a custom wrapped layer provide?

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.RNN
  • keras.layers.LSTM
  • keras.layers.GRU
  • keras.Sequential

Source: Keras documentation: Bidirectional layer. Checked 2026-10-15.