Switch from Keras to PyTorch - #24
Conversation
Add PyTorch MNIST example
Co-authored-by: gjbex <4801336+gjbex@users.noreply.github.com>
…patibility Co-authored-by: gjbex <4801336+gjbex@users.noreply.github.com>
Fix typo and replace torch.accelerator with standard CUDA detection
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
There was a problem hiding this comment.
Sorry @gjbex, your pull request is larger than the review limit of 150000 diff characters
Reviewer's GuideThis pull request replaces Keras/TensorFlow-based MNIST notebooks with PyTorch implementations and simplifies the environment to a CPU-only PyTorch core, introducing new training loops, data pipelines, model selection, and evaluation logic while removing legacy keras-specific code and dependencies. Sequence diagram for PyTorch MNIST MLP training and model selectionsequenceDiagram
actor User
participant Notebook
participant TrainLoader
participant ValidationLoader
participant Model_classic
participant Model_dropout
participant LossFunction
participant Optimizer
participant FileSystem
User->>Notebook: run_cells()
Notebook->>Notebook: transform = v2.Compose(...)
Notebook->>Notebook: full_train_dataset = MNIST(...)
Notebook->>Notebook: train_dataset, validation_dataset = random_split(...)
Notebook->>TrainLoader: make_train_loader(DATA_ORDER_SEED)
Notebook->>ValidationLoader: DataLoader(validation_dataset,...)
Notebook->>Model_classic: model = make_mlp()
Notebook->>Optimizer: optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
Notebook->>Notebook: model_history = fit(model, TrainLoader, ValidationLoader, LossFunction, Optimizer,...)
Notebook->>Notebook: model_development_results = evaluate_development_sets(model,...)
Notebook->>Model_dropout: dropout_model = make_mlp(dropout_probability=0.2)
Notebook->>Optimizer: dropout_optimizer = torch.optim.SGD(dropout_model.parameters(), lr=0.01)
Notebook->>TrainLoader: dropout_train_loader = make_train_loader(DATA_ORDER_SEED)
Notebook->>Notebook: dropout_model_history = fit(dropout_model, dropout_train_loader, ValidationLoader, LossFunction, dropout_optimizer,...)
Notebook->>Notebook: dropout_model_development_results = evaluate_development_sets(dropout_model,...)
Notebook->>Notebook: selected_model_name = min(candidate_results, key=...)
Notebook->>Notebook: selected_model = candidate_models[selected_model_name]
Notebook->>FileSystem: torch.save(selected_model.state_dict(), "models/mnist_mlp_selected.pt")
Notebook->>Notebook: final_test_metrics = evaluate(selected_model, test_loader, LossFunction,...)
Notebook-->>User: print(selected_model_name, final_test_metrics)
Flow diagram for the new PyTorch MNIST MLP workflowflowchart TD
A[Start_notebook] --> B[Prepare_data_transforms]
B --> C[Load_MNIST_datasets]
C --> D[Split_train_validation]
D --> E[Create_DataLoaders]
E --> F[Build_classic_model_using_make_mlp]
E --> G[Build_dropout_model_using_make_mlp]
F --> H[Train_classic_model_with_fit]
G --> I[Train_dropout_model_with_fit]
H --> J[Evaluate_classic_with_evaluate_development_sets]
I --> K[Evaluate_dropout_with_evaluate_development_sets]
J --> L[Select_model_by_validation_loss]
K --> L
L --> M[Save_selected_model_with_torch_save]
M --> N[Final_test_evaluation_with_evaluate]
N --> O[End]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Pull request overview
This PR reshapes the training materials to make PyTorch the primary deep-learning framework (while retaining legacy Keras content as optional), and updates accompanying documentation, metadata, and Conda environments to reflect that split.
Changes:
- Added new training metadata (
training.toml) and a detailed redesign plan (TODO.md). - Introduced/expanded PyTorch + PyTorch Lightning source-code materials and refreshed hands-on notebooks to use PyTorch for MNIST.
- Split Conda environments into core CPU/GPU (PyTorch) and legacy Keras CPU/GPU variants.
Reviewed changes
Copilot reviewed 39 out of 56 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| training.toml | Adds training metadata (outcomes, schedule, requirements). |
| TODO.md | Captures the redesign plan and framework responsibilities (scikit-learn vs PyTorch vs Lightning). |
| source-code/README.md | Updates top-level source-code index to include PyTorch/Lightning and mark Keras as legacy. |
| source-code/pytorch/README.md | Adds documentation for the PyTorch notebook set. |
| source-code/pytorch/pytorch_linux64_conda_specs.txt | Adds a pinned Linux conda spec list for the PyTorch environment. |
| source-code/pytorch/environment.yml | Adds a minimal conda environment file for PyTorch notebooks. |
| source-code/pytorch/.gitignore | Ignores local datasets and model artifacts for PyTorch notebooks. |
| source-code/pytorch-lightning/README.md | Adds documentation for Lightning notebook(s). |
| source-code/pytorch-lightning/.gitignore | Ignores Lightning logs/checkpoints and data. |
| source-code/pinns/README.md | Extends PINNs materials list (adds pendulum notebook reference). |
| source-code/pinns/environment.yml | Simplifies the PINNs environment definition. |
| source-code/keras/README.md | Adjusts the Keras index (legacy positioning). |
| source-code/data/three.txt | Adds saved sample data (MNIST digit image text dump). |
| source-code/data/five.txt | Adds saved sample data (MNIST digit image text dump). |
| README.md | Updates repo-level environment pointers (core vs GPU vs legacy Keras). |
| hands-on/README.md | Updates hands-on overview and clarifies PyTorch core vs optional legacy Keras notebooks. |
| hands-on/optional/README.md | Adds README for optional legacy Keras exercises. |
| hands-on/optional/080_imdb_rnn_lazy.ipynb | Adds legacy Keras IMDB RNN (lazy) notebook under optional. |
| hands-on/optional/080_imdb_rnn_courageous.ipynb | Adds legacy Keras IMDB RNN (courageous) notebook under optional. |
| hands-on/optional/080_imdb_rnn_complete.ipynb | Adds legacy Keras IMDB RNN (complete) notebook under optional. |
| hands-on/optional/070_imdb_data_exploration_lazy.ipynb | Adds legacy Keras IMDB exploration (lazy) notebook under optional. |
| hands-on/data/three.txt | Adds hands-on copy of saved sample data (digit image text dump). |
| hands-on/050_convolution_lazy.ipynb | Updates convolution notebook to use local saved samples instead of Keras MNIST loader. |
| hands-on/020_mnist_data_exploration_lazy.ipynb | Rewrites MNIST exploration notebook to use torchvision/PyTorch datasets and transforms. |
| hands-on/020_mnist_data_exploration_courageous.ipynb | Adds PyTorch-based “courageous” version with TODO placeholders. |
| environment.yml | Replaces large TensorFlow/Keras environment with a smaller core CPU PyTorch-focused environment. |
| environment_keras.yml | Adds a dedicated CPU environment for legacy Keras notebooks. |
| environment_keras_gpu.yml | Adds a dedicated GPU environment for legacy Keras notebooks. |
| environment_gpu.yml | Adds a GPU-enabled core environment for the instructor/validated CUDA config. |
| docs/README.md | Updates the website docs to match the revised scope (PyTorch core, PINNs, HPO). |
| docs/_config.yml | Adds site title to the Jekyll config. |
Suppressed comments (2)
source-code/README.md:18
- Typos in the scikit-learn bullet (missing space in "forsupervised", "learnign", and "high-demensional").
source-code/README.md:19 - Markdown link syntax is broken here ("
}" instead of "]"), so the link will not render correctly.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| intermediate_percent = 35 | ||
| advanced_percent = 50 | ||
| description = """ | ||
| For participants who already have basic Python programming experience, the material in this training is approximately These percentages describe the level of the machine learning topics covered in the training, not the required entry level in Python itself. |
| duration_minutes = 30 | ||
|
|
||
| [[sessions.items]] | ||
| subject = "science-learn: clustering" |
| @@ -37,8 +37,8 @@ Total duration: 4 hours. | |||
| | science-learn: clustering | 20 min. | | |||
| * [`pytorch-lightning/`](pytorch-lightning/): illustration of using PyTorch | ||
| Lightning for machine learning. | ||
| * [`parameter-optimization`](parameter-optimization): example of parameter | ||
| optimization kusing hyperopt, although the examples do not optimize | ||
| hyperparameters in machine learning, that would be very similar. |
| @@ -0,0 +1,14 @@ | |||
| # Pythorch Ligntning | |||
| 1. `pendulum.ipynb`: Jupyter notebook solving the equation of a pendulum | ||
| with damping using a PINN. | ||
| equation using a PINN. |
Summary by Sourcery
Port MNIST MLP and CNN teaching notebooks and environment from Keras/TensorFlow to a PyTorch-based stack, updating code, dependencies, and docs while preserving the pedagogical workflow.
New Features:
Enhancements:
Build:
Documentation: