Keras 3: AttributeError module 'keras' has no attribute (tested)

After upgrading to Keras 3, AttributeError: module 'keras.utils' has no attribute 'np_utils', 'keras.layers' has no attribute 'experimental', 'keras.backend' has no attribute 'get_session' and the keras.optimizers.legacy ImportError all come from removed Keras 2 paths. Each was checked on Keras 3.15 below, with its replacement.

1. Which names still exist in Keras 3

import keras
import tensorflow as tf

checks = {
    "keras.utils.to_categorical": lambda: keras.utils.to_categorical,
    "keras.utils.np_utils": lambda: keras.utils.np_utils,
    "keras.utils.Sequence": lambda: keras.utils.Sequence,
    "keras.utils.PyDataset": lambda: keras.utils.PyDataset,
    "keras.backend.get_session": lambda: keras.backend.get_session,
    "keras.optimizers.Adam": lambda: keras.optimizers.Adam,
    "keras.optimizers.legacy": lambda: keras.optimizers.legacy.Adam,
    "tf.keras.backend.set_session": lambda: tf.keras.backend.set_session,
    "keras.layers.experimental": lambda: keras.layers.experimental,
}
for name, get in checks.items():
    try:
        get()
        print("ok   ", name)
    except (AttributeError, ImportError) as e:
        print("ERROR", name, "->", type(e).__name__ + ":", e)
ok    keras.utils.to_categorical
ERROR keras.utils.np_utils -> AttributeError: module 'keras.utils' has no attribute 'np_utils'
ok    keras.utils.Sequence
ok    keras.utils.PyDataset
ERROR keras.backend.get_session -> AttributeError: module 'keras.backend' has no attribute 'get_session'
ok    keras.optimizers.Adam
ok    keras.optimizers.legacy
ERROR tf.keras.backend.set_session -> AttributeError: module 'keras._tf_keras.keras.backend' has no attribute 'set_session'
ERROR keras.layers.experimental -> AttributeError: module 'keras.layers' has no attribute 'experimental'

keras.optimizers.legacy is a trap: the attribute exists, but using it fails:

import keras

try:
    keras.optimizers.legacy.Adam(learning_rate=1e-3)
except ImportError as e:
    print("ImportError:", e)

# Replacements that exist in Keras 3
print(keras.utils.to_categorical([0, 2, 1], num_classes=3).tolist())  # was keras.utils.np_utils.to_categorical
print(keras.layers.Rescaling, keras.layers.RandomFlip)                 # were keras.layers.experimental.preprocessing.*
print(keras.optimizers.Adam(learning_rate=1e-3).learning_rate.numpy()) # not optimizers.legacy
keras.backend.clear_session()                                          # replaces get_session/set_session resets
print("clear_session ok")
ImportError: `keras.optimizers.legacy` is not supported in Keras 3. When using `tf.keras`, to continue using a `tf.keras.optimizers.legacy` optimizer, you can install the `tf_keras` package (Keras 2) and set the environment variable `TF_USE_LEGACY_KERAS=True` to configure TensorFlow to use `tf_keras` when accessing `tf.keras`.
[[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0]]
<class 'keras.src.layers.preprocessing.rescaling.Rescaling'> <class 'keras.src.layers.preprocessing.image_preprocessing.random_flip.RandomFlip'>
0.001
clear_session ok

2. Replacements

Fails in Keras 3Use instead
keras.utils.np_utils.to_categoricalkeras.utils.to_categorical
keras.layers.experimental.preprocessing.Rescaling (and RandomFlip, etc.)keras.layers.Rescaling, keras.layers.RandomFlip
keras.optimizers.legacy.Adamkeras.optimizers.Adam
keras.backend.get_session() / tf.keras.backend.set_session()Not needed: TF2 has no Session. Use keras.backend.clear_session() to reset state; see module 'tensorflow' has no attribute 'Session'.

If you cannot change old code yet, the error message itself gives the escape hatch: pip install tf_keras and set TF_USE_LEGACY_KERAS=True before importing TensorFlow, so tf.keras is Keras 2 again (we did not run that path).

Related: TensorFlow Keras import errors after Keras 3, Keras "Invalid filepath extension for saving", ImageDataGenerator deprecated.

Frequently asked questions

What replaces keras.utils.np_utils.to_categorical?

keras.utils.to_categorical; the np_utils submodule no longer exists.

What replaces keras.layers.experimental.preprocessing?

The same layers directly under keras.layers, e.g. keras.layers.Rescaling and keras.layers.RandomFlip.

Can I still use keras.optimizers.legacy?

Not in Keras 3. Use keras.optimizers.Adam etc., or install tf_keras and set TF_USE_LEGACY_KERAS=True to keep Keras 2.

Written with AI assistance. Measured on 2026-10-24 with tensorflow-cpu 2.21.0, keras 3.15.1 and Python 3.13.5 on Linux. The scripts ran verbatim and the output was pasted unedited (TensorFlow startup log lines filtered). PlainTables is our extension; Keras and TensorFlow are free.