A flexible trainer interface for Jax and Haiku.
Project description
Bax
Bax, short for "boilerplate jax", is a small library that provides a flexible trainer interface for Jax.
Bax is rather strongly opinionated in a few ways. First, it is designed for use with the
Haiku neural network library and is not
compatible with e.g. Flax. Second, Bax assumes that
data will be provided as a tf.data.Dataset
. The goal of this library is not to be
widely compatible and high-level (like Elegy).
If you are okay with making the above assumptions, then Bax will hopefully make your life much easier by implementing the boilerplate code involved in neural network training loops.
Please note that this library has not yet been extensively tested.
Installation
You can install Bax via pip:
pip install bax
Usage
Below are some simple examples that illustrate how to use Bax.
MNIST Classification
import optax
import tensorflow_datasets as tfds
import haiku as hk
import jax.numpy as jnp
import jax
from bax.trainer import Trainer
# Use TensorFlow Datasets to get our MNIST data.
train_ds = tfds.load("mnist", split="train").batch(32, drop_remainder=True)
test_ds = tfds.load("mnist", split="test").batch(32, drop_remainder=True)
# The loss function that we want to minimize.
def loss_fn(step, is_training, batch):
model = hk.Sequential([hk.Flatten(), hk.nets.MLP([128, 128, 10])])
preds = model(batch["image"] / 255.0)
labels = jax.nn.one_hot(batch["label"], 10)
loss = jnp.mean(optax.softmax_cross_entropy(preds, labels))
accuracy = jnp.mean(jnp.argmax(preds, axis=-1) == batch["label"])
# The first returned value is the loss, which is what will be minimized by the
# trainer. The second value is a dictionary that can contain other metrics you
# might be interested in (or, it can just be empty).
return loss, {"accuracy": accuracy}
trainer = Trainer(loss=loss_fn, optimizer=optax.adam(0.001))
# Run the training loop. Metrics will be printed out each time the validation
# dataset is evaluated (in this case, every 1000 steps).
trainer.fit(train_ds, steps=10000, val_dataset=test_ds, validation_freq=1000)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bax-0.1.11.tar.gz
.
File metadata
- Download URL: bax-0.1.11.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd33e206b3dd56ccef5a7470b5c439526d4c39a50dc3bf99756aa6a24443739d |
|
MD5 | 65dd312d5faab8d37c48147c48f32c79 |
|
BLAKE2b-256 | 1a87ee457af1b54297fdc6d9fa887a7f43af9e579d9297725ecc91bd62348a6a |
File details
Details for the file bax-0.1.11-py3-none-any.whl
.
File metadata
- Download URL: bax-0.1.11-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89f79823c8fc69fe7a9442deeae1c1b60157f2ed950fb3dce052c8798f87e523 |
|
MD5 | 94f72bca2ae80dad4763c4384e4ca236 |
|
BLAKE2b-256 | d3a35186d4ee97081f3ec00bba8d95eb6f3975645a0bf00d05bc58927b28b8af |