Created
December 13, 2023 21:06
-
-
Save yberreby/c0540e868633a9d254a7fc57fa07359c to your computer and use it in GitHub Desktop.
Minimal MLP in JAX - excerpt from the "Working with Pytrees" section of the JAX manual
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import jax | |
import jax.numpy as jnp | |
import matplotlib.pyplot as plt | |
def init_mlp_params(layer_widths): | |
params = [] | |
for n_in, n_out in zip(layer_widths[:-1], layer_widths[1:]): | |
params.append( | |
dict(weights=np.random.normal(size=(n_in, n_out)) * np.sqrt(2/n_in), | |
biases=np.ones(shape=(n_out,)) | |
) | |
) | |
return params | |
params = init_mlp_params([1, 128, 128, 1]) | |
def forward(params, x): | |
*hidden, last = params | |
for layer in hidden: | |
x = jax.nn.relu(x @ layer['weights'] + layer['biases']) | |
return x @ last['weights'] + last['biases'] | |
def loss_fn(params, x, y): | |
return jnp.mean((forward(params, x) - y) ** 2) | |
LEARNING_RATE = 0.0001 | |
@jax.jit | |
def update(params, x, y): | |
grads = jax.grad(loss_fn)(params, x, y) | |
# Note that `grads` is a pytree with the same structure as `params`. | |
# `jax.grad` is one of the many JAX functions that has | |
# built-in support for pytrees. | |
# This is handy, because we can apply the SGD update using tree utils: | |
return jax.tree_map( | |
lambda p, g: p - LEARNING_RATE * g, params, grads | |
) | |
xs = np.random.normal(size=(128, 1)) | |
ys = xs ** 2 | |
for _ in range(1000): | |
params = update(params, xs, ys) | |
plt.scatter(xs, ys) | |
plt.scatter(xs, forward(params, xs), label='Model prediction') | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment