TensorFlow Keras errors after upgrading to Keras 3 (tested)

TensorFlow 2.16+ installs Keras 3, so Keras 2 code fails with No module named 'keras.layers.core', cannot import name 'adam', Argument(s) not recognized: {'lr': 0.01} or Invalid filepath extension for saving. Seven failures and their fixes, tested on TensorFlow 2.21.0 with Keras 3.15.1.

1. Reproduce: Keras 2 code on Keras 3

Since TensorFlow 2.16, pip install tensorflow brings Keras 3, which removed many Keras 2 module paths and arguments. b1.py runs seven common lines from older tutorials:

import os; os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import tensorflow as tf, keras
print("tensorflow", tf.__version__, "| keras", keras.__version__)
old_code = [
    "from keras.layers.core import Dense",
    "from keras.utils.np_utils import to_categorical",
    "from keras.optimizers import adam",
    "from keras.preprocessing.image import ImageDataGenerator",
    "from keras.wrappers.scikit_learn import KerasClassifier",
    "keras.optimizers.Adam(lr=0.01)",
    "keras.Sequential([keras.Input((3,)), keras.layers.Dense(1)]).save('my_model')",
]
for line in old_code:
    try:
        exec(line)
        print("OK  ", line)
    except Exception as e:
        print("FAIL", line, "\n     ", type(e).__name__ + ":", str(e).split(" (/")[0][:170])
tensorflow 2.21.0 | keras 3.15.1
FAIL from keras.layers.core import Dense 
      ModuleNotFoundError: No module named 'keras.layers.core'
FAIL from keras.utils.np_utils import to_categorical 
      ModuleNotFoundError: No module named 'keras.utils.np_utils'
FAIL from keras.optimizers import adam 
      ImportError: cannot import name 'adam' from 'keras.optimizers'
FAIL from keras.preprocessing.image import ImageDataGenerator 
      ImportError: cannot import name 'ImageDataGenerator' from 'keras.preprocessing.image'
FAIL from keras.wrappers.scikit_learn import KerasClassifier 
      ModuleNotFoundError: No module named 'keras.wrappers.scikit_learn'
FAIL keras.optimizers.Adam(lr=0.01) 
      ValueError: Argument(s) not recognized: {'lr': 0.01}
FAIL keras.Sequential([keras.Input((3,)), keras.layers.Dense(1)]).save('my_model') 
      ValueError: Invalid filepath extension for saving. Please add either a `.keras` extension for the native Keras format (recommended) or a `.h5` extension. Use `model.export(filepath)`

(TensorFlow startup log lines filtered.) Every one fails, with three different exception types.

2. Fix: the Keras 3 spelling

import os; os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import keras
from keras.layers import Dense                    # was keras.layers.core
from keras.utils import to_categorical            # was keras.utils.np_utils
from keras.optimizers import Adam                 # class name is capitalised

opt = Adam(learning_rate=0.01)                    # lr= was removed
print("learning_rate:", float(opt.learning_rate))
print(to_categorical([0, 2], num_classes=3).tolist())

model = keras.Sequential([keras.Input((3,)), Dense(1)])
model.save("my_model.keras")                      # native format needs .keras
print("reloaded:", type(keras.models.load_model("my_model.keras")).__name__)
learning_rate: 0.009999999776482582
[[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]
reloaded: Sequential
Keras 2 code (fails)Keras 3 fix
from keras.layers.core import Densefrom keras.layers import Dense
from keras.utils.np_utils import to_categoricalfrom keras.utils import to_categorical
from keras.optimizers import adamfrom keras.optimizers import Adam
Adam(lr=0.01)Adam(learning_rate=0.01)
model.save('my_model')model.save('my_model.keras') (or model.export(path) for a SavedModel)
from keras.preprocessing.image import ImageDataGeneratorkeras.utils.image_dataset_from_directory(...)
from keras.wrappers.scikit_learn import KerasClassifierremoved from Keras; the separate scikeras package provides a wrapper

One surprise from the same environment: from tensorflow.keras.preprocessing.image import ImageDataGenerator still imported fine, while from keras.preprocessing.image import ... did not. If you only need old code to run today, that import path still works; the long-term fix is the table above.

Related: pip install tensorflow: no matching distribution found.

Frequently asked questions

Why do I get No module named keras.layers.core?

Keras 3 (installed by TensorFlow 2.16+) removed keras.layers.core. Import layers directly: from keras.layers import Dense.

Why does Adam(lr=0.01) fail?

Keras 3 removed the lr alias. Use Adam(learning_rate=0.01); the old call raises ValueError: Argument(s) not recognized.

Why does model.save fail with Invalid filepath extension?

Keras 3 needs a .keras (or .h5) extension. Use model.save("my_model.keras"), or model.export(path) for a TensorFlow SavedModel.

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; TensorFlow and Keras are free.