keras-team/keras · error · ImportError

Layer HashedCrossing requires TensorFlow. Install it via `pi

Error message

Layer HashedCrossing requires TensorFlow. Install it via `pip install tensorflow`.

What it means

The HashedCrossing layer relies on TensorFlow ops internally, so Keras raises ImportError at construction time if the tensorflow package is not importable. It cannot run on jax or torch backends regardless of keras config.

Source

Thrown at keras/src/layers/preprocessing/hashed_crossing.py:82

    >>> layer((feat1, feat2))
    array([[0., 1., 0., 0., 0.],
            [0., 0., 0., 0., 1.],
            [0., 1., 0., 0., 0.],
            [0., 1., 0., 0., 0.],
            [0., 0., 0., 1., 0.]], dtype=float32)
    """

    def __init__(
        self,
        num_bins,
        output_mode="int",
        sparse=False,
        name=None,
        dtype=None,
        **kwargs,
    ):
        if not tf.available:
            raise ImportError(
                "Layer HashedCrossing requires TensorFlow. "
                "Install it via `pip install tensorflow`."
            )

        if output_mode == "int" and dtype is None:
            dtype = "int64"

        super().__init__(name=name, dtype=dtype)
        if sparse and backend.backend() != "tensorflow":
            raise ValueError(
                "`sparse=True` can only be used with the TensorFlow backend."
            )

        argument_validation.validate_string_arg(
            output_mode,
            allowable_strings=("int", "one_hot"),
            caller_name=self.__class__.__name__,
            arg_name="output_mode",

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. pip install tensorflow and switch the Keras backend to tensorflow (KERAS_BACKEND=tensorflow)
  2. Replace HashedCrossing with a backend-agnostic alternative (e.g. combine features and use Hashing) if you must stay on jax/torch
  3. Guard the import and skip crossing features on non-TF setups

Example fix

// before
KERAS_BACKEND=jax python train.py  # uses layers.HashedCrossing
// after
pip install tensorflow  # then
KERAS_BACKEND=tensorflow python train.py
Defensive patterns

Strategy: fallback

Validate before calling

from keras.src.backend import tensorflow as tf
if not tf.available:
    raise RuntimeError("HashedCrossing needs TensorFlow installed")

Type guard

def can_use_hashed_crossing():
    from keras.src.backend import tensorflow as tf
    return tf.available

Try / catch

catch ImportError and either install TensorFlow or replace the crossing with a backend-agnostic preprocessing step

Prevention

When it happens

Trigger: Constructing layers.HashedCrossing(...) in a Keras 3 process without tensorflow installed, or with KERAS_BACKEND=jax/torch.

Common situations: Running Keras 3 with KERAS_BACKEND=jax or torch and using FeatureSpace crossings or the HashedCrossing layer directly; CI images without TensorFlow installed.

Related errors


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