flax
Flax is a neural network library for JAX that is designed for flexibility.
Flax is Google's Python library for building and training neural networks on top of JAX, letting researchers define models as plain Python classes that run fast on CPUs and AI accelerators.
Flax is a Python library for building and training neural networks, developed by Google. It sits on top of JAX, which is Google's framework for fast numerical computation that runs well on both regular CPUs and specialized AI accelerators. If you have heard of PyTorch or TensorFlow as ways to build AI models, Flax is Google's answer to that same need, but built specifically around JAX.
The library gives you the building blocks to describe the structure of a neural network: layers that transform numbers, normalization steps that keep training stable, attention mechanisms used in modern language models, dropout for preventing overfitting, and more. You assemble these into a model by writing plain Python classes, which makes it easier to read, debug, and modify compared to older frameworks that required more abstract patterns.
Flax has gone through two major API designs. The older one, called Linen, was released in 2020 and is still documented separately. The current one, called NNX and released in 2024, lets you work with models as regular Python objects that can be inspected and changed directly, which simplifies common research tasks.
The library is aimed at researchers who want to experiment freely. It is installed with a single pip command and requires Python 3.8 or later. It comes with example code for tasks like classifying handwritten digits and running inference on the Gemma language model. Flax is maintained by a team at Google DeepMind but is not an official Google product.
Where it fits
- Build and train a custom neural network for image classification using plain Python classes with Flax NNX.
- Run inference on the Gemma language model using Flax's included example code.
- Experiment with attention mechanisms and normalization layers for a research model using Flax's composable building blocks.
- Prototype a new AI architecture in Flax that runs fast on GPU or TPU accelerators via JAX.