Skip to main content

Getting Started with MNIST using JAX, NNX, and Grain

Google for DevelopersDecember 3, 20257 min442 views
29 connectionsยท39 entities in this videoโ†’

MNIST Dataset Preparation

  • ๐Ÿ“ฅ Download the MNIST dataset from HuggingFace, storing it in parquet format.
  • ๐Ÿผ Use the pandas library to read the training and testing data files into dataframes.
  • ๐Ÿ–ผ๏ธ Implement a Dataset class using grain to handle data loading, extracting image bytes, and converting them into normalized NumPy arrays.

CNN Model Definition with NNX

  • ๐Ÿ—๏ธ Define a Convolutional Neural Network (CNN) model using NNX, featuring two convolutional layers, average pooling, ReLU activation, and two linear layers.
  • ๐Ÿ” Instantiate the CNN model and use nnx.display to visualize its architecture.
  • ๐Ÿงช Pass a dummy input to the model to verify its structure and output before training.

Optimizer and Metrics Setup

  • โš™๏ธ Configure the AdamW optimizer from Optx, utilizing the nnx.param argument to explicitly target learnable parameters.
  • ๐Ÿ“Š Visualize the optimizer setup using nnx.display.
  • ๐Ÿ“ˆ Define training and evaluation step functions, including a cross-entropy loss function and gradient computation.

Training and Evaluation Loop

  • ๐Ÿš€ Decorate training and evaluation functions with nnx.jit for significant performance improvements.
  • ๐Ÿ” Implement the main training loop, iterating through the dataset, calling the train_step function, and computing metrics at intervals.
  • ๐ŸŽฏ Execute the eval_step on the test dataset to record test metrics and reset them.

Model Prediction and Visualization

  • ๐Ÿ“‰ Switch the model to evaluation mode using model.eval().
  • ๐Ÿ”ฎ Define a predict_step function to obtain predicted labels for test images.
  • ๐Ÿ“Š Plot a grid of test images with their corresponding predicted labels to visually assess model performance.
Knowledge graph39 entities ยท 29 connections

How they connect

An interactive map of every person, idea, and reference from this conversation. Hover to trace connections, click to explore.

Hover ยท drag to explore
39 entities
Chapters3 moments

Key Moments

Transcript27 segments

Full Transcript

Topics12 themes

Whatโ€™s Discussed

MNISTJAXNNXGrainConvolutional Neural Network (CNN)Machine LearningData LoadingOptimizerCross-Entropy LossJIT CompilationModel EvaluationHuggingFace
Smart Objects39 ยท 29 links
Conceptsยท 18
Productsยท 10
Mediasยท 8
Companiesยท 2
Personยท 1