
Tech • AI • Robotics
The recent integration of Keras with the modular Flax and NNX system opens new possibilities, combining Keras’s simplicity with the power and flexibility of JAX for advanced variable management and custom training.
The new linkage makes keras.Variable an instance of nnx.Variable, enabling seamless coexistence of state between Keras and NNX. This compatibility makes it easy to mix components from both ecosystems without breaking variable management.
To use this integration, two environment variables must be set before importing Keras: define the backend as JAX and explicitly enable NNX mode. This ensures Keras leverages the advanced capabilities of NNX and Flax.
The system allows verification that Keras variables are properly recognized and tracked by NNX: they appear in the managed variable list, have a trace state for just-in-time compilation, and can be accessed directly via NNX, improving control during training.
NNX provides a modular approach using standard Python classes, bringing native modularity into Keras through integration. For example, an NNX module can include linear layers, custom Keras variables, and manage their interaction within the model’s call method.
The first mode follows the classic Keras approach: model.compile and model.fit work as usual, with NNX and JAX operating behind the scenes to optimize performance, without requiring code changes.
The second mode enables custom training loops by treating a Keras model as an nnx.Module. This gives access to the broader JAX ecosystem, including Optax for optimizers, and nnx.jit and nnx.grad for efficient acceleration and differentiation.
Using the nnx.jit decorator improves performance by compiling training functions on the fly, providing noticeable speed gains even for Keras models integrated with NNX.
This synergy allows users to start easily with familiar Keras APIs while progressively accessing advanced research and optimization features from JAX, creating a unified framework for all skill levels.
Integrated Keras models can fully leverage JAX tools like Optax, along with a wide range of statistical and differential functions, greatly extending possibilities beyond the traditional Keras framework.
A comprehensive guide and code examples are available on the keras.io website, offering a clear starting point to learn and experiment with this integration.
The integration of Keras with Flax and NNX transforms state management and training possibilities, combining ease of use with powerful customization in the JAX ecosystem. This bridge enables more flexible and high-performance machine learning development.
Explain this