1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| import jax
import jax.numpy as jnp
from flax import nnx
from functools import partial
class LeNet(nnx.Module):
def __init__(self, *, rngs: nnx.Rngs):
self.features = nnx.Sequential(
nnx.Conv(
in_features=1,
out_features=6,
kernel_size=(5, 5),
padding=(2, 2),
rngs=rngs,
),
nnx.sigmoid,
partial(nnx.avg_pool, window_shape=(2, 2), strides=(2, 2)),
nnx.Conv(
in_features=6,
out_features=16,
kernel_size=(5, 5),
padding="VALID",
rngs=rngs,
),
nnx.sigmoid,
partial(nnx.avg_pool, window_shape=(2, 2), strides=(2, 2)),
)
self.fc = nnx.Sequential(
lambda x: x.reshape(x.shape[0], -1),
nnx.Linear(in_features=16 * 5 * 5, out_features=120, rngs=rngs),
nnx.sigmoid,
nnx.Linear(in_features=120, out_features=84, rngs=rngs),
nnx.sigmoid,
nnx.Linear(in_features=84, out_features=10, rngs=rngs),
)
def __call__(self, x):
x = self.features(x)
return self.fc(x)
|