Skip to content

Switch from Keras to PyTorch - #24

Merged
gjbex merged 37 commits into
masterfrom
development
Aug 14, 2026
Merged

Switch from Keras to PyTorch#24
gjbex merged 37 commits into
masterfrom
development

Conversation

@gjbex

@gjbex gjbex commented Aug 14, 2026

Copy link
Copy Markdown
Owner

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:

  • Introduce PyTorch-based MNIST multilayer perceptron and convolutional neural network notebooks, including data loading, training loops, and model selection workflows.
  • Add dropout-augmented variants of the MLP and CNN models to compare regularization effects using validation metrics.
  • Provide confusion-matrix analysis and sensitivity-to-initialization experiments for the MNIST classifiers.

Enhancements:

  • Replace Keras/TensorFlow code paths in the MNIST notebooks with idiomatic PyTorch implementations, including device management, data loaders, and loss/optimizer setup.
  • Simplify label handling by using integer class indices compatible with CrossEntropyLoss instead of one-hot encodings.
  • Refine notebook narratives to emphasize validation-based model selection and separation of training, validation, and final test evaluation.

Build:

  • Create a new conda environment definition oriented around a CPU-only PyTorch stack and core scientific Python packages, removing the previous GPU-heavy TensorFlow/Keras configuration.

Documentation:

  • Update the source-code README to document PyTorch-based examples and clarify which Keras material is legacy.

gjbex and others added 30 commits January 13, 2026 11:07
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
Copilot AI lite review requested due to automatic review settings August 14, 2026 07:38
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @gjbex, your pull request is larger than the review limit of 150000 diff characters

@gjbex
gjbex merged commit cdecc22 into master Aug 14, 2026
2 checks passed
@sourcery-ai

sourcery-ai Bot commented Aug 14, 2026

Copy link
Copy Markdown

Reviewer's Guide

This 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 selection

sequenceDiagram
    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)
Loading

Flow diagram for the new PyTorch MNIST MLP workflow

flowchart 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]
Loading

File-Level Changes

Change Details Files
Rewrite the 'complete' CNN MNIST notebook from Keras/TensorFlow to a fully PyTorch-based workflow with explicit data pipelines, training loops, model comparison, and test evaluation.
  • Replace keras/tensorflow imports and sklearn preprocessing with torch, torchvision, and native PyTorch transforms and DataLoader usage.
  • Introduce a configuration section with seeds, device selection (CPU/GPU), directories, and experiment length controls.
  • Define MNIST dataset transforms (channels-first, float32, 0–1 scaling) and train/validation/test splits using random_split with a fixed generator seed.
  • Implement reusable training, evaluation, and fit functions using CrossEntropyLoss and SGD with momentum and Nesterov acceleration, returning logits instead of softmax probabilities.
  • Add a simple CNN factory that traces tensor shapes, plus an enhanced CNN with an additional dense layer and optional dropout.
  • Track training and validation metrics, plot histories, and compute development metrics (loss, accuracy) on train and validation loaders.
  • Implement confusion-matrix computation for validation data via torch.bincount and a custom plot_confusion_matrix helper.
  • Add sensitivity-to-initialization analysis that retrains the dropout CNN multiple times with different seeds but fixed data order, plotting run-to-run variation.
  • Enforce a validation-loss-based model selection rule and run a single final test evaluation, saving selected model weights to disk.
hands-on/060_mnist_cnn_complete.ipynb
Rewrite both MNIST MLP notebooks from Keras/TensorFlow/sklearn into PyTorch-based versions, adding training loops, dropout comparison, confusion matrices, and sensitivity analysis.
  • Remove keras, tensorflow, and sklearn preprocessing in favor of torchvision MNIST datasets, v2 transforms, and integer class labels suitable for CrossEntropyLoss.
  • Introduce configuration and reproducibility sections (seeds, device selection, experiment length) and use channels-first images with flattening inside the models.
  • Define MLP architectures using nn.Sequential and nn.Linear, with a classic 784->512->512->10 model and an optional dropout variant.
  • Implement common train_one_epoch, evaluate, and fit functions mirroring the CNN notebook to track loss and accuracy for train and validation sets.
  • Plot training histories, compute development metrics, and save/reload models via state_dict files.
  • Compute and plot confusion matrices on validation data via torch.bincount and a custom plot_confusion_matrix routine.
  • Add sensitivity-to-initial-conditions experiments that retrain the dropout MLP multiple times with different seeds but fixed mini-batch order, summarizing mean and standard deviation of loss and accuracy.
  • Wire up a validation-loss-based model-selection mechanism and perform a single final test evaluation of the selected model, persisting weights to disk.
  • In the 'courageous' notebook, leave key implementation points (MLP factory, training loop, model selection) as TODOs to be filled in by the reader while providing full PyTorch scaffolding around them.
hands-on/040_mnist_mlp_lazy.ipynb
hands-on/040_mnist_mlp_courageous.ipynb
Simplify and refocus environment definitions toward a core, CPU-only PyTorch stack and a lean pinns environment, dropping legacy Keras/TensorFlow-heavy configuration.
  • Replace the top-level environment with a 'machine_learning_with_python_core_cpu' environment that pins Python and PyTorch CPU, torchvision, scikit-learn, hyperopt, bayesian-optimization, LIME, jupyterlab, matplotlib, and supporting packages.
  • Shrink the pinns environment to a minimal PyTorch + SciPy + Jupyterlab + TorchOpt + torchvision stack suitable for PINN examples.
  • Remove the monolithic GPU-heavy environment specification that included TensorFlow, Keras, CUDA toolchains, and a very large dependency graph.
  • Add a dedicated pytorch environment file under source-code/pinns with clear dependencies and a fixed prefix path.
environment.yml
source-code/pinns/environment.yml
Update the top-level source-code README and Jekyll docs configuration to align with the new PyTorch focus and site title.
  • Revise README to describe new PyTorch and PyTorch Lightning directories, clarify available notebooks and example topics, and mark Keras content as legacy material.
  • Adjust the docs/_config.yml Jekyll site configuration to set a descriptive title for the documentation.
  • Tidy wording and list formatting to reflect the current structure of the source-code tree.
source-code/README.md
docs/_config.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread training.toml
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.
Comment thread training.toml
duration_minutes = 30

[[sessions.items]]
subject = "science-learn: clustering"
Comment thread docs/README.md
@@ -37,8 +37,8 @@ Total duration: 4 hours.
| science-learn: clustering | 20 min. |
Comment thread source-code/README.md
Comment on lines +10 to 14
* [`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
Comment on lines +12 to +14
1. `pendulum.ipynb`: Jupyter notebook solving the equation of a pendulum
with damping using a PINN.
equation using a PINN.
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.

3 participants