examples/mnist directory shows how to use ggml for both training and inference on the MNIST handwritten digit dataset. Two model architectures are provided: a fully connected network and a convolutional network.
Model architectures
Fully connected
Two dense layers with 784 → 500 → 10 units. Trained with PyTorch and exported to GGUF, or trained directly in ggml.
Convolutional (CNN)
Two convolutional layers followed by a dense output layer. Trained with TensorFlow and exported to GGUF, or trained directly in ggml.
Model structure
Themnist_model struct holds all weights and contexts for both architectures:
Getting the data
The dataset is downloaded automatically when you run the Python training scripts. You can also download it manually from HuggingFace.Downloads from the original Yann LeCun website are frequently throttled. Use HuggingFace instead.
Fully connected network
Train with PyTorch
Train a fully connected model in PyTorch and save it as a GGUF file:Evaluate with ggml
Train with ggml
You can also train the fully connected model directly in ggml:mnist-eval as shown above.
Convolutional network
Train with TensorFlow
Evaluate with ggml
Train with ggml
Hardware acceleration
Bothmnist-train and mnist-eval are backend-agnostic. You can select a specific backend by appending its name:
Batch configuration
Gradient accumulation is used during training via separate logical and physical batch sizes:MNIST_NBATCH_LOGICAL % MNIST_NBATCH_PHYSICAL == 0.
Web demo
The evaluation code can be compiled to WebAssembly using Emscripten:Neural networks are susceptible to distributional shift. Digits that look significantly different from the MNIST training data (e.g. not centred) may be misclassified.
