Skip to content

Conversation

@lovelyoverflow
Copy link

Summary

This PR adds an inference script (infer.py) to the MNIST example, demonstrating how to load and use trained models for predictions.

Currently, main.py only shows training. This addition completes the tutorial by showing the inference workflow.

What's New

New file: mnist/infer.py

  • Load trained model weights from .safetensors file
  • Predict on random test samples with confidence scores
  • ASCII art visualization - displays digits in terminal (no matplotlib required)
  • Interactive mode to test specific samples
  • Supports both MNIST and Fashion-MNIST

Modified: mnist/main.py

  • Add --save-model flag to save trained weights (default: model.safetensors)
  • Backward compatible - existing usage unchanged

Modified: mnist/README.md

  • Add inference documentation with usage examples

Usage

# Train and save model
python main.py --gpu

# Run inference on 5 random samples
python infer.py --num-samples 5

# Interactive mode
python infer.py --interactive

Example Output

✓ Sample 1234:
  True:       8
  Predicted:  8
  Confidence: 89.1%
  Top 3:
    8: 89.1%
    3: 5.2%
    9: 2.1%

Why This Is Useful

  1. Completes the tutorial - Shows full ML workflow (train + inference)
  2. Beginner-friendly - Answers common question: "How do I use the trained model?"
  3. No extra dependencies - ASCII visualization works in any terminal
  4. Educational - Demonstrates MLX model loading patterns

Testing

Tested on macOS with Apple Silicon:

  • ✅ Model training and saving
  • ✅ Model loading and inference
  • ✅ Both MNIST and Fashion-MNIST datasets
  • ✅ Interactive mode
  • ✅ ASCII visualization

All changes are backward compatible.

This adds a CLI-based inference script to demonstrate model loading
and prediction after training.

- New file: mnist/infer.py with ASCII visualization
- Modified: mnist/main.py to save trained weights
- Modified: mnist/README.md with inference documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant