【从0开始】使用Flax NNX API 构建简单神经网络并训练
与 Linen API 不同,NNX 使用起来对初学者更加简单,跟 PyTorch 的体验更加接近。
任务
使用MLP拟合简单函数: $$ y=2x^2+1 $$
代码
from typing import Generator
import jax.numpy as jnp
import jax.random as jrm
import optax as ox
from flax import nnx
from jax import Array
class Network(nnx.Module):
"""def a simple MLP"""
def __init__(self, in_dim: int, out_dim: int, rng:., hidden_dim: int):
super().__init__()
self.linear1 = nnx.(,, rngs=)
self.linear2 = nnx.(,, rngs=)
self.linear3 = nnx.(,, rngs=)
def __call__(self, x) -> Array:
x = self.()
x = nnx.()
x = self.()
x = nnx.()
x = self.()
return x
def make_dataset(
X:, Y:, batch: int, key
) ->[[,,], None, None]:
"dataset sample function"
combined = jnp.((,), axis=1)[..., None]
while True:
key, subkey = jrm.()
selected = jrm.(,, shape=(,))
yield[:, 0],[:, 1], key
def loss_fn(model:, batch):
x, y = batch
predicted =()
return ox.(,).()
@nnx.jit
def train_step(model:, optimizer:., batch):
loss, grads = nnx.()(,)
optimizer.()
return loss
# hyper parameter
seed = 0
batch = 32
# make dataset
X = jnp.(0, 10, 0.005)
Y = 2 * X**2 + 1.0
# build model & optimizer
model =(1, 1, hidden_dim=20, rng=.())
optimizer = nnx.(,.(0.001))
# train
key = jrm.()
for i, (x, y, _) in enumerate((,,,)):
loss =(,, (,))
print(,)
if i >= 6000:
break
依赖如下
$ uv pip list
Package Version
-------------------- --------
absl-py 2.4.0
aiofiles 25.1.0
box2d 2.3.10
cloudpickle 3.1.2
colorama 0.4.6
docstring-parser 0.17.0
etils 1.13.0
farama-notifications 0.0.4
flax 0.10.7
fsspec 2026.2.0
gymnasium 1.2.3
humanize 4.15.0
importlib-resources 6.5.2
jax 0.7.2
jaxlib 0.7.2
markdown-it-py 4.0.0
mdurl 0.1.2
ml-dtypes 0.5.1
msgpack 1.1.2
nest-asyncio 1.6.0
numpy 2.2.2
opt-einsum 3.4.0
optax 0.2.7
orbax-checkpoint 0.11.32
packaging 26.0
protobuf 6.33.5
psutil 7.2.2
pygame 2.6.1
pygments 2.19.2
pyyaml 6.0.3
rich 14.3.3
scipy 1.15.1
simplejson 3.20.2
swig 4.4.1
tensorboardx 2.6.4
tensorstore 0.1.81
tqdm 4.67.3
treescope 0.1.10
typeguard 4.5.1
typing-extensions 4.15.0
tyro 1.0.7
zipp 3.23.0