keras-team/keras · error · ValueError

Please initialize `Bidirectional` layer with a…

Error message

Please initialize `Bidirectional` layer with a `keras.layers.Layer` instance. Received: {layer}

What it means

Raised by Bidirectional.__init__ when the `layer` argument is not an instance of keras.layers.Layer. Bidirectional wraps a recurrent layer to run it forward and backward, so it requires an actual Layer object (e.g. LSTM, GRU), not a class, string, or arbitrary callable.

Solutions

  1. Pass an instantiated layer, e.g. Bidirectional(LSTM(10)) not Bidirectional(LSTM)
  2. Do not pass layer names (strings) or functions; wrap them in a Layer instance first
  3. For the backward pass, supply the layer via `layer` and optionally `backward_layer`
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/rnn/bidirectional.py:95 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/37719f292f4aca07. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/layers/rnn/bidirectional.py:95

    model = Sequential([
        Input(shape=(5, 10)),
        Bidirectional(forward_layer, backward_layer=backward_layer),
        Dense(5, activation="softmax"),
    ])
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
    ```
    """

    def __init__(
        self,
        layer,
        merge_mode="concat",
        weights=None,
        backward_layer=None,
        **kwargs,
    ):
        if not isinstance(layer, Layer):
            raise ValueError(
                "Please initialize `Bidirectional` layer with a "
                f"`keras.layers.Layer` instance. Received: {layer}"
            )
        if backward_layer is not None and not isinstance(backward_layer, Layer):
            raise ValueError(
                "`backward_layer` need to be a `keras.layers.Layer` "
                f"instance. Received: {backward_layer}"
            )
        if merge_mode not in ["sum", "mul", "ave", "concat", None]:
            raise ValueError(
                f"Invalid merge mode. Received: {merge_mode}. "
                "Merge mode should be one of "
                '{"sum", "mul", "ave", "concat", None}'
            )
        super().__init__(**kwargs)

        # Recreate the forward layer from the original layer config, so that it
        # will not carry over any state from the layer.

View on GitHub (pinned to 7a34a03db6)