Flax is a high-performance neural network library for JAX, designed for flexibility and ease of use. It provides building blocks for defining models, handling parameters, and managing training state within the JAX ecosystem. As of my last check, the current version is 0.12.6. Its release cadence is closely tied to JAX updates and major developments in the JAX ecosystem, with frequent minor and patch releases.
pip install flax jax[cpu]Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a basic neural network module using `flax.linen`, initialize its parameters with a JAX PRNG key, and perform a forward pass. It also shows how to pass explicit PRNG keys for stochastic operations.
Migrate to `optax` for all optimizer definitions and applications. `flax.training.train_state` is designed to work seamlessly with `optax` optimizers.
Always assign the result of any state-modifying function (e.g., `optimizer.apply_gradients` or `state.apply_gradients`) back to your state variable: `state = state.apply_gradients(grads)`.
For operations requiring randomness within `nn.Module` (e.g., dropout), pass a dictionary of PRNG keys to `model.apply()` using the `rngs` argument (e.g., `model.apply(..., rngs={'dropout': dropout_key})`). Use `jax.random.split` to generate new sub-keys for subsequent random operations.Ensure that the input shape used for `model.init()` accurately reflects the expected input shape during the model's lifetime. For dynamic shapes, consider using `jax.ShapeDtypeStruct` or designing modules to be robust to varying batch sizes or sequence lengths.