Keras Nightly
Keras 3 is a multi-backend deep learning framework that supports JAX, TensorFlow, PyTorch, and OpenVINO (for inference-only). The `keras-nightly` package provides daily development builds of Keras, offering access to the latest features and bug fixes. It focuses on accelerated model development and state-of-the-art performance by leveraging backend-specific optimizations.
Common errors
-
ImportError: cannot import name 'type_spec_registry' from 'tensorflow.python.framework'
cause Version incompatibility between TensorFlow and Keras. This error often occurs when an older TensorFlow version is installed with a newer Keras, or vice-versa, that expects a specific internal structure.fixEnsure that your TensorFlow and Keras installations are compatible. Upgrade TensorFlow to a recent version (e.g., 2.16.1+ for Keras 3). It is recommended to install Keras first, then its chosen backend. -
Import "tensorflow.keras" could not be resolved
cause Attempting to import Keras components from the `tensorflow.keras` namespace with Keras 3.x installed, which uses the standalone `keras` package.fixReplace `from tensorflow.keras import ...` with `from keras import ...` and `tf.keras.` prefixes with `keras.`. -
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)` if you want to export a SavedModel for use with TFLite/TFServing/etc.
cause Attempting to save a Keras 3 model using `model.save()` with an unsupported file extension (e.g., no extension or `.tf`).fixWhen using `model.save()`, use `.keras` (recommended) or `.h5` extensions. If you specifically need to export to the TensorFlow SavedModel format, use `model.export(filepath)` instead. -
ValueError: A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.operations`).
cause Mixing Keras symbolic tensors (KerasTensors) directly with native TensorFlow operations outside of the `keras.ops` namespace in functional models.fixReplace native TensorFlow operations (`tf.*`) with their equivalent functions from `keras.ops` (e.g., `keras.ops.matmul` instead of `tf.linalg.matmul`). -
ValueError: Only instances of `keras.Layer` can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object ...>
cause This typically occurs in environments where both Keras 3 (standalone) and TensorFlow's bundled Keras 2 (`tf.keras`) are present, leading to a type mismatch when trying to add a layer instantiated from one Keras version into a model from another.fixEnsure consistency in your Keras imports. If using Keras 3, all layers and models should come from the `keras` namespace. Avoid mixing `tf.keras` and `keras` objects. Consider using virtual environments to isolate dependencies.
Warnings
- breaking Keras 3.13.0 and newer versions require Python 3.11 or higher. Using older Python versions will result in installation or runtime errors.
- breaking The `tensorflow.keras` namespace is deprecated for Keras 3.x. All imports should be directly from `keras` (e.g., `import keras`, `from keras import layers`).
- breaking Saving models to the TensorFlow SavedModel format via `model.save()` is no longer supported in Keras 3. Loading TensorFlow SavedModels via `keras.models.load_model()` is also not supported.
- gotcha Keras-nightly builds are development versions and may contain bugs, incomplete features, or unstable APIs. They are not recommended for production environments.
- gotcha The Keras backend (TensorFlow, JAX, or PyTorch) must be configured *before* the first `import keras` statement in your application. Changing the backend after import is not supported.
- gotcha When using the TensorFlow backend with Keras 3 on GPU, `jit_compile` for `Model` is `True` by default. This can cause XLA-related errors if your custom layers or models use TensorFlow operations not supported by XLA.
Install
-
pip install keras-nightly -
pip install tensorflow -
pip install jax[cuda12_pip] -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html -
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
Imports
- keras
from tensorflow import keras
import keras
- layers
from tensorflow.keras import layers
from keras import layers
Quickstart
import os
import keras
import numpy as np
# Configure the Keras backend (e.g., 'tensorflow', 'jax', 'torch')
os.environ["KERAS_BACKEND"] = "tensorflow"
# Load a dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
# Build a simple model
model = keras.Sequential(
[
keras.layers.Input(shape=(28, 28, 1)),
keras.layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
keras.layers.MaxPooling2D(pool_size=(2, 2)),
keras.layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
keras.layers.MaxPooling2D(pool_size=(2, 2)),
keras.layers.Flatten(),
keras.layers.Dropout(0.5),
keras.layers.Dense(10, activation="softmax"),
]
)
# Compile the model
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
# Train the model
model.fit(x_train, y_train, batch_size=128, epochs=5, validation_split=0.1)
# Evaluate the model
loss, accuracy = model.evaluate(x_test, y_test)
print(f"Test accuracy: {accuracy:.4f}")