Add PyTorch track to the lesson - #639
Conversation
Implemented in @mimer-ai/deep-learning-intro by the following authors: - @ashwinvis Ashwin - @otaub Oskar - @ffrancesco94 Francesco - @marlon-tobaben Marlon - @lodo1995 Lodovico Initial version. More changes required as noted in #631
Thank you!Thank you for your pull request 😃 🤖 This automated message can help you check the rendered files in your submission for clarity. If you have any questions, please feel free to open an issue in {sandpaper}. If you have files that automatically render output (e.g. R Markdown), then you should check for the following:
Rendered Changes🔍 Inspect the changes: https://github.com/carpentries-lab/deep-learning-intro/compare/md-outputs..md-outputs-PR-639 The following changes were observed in the rendered markdown documents: What does this mean?If you have source files that require output and figures to be generated (e.g. R Markdown), then it is important to make sure the generated figures and output are reproducible. This output provides a way for you to inspect the output in a diff-friendly manner so that it's easy to see the changes that occur due to new software versions or randomisation. ⏱️ Updated at 2026-03-10 14:48:03 +0000 |
carschno
left a comment
There was a problem hiding this comment.
I had a look at some parts of the draft. It looks very good, thanks a lot!
I have added a few minor comments and left suggestions when I spotted typos etc.
|
|
||
|  | ||
| ](fig/01_nn_abstraction_layers.png){alt='Example of two different neural networks and how each layer process different abstract representations of input data'} |
There was a problem hiding this comment.
| ](fig/01_nn_abstraction_layers.png){alt='Example of two different neural networks and how each layer process different abstract representations of input data'} | |
| ](fig/01_nn_abstraction_layers.png){alt='Example of two different neural networks and how each layer processes different abstract representations of the input data'} |
|
|
||
| ```shell | ||
| python3 -m pip install jupyter seaborn scikit-learn pandas tensorflow pydot | ||
| python3 -m pip install jupyter seaborn scikit-learn pandas tqdm torchinfo torchmetrics torch torchvision |
There was a problem hiding this comment.
Not sure all of these packages need to be installed explicitly, e.g. tqdm and torch should be indirect dependencies. Only pointing it out because it might cause resolution difficulties in the future.
| because the accuracy of a model depends on the data used to train and test it. | ||
| ::: | ||
|
|
||
| ### Scale the input features |
There was a problem hiding this comment.
It might make sense to separate the scaling addition for quicker merging.
|
|
||
| <!-- end-tab --><!-- end-tab --> | ||
|
|
||
| ###### PyTorch |
There was a problem hiding this comment.
Maybe this would be a good occasion to point out the advantages of Keras? Ie. higher-level abstraction allowing easier usage for standard use cases.
| For this episode it is useful if everyone gets the same results from their training. | ||
| Keras uses a random number generator at certain points during its execution. | ||
| Therefore we will need to set two random seeds, one for numpy and one for tensorflow: | ||
| Keras and PyTorch uses a random number generator at certain points during its execution. |
There was a problem hiding this comment.
| Keras and PyTorch uses a random number generator at certain points during its execution. | |
| Keras and PyTorch use a random number generator at certain points during its execution. |
| In Pytorch, the architecture of a neural network is defined in a class that | ||
| inherits from `torch.nn.Module`. The network itself is created by stacking | ||
| layers and linking them together. In this episode, we will only use one type of |
There was a problem hiding this comment.
Perhaps change the perspective: start from the concept (layer/modules) that are implemented as Module classes, along these lines:
| In Pytorch, the architecture of a neural network is defined in a class that | |
| inherits from `torch.nn.Module`. The network itself is created by stacking | |
| layers and linking them together. In this episode, we will only use one type of | |
| In Pytorch, a neural network is defined as a set of neural network layer modules thatin a class that are stacked and linked together. These modules are implemented as classes inheriting from `torch.nn.Module`. | |
| In this episode, we will only use one type of |
| class PenguinModel(torch.nn.Module): | ||
| def __init__(self, input_shape): | ||
| super().__init__() | ||
| self.hidden_layer = torch.nn.Linear(input_shape, 10) | ||
| self.output_layer = torch.nn.Linear(10, 3) | ||
|
|
||
| def forward(self, x): | ||
| x = self.hidden_layer(x) | ||
| x = torch.nn.functional.relu(x) | ||
| x = self.output_layer(x) | ||
| x = torch.nn.functional.softmax(x, dim=1) | ||
| return x |
There was a problem hiding this comment.
The example might raise the question why some layers are defined on class level (self.hidden_layer and self.output_layer), while others are defined in the forward() method.
Perhaps briefly indicate the difference (activation layers). I think this is a key difference to Keras which by default abstracts these things away.
|
|
||
| ###### PyTorch | ||
|
|
||
| To run inference (i.e. make predictions) with Pytorch, we need to set the model in "evaluation mode". |
There was a problem hiding this comment.
| To run inference (i.e. make predictions) with Pytorch, we need to set the model in "evaluation mode". | |
| To run inference (i.e. make predictions) with Pytorch, we need to set the model in "evaluation mode" to avoid that its parameters are changed. |
| [Keras can also use either PyTorch or JAX as a backend](https://keras.io/getting_started/#configuring-your-backend). | ||
|
|
||
| Note for MacOS users: there is a package `tensorflow-metal` which accelerates the training of machine learning models with TensorFlow on a recent Mac with a Silicon chip (M1/M2/M3). | ||
| However, the installation is currently broken in the most recent version (as of January 2025), see the [developer forum](https://developer.apple.com/forums/thread/772147). |
There was a problem hiding this comment.
This presumably needs to be updated (out of scope for this PR).
| Alternatively you can use [Google colab](https://colab.research.google.com/). If you open a jupyter notebook here, the required packages are already pre-installed. Note that google colab uses jupyter notebook instead of Jupyter Lab. | ||
| [](https://mybinder.org/v2/gh/mimer-ai/deep-learning-intro/scaffolds) | ||
|
|
||
| If a local installation does not work for you, it is also possible to run this lesson in [Binder Hub](https://mybinder.org/v2/gh/mimer-ai/deep-learning-intro/scaffolds). This should give you an environment with all the required software and data to run this lesson, nothing which is saved will be stored, please copy any files you want to keep. Note that if you are the first person to launch this in the last few days it can take several minutes to startup. The second person who loads it should find it loads in under a minute. Instructors who intend to use this option should start it themselves shortly before the workshop begins. |
There was a problem hiding this comment.
It might be good to extract the cloud solutions section to a separate PR, too, to facilitate quicker merging.
There was a problem hiding this comment.
That's a good idea. Will do so.
Implemented in @mimer-ai/deep-learning-intro by the following authors:
Initial version. More changes required as noted in #631