Notebooks
G
Google
Loading Datasets

Loading Datasets

jaxguidesdata_preprocessingdocsflax

Loading datasets

Open in Colab

A neural net written in Jax+Flax expects its input data as jax.numpy array instances. Therefore, loading a dataset from any source is as simple as converting it to jax.numpy types and reshaping it to the appropriate dimensions for your network.

As an example, this guide demonstrates how to import MNIST using the APIs from Torchvision, Tensorflow, and Hugging Face. We'll load the whole dataset into memory. For datasets that don't fit into memory the process is analogous but should be done in a batchwise fashion.

The MNIST dataset consists of greyscale images of 28x28 pixels of handwritten digits, and has a designated 60k/10k train/test split. The task is to predict the correct class (digit 0, ..., 9) of each image.

Assuming a CNN-based classifier, the input data should have shape (B, 28, 28, 1), where the trailing singleton dimension denotes the greyscale image channel.

The labels are simply the integer denoting the digit corresponding to the image. Labels should therefore have shape (B,), to enable loss computation with optax.softmax_cross_entropy_with_integer_labels.

[2]

Loading from torchvision.datasets

[3]
[4]
[5]
(60000, 28, 28, 1) float32
(60000,) int16
(10000, 28, 28, 1) float32
(10000,) int16

Loading from tensorflow_datasets

[5]
[6]
[7]
(60000, 28, 28, 1) float32
(60000,) int16
(10000, 28, 28, 1) float32
(10000,) int16

Loading from 🤗 Hugging Face datasets

[ ]
[9]
[10]
(60000, 28, 28, 1) float32
(60000,) int16
(10000, 28, 28, 1) float32
(10000,) int16