Interactive MNIST playground powered by PyTorch and managed with uv. Notebooks and Python percent scripts stay in sync via Jupytext, and you can either explore the project inside JupyterLab or run the CLI directly with python through uv.
- Python 3.12 or newer
- uv (install via
curl -LsSf https://astral.sh/uv/install.sh | shor follow the official instructions for your OS)
# install all runtime and development dependencies
uv sync
# (optional) register the kernel so it shows up in JupyterLab
uv run python -m ipykernel install --user --name "$(basename "$PWD")"uv sync creates a uv-managed virtual environment, installs the packages pinned in uv.lock, and ensures JupyterLab, Jupytext, torch, and testing tools are available.
There are two supported workflows—pick whichever fits how you want to interact with the MNIST demo.
uv run jupyter lab- Open
main.ipynb,mnist.ipynband select thepython-uppgift01kernel (or whatever name you chose duringipykernel install). - Notebook files (
.ipynb) mirror their.pycounterparts via Jupytext’spy:percentformat, so edits in either place stay synchronized. - Use
uv run jupytext --sync path/to/notebook.ipynbif you need to force a manual resync.
uv run python main.py- The script asks whether to reuse existing weights in
mnist_cnn.ptor retrain the CNN from scratch (default 10 epochs). - On the first run, the MNIST dataset downloads to
data/; subsequent runs reuse the cached files. - After training/loading, you can repeatedly classify test samples, optionally plotting the digit with Matplotlib as you go.
- Pairing rules live in
pyproject.tomlunder[tool.jupytext]withformats = "ipynb,py:percent". - Any
.ipynbsaved in JupyterLab automatically updates its sibling.pyfile, keeping the repo friendly for reviews and diffs.
Unit tests cover the MNIST helper module. Run them with:
uv run pytest- JupyterLab 4 ships with the
jupyter-collaborationextension, so you can share a running Lab URL for real-time editing. - Collaboration state stays local; you only need to commit the synced notebooks/scripts.
- Always invoke tooling through
uv run …to ensure you use the managed environment. - The registered kernel name defaults to your project folder (for example,
python-uppgift01).