Neural Networks: 1 Basic Optimizer
Neural Networks: 1 Basic Optimizer
DL Tutors Team
Exercise 1
April 25, 2023
Neural Networks
Neural Networks
1 Basic Optimizer
In this course we will implement advanced optimization schemes, but in the first exercise we
start with the basic Stochastic Gradient Descent (SGD).
Task:
• The Sgd constructor receives the learning rate with data type float.
You can verify your implementation using the provided testsuite by providing the commandline
parameter TestOptimizers1.
1
Deep Learning Exercises
DL Tutors Team
Exercise 1
April 25, 2023
Neural Networks
2 Base Layer
We will realize a small layer oriented Deep Learning framework in this exercise. Layer oriented
frameworks represent a higher level of abstraction to their users than graph oriented frame-
works. This approach limits flexibility but enables easy experimentation using conventional
architectures. Every layer in these architectures has to implement two fundamental opera-
tions: forward(input tensor), backward(error tensor). These operations are the basic
steps executed during training and testing.
We distinguish between trainable and non-trainable layers. Trainable layers have pa-
rameters that are optimized during training (e. g. the Fully Connected Layer, which must be
implemented in this task), while non-trainable layers remain fixed (e. g. the ReLU activation
function).
Task:
• This class will be inherited by every layer in our framework. For information on inheri-
tance in python, please refer to here.
• Write a constructor for this class receiving no arguments. In this constructor, initialize
a boolean member trainable with False. This member will be used to distinguish
trainable from non-trainable layers.
• Optionally, you can add other members like a default weights parameter, which might
come in handy.
2
Deep Learning Exercises
DL Tutors Team
Exercise 1
April 25, 2023
Neural Networks
The Fully Connected (FC) layer is the theoretic backbone of layer oriented architectures. It
performs a linear operation on its input.
Task:
3
Deep Learning Exercises
DL Tutors Team
Exercise 1
April 25, 2023
Neural Networks
The Rectified Linear Unit is the standard activation function in Deep Learning nowadays. It
has revolutionized Neural Networks because it reduces the effect of the “vanishing gradient”
problem.
Task:
Implement a class ReLU in the file “ReLU.py” in folder “Layers”. This class also has to
provide the methods forward(input tensor) and backward(error tensor).
• Write a constructor for this class, receiving no arguments. The ReLU does not have
trainable parameters, so you don’t have to change the inherited member trainable.
• Implement a method forward(input tensor) which returns a tensor that serves as the
input tensor for the next layer.
You can verify your implementation using the provided testsuite by providing the commandline
parameter TestReLU
4
Deep Learning Exercises
DL Tutors Team
Exercise 1
April 25, 2023
Neural Networks
5 SoftMax Layer
The SoftMax activation function is used to transform the logits (the output of the network)
into a probability distribution. Therefore, SoftMax is typically used for classification tasks.
Task:
Implement a class SoftMax in the file: “SoftMax.py” in folder “Layers”. This class also has
to provide the methods forward(input tensor) and backward(error tensor).
• Implement a method forward(input tensor) which returns the estimated class proba-
bilities for each row representing an element of the batch.
You can verify your implementation using the provided testsuite by providing the commandline
parameter TestSoftMax
5
Deep Learning Exercises
DL Tutors Team
Exercise 1
April 25, 2023
Neural Networks
The cross entropy Loss is often used in classification task, typically in conjunction with SoftMax
(or Sigmoid).
Task:
• Implement a method backward(label tensor) which returns the error tensor for the
previous layer. The backpropagation starts here, hence no error tensor is needed.
Instead, we need the label tensor.
Hint: the same hint as before applies.
You can verify your implementation using the provided testsuite by providing the commandline
parameter TestCrossEntropyLoss
6
Deep Learning Exercises
DL Tutors Team
Exercise 1
April 25, 2023
Neural Networks
The Neural Network defines the whole architecture by containing all its layers from the input
to the loss layer. This Network manages the testing and the training, that means it calls all
forward methods passing the data from the beginning to the end, as well as the optimization
by calling all backward passes afterwards.
Task:
• Implement a method forward using input from the data layer and passing it through
all layers of the network. Note that the data layer provides an input tensor and a
label tensor upon calling next() on it. The output of this function should be the
output of the last layer (i. e. the loss layer) of the network.
• Implement a method backward starting from the loss layer passing it the label tensor
for the current input and propagating it back through the network.
• Finally implement a convenience method test(input tensor) which propagates the in-
put tensor through the network and returns the prediction of the last layer. For clas-
sification tasks we typically query the probabilistic output of the SoftMax layer.
You can verify your implementation using the provided testsuite by providing the commandline
parameter TestNeuralNetwork1
7
Deep Learning Exercises
DL Tutors Team
Exercise 1
April 25, 2023
Neural Networks
Task:
Debug your implementation until every test in the suite passes. You can run all tests by
providing no commandline parameter. To run the unittests you can either execute them with
python in the terminal or with the dedicated unittest environment of PyCharm. We recommend
the latter one, as it provides a better overview of all tests. For the automated computation of
the bonus points achieved in one exercise, run the unittests with the bonus flag in a terminal,
with
to check out the manual. For dispatching your folder run e.g.
1
https://www.jetbrains.com/help/pycharm/creating-and-editing-run-debug-configurations.html