|
| 1 | +# Copyright 2024 The Flax Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# %% |
| 16 | +import jax |
| 17 | +import jax.numpy as jnp |
| 18 | +import matplotlib.pyplot as plt |
| 19 | +import numpy as np |
| 20 | +import optax |
| 21 | + |
| 22 | +from flax import nnx, struct |
| 23 | + |
| 24 | +X = np.linspace(0, 1, 100)[:, None] |
| 25 | +Y = 0.8 * X**2 + 0.1 + np.random.normal(0, 0.1, size=X.shape) |
| 26 | + |
| 27 | + |
| 28 | +def dataset(batch_size): |
| 29 | + while True: |
| 30 | + idx = np.random.choice(len(X), size=batch_size) |
| 31 | + yield X[idx], Y[idx] |
| 32 | + |
| 33 | +class Linear(nnx.Module): |
| 34 | + def __init__(self, din: int, dout: int, *, rngs: nnx.Rngs): |
| 35 | + self.w = jax.random.normal(rngs.params(), (din, dout)) |
| 36 | + self.b = jnp.zeros((dout,)) |
| 37 | + |
| 38 | + def __call__(self, x): |
| 39 | + return x @ self.w + self.b |
| 40 | + |
| 41 | + |
| 42 | +class MLP(nnx.Module): |
| 43 | + def __init__(self, din, dhidden, dout, *, rngs: nnx.Rngs): |
| 44 | + self.count = jnp.array(0) |
| 45 | + self.linear1 = Linear(din, dhidden, rngs=rngs) |
| 46 | + self.linear2 = Linear(dhidden, dout, rngs=rngs) |
| 47 | + |
| 48 | + def __call__(self, x): |
| 49 | + self.count += 1 |
| 50 | + return self.linear2(nnx.relu(self.linear1(x))) |
| 51 | + |
| 52 | +def is_param(path, value): |
| 53 | + key = path[-1] |
| 54 | + return key == 'w' or key == 'b' |
| 55 | + |
| 56 | +model = MLP(din=1, dhidden=32, dout=1, rngs=nnx.Rngs(0)) |
| 57 | +tx = optax.sgd(1e-3) |
| 58 | +optimizer = nnx.Optimizer(model, tx, wrt=is_param) |
| 59 | + |
| 60 | + |
| 61 | +@nnx.jit |
| 62 | +def train_step(model: MLP, optimizer: nnx.Optimizer, batch): |
| 63 | + x, y = batch |
| 64 | + |
| 65 | + def loss_fn(model: MLP): |
| 66 | + y_pred = model(x) |
| 67 | + return jnp.mean((y - y_pred) ** 2) |
| 68 | + |
| 69 | + diff_state = nnx.DiffState(0, is_param) |
| 70 | + grads: nnx.State = nnx.grad(loss_fn, argnums=diff_state)(model) |
| 71 | + optimizer.update(grads) |
| 72 | + |
| 73 | + |
| 74 | +@nnx.jit |
| 75 | +def test_step(model: MLP, batch): |
| 76 | + x, y = batch |
| 77 | + y_pred = model(x) |
| 78 | + loss = jnp.mean((y - y_pred) ** 2) |
| 79 | + return {'loss': loss} |
| 80 | + |
| 81 | + |
| 82 | +total_steps = 10_000 |
| 83 | +for step, batch in enumerate(dataset(32)): |
| 84 | + train_step(model, optimizer, batch) |
| 85 | + |
| 86 | + if step % 1000 == 0: |
| 87 | + logs = test_step(model, (X, Y)) |
| 88 | + print(f"step: {step}, loss: {logs['loss']}") |
| 89 | + |
| 90 | + if step >= total_steps - 1: |
| 91 | + break |
| 92 | + |
| 93 | +print('times called:', model.count) |
| 94 | + |
| 95 | +y_pred = model(X) |
| 96 | + |
| 97 | +plt.scatter(X, Y, color='blue') |
| 98 | +plt.plot(X, y_pred, color='black') |
| 99 | +plt.show() |
0 commit comments