This repo helps you to guide through the exercise to train deep neural networks in Python. Here, we will use the PyTorch framework inside the denspp.offline framework. You find further information here. Software for RTG Specialized Course - Deep Learning
To use this software, we recommend to install the following tools:
- uv package manager (Link for installation)
- VScode (Link for Downloading and Installation)
- Git (Link for Downloading and Installation)
To download the code open a terminal/cmd and navigate to the target directory for example:
cd C:\Users\Student\Documents\GitHub
Then clone the repository (e.g., download a copy of the code):
git clone https://github.com/AErbsloeh/rtg_deeplearning
This will automatically create a subfolder in your directory named rtg_deeplearning. Go to your code editor of choice (e.g. VScode) and navigate to this folder.
To use the code, you have to initialise the project. Please run the following steps:
- Please enter
uv syncinto the terminal in order to install all packages. - After it, please run the
init_project.pyfile.
In order to train a custom-defined model, you need a Python class to run. Here, are the steps to get it:
- Generate a new Python file in
src_dnn/models - Add the imports:
from torch import Tensor, argmax, flatten, nn - Add the following code segment for a
nn.Module
# Important notes: Add a custom model name, but it must have a _v<idx> at the end!
class ModelName_v0(nn.Module):
def __init__(self):
super().__init__()
self.model_shape = (1, 28, 28)
# Define the model structure
self.model = nn.Sequential(
nn.Linear(784, 10)
)
def forward(self, x: Tensor) -> tuple[Tensor, Tensor]:
x = flatten(x, start_dim=1)
prob = self.model(x)
return prob, argmax(prob, 1)
- Run the
run_training.pyfile once in order to generate all config files ther should be a text output that new JSON files are generated and the request to run the python script againt (e.g., adapt and restart) - Leave the generated config files for now and run the
run_training.pyagain. Here, an example model will be deployed and another JSON file should be generated. - Finally, select your model by setting it's name in the
ConfigClassifier_MNIST.jsonconfig file undermodel_name. - Start training by runing the
run_training.pyfile again.
In order to build and train custom neural networks, we will use the MNIST dataset (link)[https://en.wikipedia.org/wiki/MNIST_database]. It includes 70,000 figures / samples with handwritten numbers (shape [28 x 28 pixels] in grayscale). Also, each sample has a label for the corresponding number (0-9).
During this exercise, we will train a simple model for this benchmark and enhance it until we get a lightweight and stable model.
All layer functionalities are in the torch.nn library.
Please try to extract the performance (accuracy, overfitting), extract the number of parameters for each model and explain the changes on the model's performance.
- Build and train a simple model with one
Linearlayer (768 -> 10). Are any further layers/functions necessary? - Please add an activation function like
PReLUorReLU. - Please add a second computing block of
LinearandPReLU/ReLU(784 -> 256 -> 10). - Please add a third computing block of
LinearandPReLU/ReLU(784 -> 256 -> 96 -> 10). - Please add a
Dropoutafter the last activation function - Please add a
BatchNorm1dafter the firstLinearlayer. - Please add a fourth computing block of
Linear,BatchNorm1dandPReLU/ReLU(784 -> 512 -> 256 -> 96 -> 10).
In this case you need a CNN for feature extraction and a MLP for classification:
Hint: We can talk a Conv2d, the following configuration is given in breakets: (num. filters, kernel size, stride, padding).
- Build and train a simple model with one
Conv2d(16, 4, 2, 1) layer,ReLU, andLazyLinear(10) for classification. Are any further layers/functions necessary? - Please add a pooling like
MaxPool2dlayer with (kernel=3, stride=2). - Please add a second computing block with
Conv2d(64, 3, 1, 0) andMaxPool2d(2, 1). - Please add a
BatchNorm2dafter eachConv2dlayer. - Please add a
Dropout2d(p=15%) before the last Conv2d layer. - Please add in the MLP sequential a computing layer with
Linear(Lazy -> 128 -> 10),BatchNorm1dandReLU.