Deep Learning – Summer 2024/25

The objective of this course is to provide a comprehensive introduction to deep neural networks, which have consistently demonstrated superior performance across diverse domains, notably in processing and generating images, text, and speech.

The course focuses both on theory spanning from the basics to the latest advances, as well as on practical implementations in Python and PyTorch (students implement and train deep neural networks performing image classification, image segmentation, object detection, part of speech tagging, lemmatization, speech recognition, reading comprehension, and image generation). Basic Python skills are required, but no previous knowledge of artificial neural networks is needed; basic machine learning understanding is advantageous.

Students work either individually or in small teams on weekly assignments, including competition tasks, where the goal is to obtain the highest performance in the class.

Optionally, you can obtain a micro-credential after passing the course.

About

SIS code: NPFL138
Semester: summer
E-credits: 8
Examination: 3/4 C+Ex
Guarantor: Milan Straka

Timespace Coordinates

  • lectures: Czech lecture is held on Tuesday 15:40 in S5, English lecture on Tuesday 10:40 in S3; first lecture is on Feb 18
  • practicals: there are two parallel practicals, a Czech one on Thursday 10:40 in S9, and an English one on Thursday 9:00 in S9; first practicals are on Feb 20
  • consultations: entirely optional consultations take place on Wednesday 12:20 in S9; first consultations are on Feb 26

All lectures and practicals will be recorded and available on this website.

Lectures

1. Introduction to Deep Learning Slides PDF Slides CZ Lecture CZ Practicals EN Lecture EN Practicals Questions numpy_entropy pca_first mnist_layers_activations

2. Training Neural Networks Slides PDF Slides CZ Lecture CZ Practicals EN Lecture EN Practicals Questions sgd_backpropagation sgd_manual mnist_training gym_cartpole

3. Training Neural Networks II Slides PDF Slides CZ Lecture CZ Losses&Metrics CZ Practicals EN Lecture EN Losses&Metrics EN Practicals Questions mnist_regularization mnist_ensemble uppercase

License

Unless otherwise stated, teaching materials for this course are available under CC BY-SA 4.0.

A micro-credential (aka micro-certificate) is a digital certificate attesting that you have gained knowledge and skills in a specific area. It should be internationally recognized and verifiable using an online EU-wide verification system.

A micro-credential can be obtained both by the university students and external participants.

External Participants

If you are not a university student, you can apply to the Deep Learning micro-credential course and then attend the course along the university students. Upon successfully passing the course, you will obtain the micro-credential.

The price of the course is 5 000 Kč. The lectures take place for 14 weeks from Feb 18 to May 23, the examination period runs until the end of September.

If you have applied to the course, you only need to attend (or watch) the first lecture and the first practicals; the organization of the whole course and the setup instructions are described there.

University Students

If you have passed the course as a part of your study plan (in academic year 2024/25 or later), you can obtain the micro-credential by paying only an administrative fee. The exact price is not known yet, but our estimate is around 220 Kč. More information will be sent to the course participants during the examination period.


The lecture content, including references to study materials. The main study material is the Deep Learning Book by Ian Goodfellow, Yoshua Bengio and Aaron Courville, (referred to as DLB).

References to study materials cover all theory required at the exam, and sometimes even more – the references in italics cover topics not required for the exam.

1. Introduction to Deep Learning

 Feb 18 Slides PDF Slides CZ Lecture CZ Practicals EN Lecture EN Practicals Questions numpy_entropy pca_first mnist_layers_activations

  • Random variables, probability distributions, expectation, variance, Bernoulli distribution, Categorical distribution [Sections 3.2, 3.3, 3.8, 3.9.1 and 3.9.2 of DLB]
  • Self-information, entropy, cross-entropy, KL-divergence [Section 3.13 of DBL]
  • Gaussian distribution [Section 3.9.3 of DLB]
  • Machine Learning Basics [Section 5.1-5.1.3 of DLB]
  • History of Deep Learning [Section 1.2 of DLB]
  • Linear regression [Section 5.1.4 of DLB]
  • Challenges Motivating Deep Learning [Section 5.11 of DLB]
  • Neural network basics
    • Neural networks as graphs [Chapter 6 before Section 6.1 of DLB]
    • Output activation functions [Section 6.2.2 of DLB, excluding Section 6.2.2.4]
    • Hidden activation functions [Section 6.3 of DLB, excluding Section 6.3.3]
    • Basic network architectures [Section 6.4 of DLB, excluding Section 6.4.2]
  • Universal approximation theorem

2. Training Neural Networks

 Feb 25 Slides PDF Slides CZ Lecture CZ Practicals EN Lecture EN Practicals Questions sgd_backpropagation sgd_manual mnist_training gym_cartpole

  • Capacity, overfitting, underfitting, regularization [Section 5.2 of DLB]
  • Hyperparameters and validation sets [Section 5.3 of DLB]
  • Maximum Likelihood Estimation [Section 5.5 of DLB]
  • Neural network training
    • Gradient Descent and Stochastic Gradient Descent [Sections 4.3 and 5.9 of DLB]
    • Backpropagation algorithm [Section 6.5 to 6.5.3 of DLB, especially Algorithms 6.1 and 6.2; note that Algorithms 6.5 and 6.6 are used in practice]
    • SGD algorithm [Section 8.3.1 and Algorithm 8.1 of DLB]
    • SGD with Momentum algorithm [Section 8.3.2 and Algorithm 8.2 of DLB]
    • SGD with Nestorov Momentum algorithm [Section 8.3.3 and Algorithm 8.3 of DLB]
    • Optimization algorithms with adaptive gradients
      • AdaGrad algorithm [Section 8.5.1 and Algorithm 8.4 of DLB]
      • RMSProp algorithm [Section 8.5.2 and Algorithm 8.5 of DLB]
      • Adam algorithm [Section 8.5.3 and Algorithm 8.7 of DLB]

3. Training Neural Networks II

 Mar 4 Slides PDF Slides CZ Lecture CZ Losses&Metrics CZ Practicals EN Lecture EN Losses&Metrics EN Practicals Questions mnist_regularization mnist_ensemble uppercase

  • Regularization [Chapter 7 until Section 7.1 of DLB]
    • Early stopping [Section 7.8 of DLB, without the How early stopping acts as a regularizer part]
    • L2 and L1 regularization [Sections 7.1 and 5.6.1 of DLB; plus slides 17-18]
    • Dataset augmentation [Section 7.4 of DLB]
    • Ensembling [Section 7.11 of DLB]
    • Dropout [Section 7.12 of DLB]
    • Label smoothing [Section 7.5.1 of DLB]
  • Saturating non-linearities [Section 6.3.2 and second half of Section 6.2.2.2 of DLB]
  • Parameter initialization strategies [Section 8.4 of DLB]
  • Gradient clipping [Section 10.11.1 of DLB]
  • Softmax with NLL (negative log likelihood) as a loss function [Section 6.2.2.3 of DLB, notably equation (6.30)]

Requirements

To pass the practicals, you need to obtain at least 80 points, excluding the bonus points. Note that all surplus points (both bonus and non-bonus) will be transfered to the exam. In total, assignments for at least 120 points (not including the bonus points) will be available, and if you solve all the assignments (any non-zero amount of points counts as solved), you automatically pass the exam with grade 1.

Environment

The tasks are evaluated automatically using the ReCodEx Code Examiner.

The evaluation is performed using Python 3.11, PyTorch 2.6.0, Python Image Models 1.0.14, HF Transformers 4.48.1, and Gymnasium 1.0.0. You should install the exact version of these packages yourselves.

Teamwork

Solving assignments in teams (of size at most 3) is encouraged, but everyone has to participate (it is forbidden not to work on an assignment and then submit a solution created by other team members). All members of the team must submit in ReCodEx individually, but can have exactly the same sources/models/results. Each such solution must explicitly list all members of the team to allow plagiarism detection using this template.

No Cheating

Cheating is strictly prohibited and any student found cheating will be punished. The punishment can involve failing the whole course, or, in grave cases, being expelled from the faculty. While discussing assignments with any classmate is fine, each team must complete the assignments themselves, without using code they did not write (unless explicitly allowed). Of course, inside a team you are allowed to share code and submit identical solutions. Note that all students involved in cheating will be punished, so if you share your source code with a friend, both you and your friend will be punished. That also means that you should never publish your solutions.

numpy_entropy

 Deadline: Mar 05, 22:00  2 points

The goal of this exercise is to familiarize with Python, NumPy and ReCodEx submission system. Start with the numpy_entropy.py.

Load a file specified in args.data_path, whose lines consist of data points of our dataset, and load a file specified in args.model_path, which describes a model probability distribution, with each line being a tab-separated pair of (data point, probability).

Then compute the following quantities using NumPy, and print them each on a separate line rounded on two decimal places (or inf for positive infinity, which happens when an element of data distribution has zero probability under the model distribution):

  • entropy H(data distribution)
  • cross-entropy H(data distribution, model distribution)
  • KL-divergence DKL(data distribution, model distribution)

Use natural logarithms to compute the entropies and the divergence.

  1. python3 numpy_entropy.py --data_path numpy_entropy_data_1.txt --model_path numpy_entropy_model_1.txt
Entropy: 0.96 nats
Crossentropy: 0.99 nats
KL divergence: 0.03 nats
  1. python3 numpy_entropy.py --data_path numpy_entropy_data_2.txt --model_path numpy_entropy_model_2.txt
Entropy: 0.96 nats
Crossentropy: inf nats
KL divergence: inf nats

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

Entropy: 0.96 nats
Crossentropy: 0.99 nats
KL divergence: 0.03 nats
Entropy: 0.96 nats
Crossentropy: inf nats
KL divergence: inf nats
Entropy: 4.15 nats
Crossentropy: 4.23 nats
KL divergence: 0.08 nats
Entropy: 4.99 nats
Crossentropy: 5.03 nats
KL divergence: 0.04 nats

pca_first

 Deadline: Mar 05, 22:00  2 points

The goal of this exercise is to familiarize with PyTorch torch.Tensors, shapes and basic tensor manipulation methods. Start with the pca_first.py template.

In this assignment, you should compute the covariance matrix of several examples from the MNIST dataset, then compute the first principal component, and quantify the explained variance of it. It is fine if you are not familiar with terms like covariance matrix or principal component – the template contains a detailed description of what you have to do.

Finally, you might want to read the Introduction to PyTorch Tensors.

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  1. python3 pca_first.py --examples=1024 --iterations=64
Total variance: 53.12
Explained variance: 9.64%
  1. python3 pca_first.py --examples=8192 --iterations=128
Total variance: 53.05
Explained variance: 9.89%
  1. python3 pca_first.py --examples=55000 --iterations=1024
Total variance: 52.74
Explained variance: 9.71%

mnist_layers_activations

 Deadline: Mar 05, 22:00  2 points

Before solving the assignment, start by playing with example_pytorch_tensorboard.py, in order to familiarize with PyTorch and TensorBoard. After running the example, start TensorBoard in the same directory using tensorboard --logdir logs and open http://localhost:6006 in a browser and explore the generated logs.

Your goal is to modify the mnist_layers_activations.py template such that a user-specified neural network is constructed:

  • A number of hidden layers (including zero) can be specified on the command line using the parameter hidden_layers.
  • Activation function of these hidden layers can be also specified as a command line parameter activation, with supported values of none, relu, tanh and sigmoid.

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  1. python3 mnist_layers_activations.py --epochs=1 --hidden_layers=0 --activation=none
Epoch 1/1 1.0s train_loss=0.5374 train_accuracy=0.8614 dev_loss=0.2768 dev_accuracy=0.9270
  1. python3 mnist_layers_activations.py --epochs=1 --hidden_layers=1 --activation=none
Epoch 1/1 1.4s train_loss=0.3791 train_accuracy=0.8922 dev_loss=0.2400 dev_accuracy=0.9290
  1. python3 mnist_layers_activations.py --epochs=1 --hidden_layers=1 --activation=relu
Epoch 1/1 1.5s train_loss=0.3178 train_accuracy=0.9104 dev_loss=0.1482 dev_accuracy=0.9566
  1. python3 mnist_layers_activations.py --epochs=1 --hidden_layers=1 --activation=tanh
Epoch 1/1 1.4s train_loss=0.3318 train_accuracy=0.9061 dev_loss=0.1632 dev_accuracy=0.9530
  1. python3 mnist_layers_activations.py --epochs=1 --hidden_layers=1 --activation=sigmoid
Epoch 1/1 1.4s train_loss=0.4985 train_accuracy=0.8788 dev_loss=0.2156 dev_accuracy=0.9382
  1. python3 mnist_layers_activations.py --epochs=1 --hidden_layers=3 --activation=relu
Epoch 1/1 1.7s train_loss=0.2700 train_accuracy=0.9213 dev_loss=0.1188 dev_accuracy=0.9680

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  • python3 mnist_layers_activations.py --hidden_layers=0 --activation=none
Epoch  1/10 1.0s train_loss=0.5374 train_accuracy=0.8614 dev_loss=0.2768 dev_accuracy=0.9270
Epoch  5/10 1.0s train_loss=0.2779 train_accuracy=0.9220 dev_loss=0.2201 dev_accuracy=0.9430
Epoch 10/10 1.0s train_loss=0.2591 train_accuracy=0.9278 dev_loss=0.2139 dev_accuracy=0.9432
  • python3 mnist_layers_activations.py --hidden_layers=1 --activation=none
Epoch  1/10 1.4s train_loss=0.3791 train_accuracy=0.8922 dev_loss=0.2400 dev_accuracy=0.9290
Epoch  5/10 1.4s train_loss=0.2775 train_accuracy=0.9225 dev_loss=0.2217 dev_accuracy=0.9396
Epoch 10/10 1.4s train_loss=0.2645 train_accuracy=0.9247 dev_loss=0.2264 dev_accuracy=0.9378
  • python3 mnist_layers_activations.py --hidden_layers=1 --activation=relu
Epoch  1/10 1.4s train_loss=0.3178 train_accuracy=0.9104 dev_loss=0.1482 dev_accuracy=0.9566
Epoch  5/10 1.5s train_loss=0.0627 train_accuracy=0.9811 dev_loss=0.0827 dev_accuracy=0.9786
Epoch 10/10 1.6s train_loss=0.0240 train_accuracy=0.9930 dev_loss=0.0782 dev_accuracy=0.9810
  • python3 mnist_layers_activations.py --hidden_layers=1 --activation=tanh
Epoch  1/10 1.4s train_loss=0.3318 train_accuracy=0.9061 dev_loss=0.1632 dev_accuracy=0.9530
Epoch  5/10 1.4s train_loss=0.0732 train_accuracy=0.9798 dev_loss=0.0837 dev_accuracy=0.9768
Epoch 10/10 1.5s train_loss=0.0254 train_accuracy=0.9943 dev_loss=0.0733 dev_accuracy=0.9790
  • python3 mnist_layers_activations.py --hidden_layers=1 --activation=sigmoid
Epoch  1/10 1.4s train_loss=0.4985 train_accuracy=0.8788 dev_loss=0.2156 dev_accuracy=0.9382
Epoch  5/10 1.4s train_loss=0.1249 train_accuracy=0.9641 dev_loss=0.1077 dev_accuracy=0.9698
Epoch 10/10 1.4s train_loss=0.0605 train_accuracy=0.9837 dev_loss=0.0781 dev_accuracy=0.9762
  • python3 mnist_layers_activations.py --hidden_layers=3 --activation=relu
Epoch  1/10 1.7s train_loss=0.2700 train_accuracy=0.9213 dev_loss=0.1188 dev_accuracy=0.9680
Epoch  5/10 1.9s train_loss=0.0477 train_accuracy=0.9849 dev_loss=0.0787 dev_accuracy=0.9794
Epoch 10/10 1.9s train_loss=0.0248 train_accuracy=0.9916 dev_loss=0.1015 dev_accuracy=0.9762
  • python3 mnist_layers_activations.py --hidden_layers=10 --activation=relu
Epoch  1/10 2.8s train_loss=0.3562 train_accuracy=0.8911 dev_loss=0.1556 dev_accuracy=0.9598
Epoch  5/10 3.3s train_loss=0.0864 train_accuracy=0.9764 dev_loss=0.1164 dev_accuracy=0.9686
Epoch 10/10 3.3s train_loss=0.0474 train_accuracy=0.9874 dev_loss=0.0877 dev_accuracy=0.9774
  • python3 mnist_layers_activations.py --hidden_layers=10 --activation=sigmoid
Epoch  1/10 2.6s train_loss=1.9711 train_accuracy=0.1803 dev_loss=1.8477 dev_accuracy=0.2148
Epoch  5/10 2.6s train_loss=0.9947 train_accuracy=0.5815 dev_loss=0.8246 dev_accuracy=0.6392
Epoch 10/10 2.6s train_loss=0.4406 train_accuracy=0.8924 dev_loss=0.4239 dev_accuracy=0.8992

sgd_backpropagation

 Deadline: Mar 12, 22:00  3 points

The template was updated on Feb 27, 17:30. The original one did not shuffle the training data. You do not need to redownload it, ReCodEx accepts both variants. However, the Tests and Examples have been regenerated using the updated template.

In this exercise you will learn how to compute gradients using the so-called automatic differentiation, which allows to automatically run backpropagation algorithm for a given computation. You can read the Automatic Differentiation with torch.autograd tutorial if interested. After computing the gradient, you should then perform training by running manually implemented minibatch stochastic gradient descent.

Starting with the sgd_backpropagation.py template, you should:

  • implement a neural network with a single tanh hidden layer and categorical output layer;
  • compute the crossentropy loss;
  • use .backward() to automatically compute the gradient of the loss with respect to all variables;
  • perform the SGD update.

This assignment also demonstrate the most important parts of the npfl138.TrainableModule that we are using.

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  1. python3 sgd_backpropagation.py --epochs=2 --batch_size=64 --hidden_layer=20 --learning_rate=0.1
Dev accuracy after epoch 1 is 92.98
Dev accuracy after epoch 2 is 94.42
Test accuracy after epoch 2 is 92.72
  1. python3 sgd_backpropagation.py --epochs=2 --batch_size=100 --hidden_layer=32 --learning_rate=0.2
Dev accuracy after epoch 1 is 93.58
Dev accuracy after epoch 2 is 95.26
Test accuracy after epoch 2 is 93.75

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  • python3 sgd_backpropagation.py --batch_size=64 --hidden_layer=20 --learning_rate=0.1
Dev accuracy after epoch 1 is 92.98
Dev accuracy after epoch 2 is 94.42
Dev accuracy after epoch 3 is 94.68
Dev accuracy after epoch 4 is 95.08
Dev accuracy after epoch 5 is 95.28
Dev accuracy after epoch 6 is 95.20
Dev accuracy after epoch 7 is 95.52
Dev accuracy after epoch 8 is 95.32
Dev accuracy after epoch 9 is 95.66
Dev accuracy after epoch 10 is 95.84
Test accuracy after epoch 10 is 95.02
  • python3 sgd_backpropagation.py --batch_size=100 --hidden_layer=32 --learning_rate=0.2
Dev accuracy after epoch 1 is 93.58
Dev accuracy after epoch 2 is 95.26
Dev accuracy after epoch 3 is 95.66
Dev accuracy after epoch 4 is 95.90
Dev accuracy after epoch 5 is 96.26
Dev accuracy after epoch 6 is 96.52
Dev accuracy after epoch 7 is 96.52
Dev accuracy after epoch 8 is 96.74
Dev accuracy after epoch 9 is 96.74
Dev accuracy after epoch 10 is 96.62
Test accuracy after epoch 10 is 95.84

sgd_manual  

 Deadline: Mar 12 Mar 19, 22:00  2 points

The template was updated on Feb 27, 17:30. The original one did not shuffle the training data. You do not need to redownload it, ReCodEx accepts both variants. However, the Tests and Examples have been regenerated using the updated template.

The goal in this exercise is to extend your solution to the sgd_backpropagation assignment by manually computing the gradient.

While in this assignment we compute the gradient manually, we will nearly always use the automatic differentiation. Therefore, the assignment is more of a mathematical exercise than a real-world application. Furthermore, we will compute the derivatives together on the Mar 06 practicals.

Start with the sgd_manual.py template, which is based on the sgd_backpropagation.py one.

Note that ReCodEx disables the PyTorch automatic differentiation during evaluation.

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  1. python3 sgd_manual.py --epochs=2 --batch_size=64 --hidden_layer=20 --learning_rate=0.1
Dev accuracy after epoch 1 is 92.98
Dev accuracy after epoch 2 is 94.42
Test accuracy after epoch 2 is 92.72
  1. python3 sgd_manual.py --epochs=2 --batch_size=100 --hidden_layer=32 --learning_rate=0.2
Dev accuracy after epoch 1 is 93.58
Dev accuracy after epoch 2 is 95.26
Test accuracy after epoch 2 is 93.75

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  • python3 sgd_manual.py --batch_size=64 --hidden_layer=20 --learning_rate=0.1
Dev accuracy after epoch 1 is 92.98
Dev accuracy after epoch 2 is 94.42
Dev accuracy after epoch 3 is 94.68
Dev accuracy after epoch 4 is 95.08
Dev accuracy after epoch 5 is 95.28
Dev accuracy after epoch 6 is 95.20
Dev accuracy after epoch 7 is 95.52
Dev accuracy after epoch 8 is 95.32
Dev accuracy after epoch 9 is 95.66
Dev accuracy after epoch 10 is 95.84
Test accuracy after epoch 10 is 95.02
  • python3 sgd_manual.py --batch_size=100 --hidden_layer=32 --learning_rate=0.2
Dev accuracy after epoch 1 is 93.58
Dev accuracy after epoch 2 is 95.26
Dev accuracy after epoch 3 is 95.66
Dev accuracy after epoch 4 is 95.90
Dev accuracy after epoch 5 is 96.26
Dev accuracy after epoch 6 is 96.52
Dev accuracy after epoch 7 is 96.52
Dev accuracy after epoch 8 is 96.74
Dev accuracy after epoch 9 is 96.74
Dev accuracy after epoch 10 is 96.62
Test accuracy after epoch 10 is 95.84

mnist_training

 Deadline: Mar 12, 22:00  2 points

This exercise should teach you using different optimizers, learning rates, and learning rate decays. Your goal is to modify the mnist_training.py template and implement the following:

  • Using specified optimizer (either SGD or Adam).
  • Optionally using momentum for the SGD optimizer.
  • Using specified learning rate for the optimizer.
  • Optionally use a given learning rate schedule. The schedule can be either linear, exponential, or cosine. If a schedule is specified, you also get a final learning rate, and the learning rate should be gradually decresed during training to reach the final learning rate just after the training (i.e., the first update after the training would use exactly the final learning rate).

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  1. python3 mnist_training.py --epochs=1 --optimizer=SGD --learning_rate=0.01
Epoch 1/1 1.2s train_loss=0.8300 train_accuracy=0.7960 dev_loss=0.3780 dev_accuracy=0.9060
  1. python3 mnist_training.py --epochs=1 --optimizer=SGD --learning_rate=0.01 --momentum=0.9
Epoch 1/1 1.2s train_loss=0.3731 train_accuracy=0.8952 dev_loss=0.1912 dev_accuracy=0.9472
  1. python3 mnist_training.py --epochs=1 --optimizer=SGD --learning_rate=0.1
Epoch 1/1 1.1s train_loss=0.3660 train_accuracy=0.8970 dev_loss=0.1945 dev_accuracy=0.9460
  1. python3 mnist_training.py --epochs=1 --optimizer=Adam --learning_rate=0.001
Epoch 1/1 1.5s train_loss=0.3025 train_accuracy=0.9152 dev_loss=0.1487 dev_accuracy=0.9582
  1. python3 mnist_training.py --epochs=1 --optimizer=Adam --learning_rate=0.01
Epoch 1/1 1.6s train_loss=0.2333 train_accuracy=0.9297 dev_loss=0.1618 dev_accuracy=0.9508
  1. python3 mnist_training.py --epochs=2 --optimizer=Adam --learning_rate=0.01 --decay=linear --learning_rate_final=0.0001
Epoch 1/2 1.6s train_loss=0.2162 train_lr=0.0050 train_accuracy=0.9341 dev_loss=0.1150 dev_accuracy=0.9658
Epoch 2/2 1.9s train_loss=0.0790 train_lr=0.0001 train_accuracy=0.9759 dev_loss=0.0739 dev_accuracy=0.9778
Next learning rate to be used: 0.0001
  1. python3 mnist_training.py --epochs=2 --optimizer=Adam --learning_rate=0.01 --decay=exponential --learning_rate_final=0.001
Epoch 1/2 1.6s train_loss=0.2022 train_lr=0.0032 train_accuracy=0.9383 dev_loss=0.0989 dev_accuracy=0.9746
Epoch 2/2 1.8s train_loss=0.0748 train_lr=0.0010 train_accuracy=0.9769 dev_loss=0.0777 dev_accuracy=0.9790
Next learning rate to be used: 0.001
  1. python3 mnist_training.py --epochs=2 --optimizer=Adam --learning_rate=0.01 --decay=cosine --learning_rate_final=0.0001
Epoch 1/2 1.7s train_loss=0.2192 train_lr=0.0050 train_accuracy=0.9333 dev_loss=0.1155 dev_accuracy=0.9680
Epoch 2/2 1.9s train_loss=0.0720 train_lr=0.0001 train_accuracy=0.9776 dev_loss=0.0765 dev_accuracy=0.9790
Next learning rate to be used: 0.0001

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  • python3 mnist_training.py --optimizer=SGD --learning_rate=0.01
Epoch  1/10 1.1s train_loss=0.8300 train_accuracy=0.7960 dev_loss=0.3780 dev_accuracy=0.9060
Epoch  2/10 1.1s train_loss=0.4088 train_accuracy=0.8892 dev_loss=0.2940 dev_accuracy=0.9208
Epoch  3/10 1.1s train_loss=0.3473 train_accuracy=0.9030 dev_loss=0.2585 dev_accuracy=0.9286
Epoch  4/10 1.1s train_loss=0.3144 train_accuracy=0.9116 dev_loss=0.2383 dev_accuracy=0.9352
Epoch  5/10 1.1s train_loss=0.2911 train_accuracy=0.9184 dev_loss=0.2230 dev_accuracy=0.9404
Epoch  6/10 1.1s train_loss=0.2729 train_accuracy=0.9235 dev_loss=0.2093 dev_accuracy=0.9432
Epoch  7/10 1.2s train_loss=0.2577 train_accuracy=0.9281 dev_loss=0.1993 dev_accuracy=0.9480
Epoch  8/10 1.1s train_loss=0.2442 train_accuracy=0.9316 dev_loss=0.1903 dev_accuracy=0.9510
Epoch  9/10 1.1s train_loss=0.2326 train_accuracy=0.9350 dev_loss=0.1828 dev_accuracy=0.9546
Epoch 10/10 1.1s train_loss=0.2222 train_accuracy=0.9379 dev_loss=0.1744 dev_accuracy=0.9546
  • python3 mnist_training.py --optimizer=SGD --learning_rate=0.01 --momentum=0.9
Epoch  1/10 1.2s train_loss=0.3731 train_accuracy=0.8952 dev_loss=0.1912 dev_accuracy=0.9472
Epoch  2/10 1.3s train_loss=0.1942 train_accuracy=0.9437 dev_loss=0.1322 dev_accuracy=0.9662
Epoch  3/10 1.4s train_loss=0.1432 train_accuracy=0.9588 dev_loss=0.1137 dev_accuracy=0.9688
Epoch  4/10 1.4s train_loss=0.1148 train_accuracy=0.9674 dev_loss=0.0954 dev_accuracy=0.9744
Epoch  5/10 1.4s train_loss=0.0962 train_accuracy=0.9728 dev_loss=0.0914 dev_accuracy=0.9740
Epoch  6/10 1.4s train_loss=0.0824 train_accuracy=0.9767 dev_loss=0.0823 dev_accuracy=0.9772
Epoch  7/10 1.4s train_loss=0.0718 train_accuracy=0.9801 dev_loss=0.0806 dev_accuracy=0.9780
Epoch  8/10 1.4s train_loss=0.0640 train_accuracy=0.9817 dev_loss=0.0741 dev_accuracy=0.9800
Epoch  9/10 1.4s train_loss=0.0565 train_accuracy=0.9841 dev_loss=0.0775 dev_accuracy=0.9800
Epoch 10/10 1.4s train_loss=0.0509 train_accuracy=0.9861 dev_loss=0.0737 dev_accuracy=0.9788
  • python3 mnist_training.py --optimizer=SGD --learning_rate=0.1
Epoch  1/10 1.2s train_loss=0.3660 train_accuracy=0.8970 dev_loss=0.1945 dev_accuracy=0.9460
Epoch  2/10 1.1s train_loss=0.1940 train_accuracy=0.9438 dev_loss=0.1320 dev_accuracy=0.9652
Epoch  3/10 1.1s train_loss=0.1433 train_accuracy=0.9588 dev_loss=0.1101 dev_accuracy=0.9696
Epoch  4/10 1.2s train_loss=0.1146 train_accuracy=0.9673 dev_loss=0.0941 dev_accuracy=0.9748
Epoch  5/10 1.2s train_loss=0.0949 train_accuracy=0.9735 dev_loss=0.0915 dev_accuracy=0.9754
Epoch  6/10 1.1s train_loss=0.0816 train_accuracy=0.9766 dev_loss=0.0804 dev_accuracy=0.9782
Epoch  7/10 1.1s train_loss=0.0714 train_accuracy=0.9800 dev_loss=0.0783 dev_accuracy=0.9792
Epoch  8/10 1.1s train_loss=0.0627 train_accuracy=0.9819 dev_loss=0.0734 dev_accuracy=0.9804
Epoch  9/10 1.1s train_loss=0.0558 train_accuracy=0.9843 dev_loss=0.0759 dev_accuracy=0.9814
Epoch 10/10 1.2s train_loss=0.0502 train_accuracy=0.9860 dev_loss=0.0728 dev_accuracy=0.9806
  • python3 mnist_training.py --optimizer=Adam --learning_rate=0.001
Epoch  1/10 1.5s train_loss=0.3025 train_accuracy=0.9152 dev_loss=0.1487 dev_accuracy=0.9582
Epoch  2/10 1.6s train_loss=0.1349 train_accuracy=0.9601 dev_loss=0.1003 dev_accuracy=0.9724
Epoch  3/10 1.6s train_loss=0.0909 train_accuracy=0.9724 dev_loss=0.0893 dev_accuracy=0.9756
Epoch  4/10 1.6s train_loss=0.0686 train_accuracy=0.9797 dev_loss=0.0879 dev_accuracy=0.9742
Epoch  5/10 1.6s train_loss=0.0542 train_accuracy=0.9838 dev_loss=0.0755 dev_accuracy=0.9782
Epoch  6/10 1.6s train_loss=0.0434 train_accuracy=0.9873 dev_loss=0.0781 dev_accuracy=0.9786
Epoch  7/10 1.6s train_loss=0.0344 train_accuracy=0.9900 dev_loss=0.0735 dev_accuracy=0.9796
Epoch  8/10 1.7s train_loss=0.0280 train_accuracy=0.9913 dev_loss=0.0746 dev_accuracy=0.9800
Epoch  9/10 1.6s train_loss=0.0225 train_accuracy=0.9934 dev_loss=0.0768 dev_accuracy=0.9814
Epoch 10/10 1.6s train_loss=0.0189 train_accuracy=0.9947 dev_loss=0.0838 dev_accuracy=0.9780
  • python3 mnist_training.py --optimizer=Adam --learning_rate=0.01
Epoch  1/10 1.6s train_loss=0.2333 train_accuracy=0.9297 dev_loss=0.1618 dev_accuracy=0.9508
Epoch  2/10 1.9s train_loss=0.1456 train_accuracy=0.9569 dev_loss=0.1718 dev_accuracy=0.9600
Epoch  3/10 1.9s train_loss=0.1257 train_accuracy=0.9637 dev_loss=0.1653 dev_accuracy=0.9626
Epoch  4/10 1.9s train_loss=0.1128 train_accuracy=0.9679 dev_loss=0.1789 dev_accuracy=0.9604
Epoch  5/10 1.9s train_loss=0.1013 train_accuracy=0.9718 dev_loss=0.1316 dev_accuracy=0.9684
Epoch  6/10 2.0s train_loss=0.0992 train_accuracy=0.9729 dev_loss=0.1425 dev_accuracy=0.9642
Epoch  7/10 2.0s train_loss=0.0963 train_accuracy=0.9750 dev_loss=0.1814 dev_accuracy=0.9702
Epoch  8/10 2.0s train_loss=0.0969 train_accuracy=0.9759 dev_loss=0.1727 dev_accuracy=0.9712
Epoch  9/10 2.0s train_loss=0.0833 train_accuracy=0.9786 dev_loss=0.1854 dev_accuracy=0.9666
Epoch 10/10 2.0s train_loss=0.0808 train_accuracy=0.9796 dev_loss=0.1904 dev_accuracy=0.9710
  • python3 mnist_training.py --optimizer=Adam --learning_rate=0.01 --decay=linear --learning_rate_final=0.0001
Epoch  1/10 1.6s train_loss=0.2329 train_lr=0.0090 train_accuracy=0.9295 dev_loss=0.1592 dev_accuracy=0.9542
Epoch  2/10 1.9s train_loss=0.1313 train_lr=0.0080 train_accuracy=0.9611 dev_loss=0.1211 dev_accuracy=0.9674
Epoch  3/10 1.9s train_loss=0.0983 train_lr=0.0070 train_accuracy=0.9696 dev_loss=0.1034 dev_accuracy=0.9734
Epoch  4/10 1.9s train_loss=0.0713 train_lr=0.0060 train_accuracy=0.9784 dev_loss=0.1250 dev_accuracy=0.9690
Epoch  5/10 1.9s train_loss=0.0557 train_lr=0.0051 train_accuracy=0.9825 dev_loss=0.1086 dev_accuracy=0.9748
Epoch  6/10 1.9s train_loss=0.0414 train_lr=0.0041 train_accuracy=0.9867 dev_loss=0.0983 dev_accuracy=0.9776
Epoch  7/10 1.9s train_loss=0.0246 train_lr=0.0031 train_accuracy=0.9921 dev_loss=0.1009 dev_accuracy=0.9782
Epoch  8/10 1.9s train_loss=0.0144 train_lr=0.0021 train_accuracy=0.9955 dev_loss=0.0996 dev_accuracy=0.9798
Epoch  9/10 2.0s train_loss=0.0072 train_lr=0.0011 train_accuracy=0.9979 dev_loss=0.0999 dev_accuracy=0.9800
Epoch 10/10 1.9s train_loss=0.0039 train_lr=0.0001 train_accuracy=0.9993 dev_loss=0.0985 dev_accuracy=0.9812
Next learning rate to be used: 0.0001
  • python3 mnist_training.py --optimizer=Adam --learning_rate=0.01 --decay=exponential --learning_rate_final=0.001
Epoch  1/10 1.6s train_loss=0.2235 train_lr=0.0079 train_accuracy=0.9331 dev_loss=0.1471 dev_accuracy=0.9584
Epoch  2/10 1.9s train_loss=0.1151 train_lr=0.0063 train_accuracy=0.9654 dev_loss=0.1097 dev_accuracy=0.9706
Epoch  3/10 1.9s train_loss=0.0782 train_lr=0.0050 train_accuracy=0.9757 dev_loss=0.1059 dev_accuracy=0.9748
Epoch  4/10 1.9s train_loss=0.0521 train_lr=0.0040 train_accuracy=0.9839 dev_loss=0.0984 dev_accuracy=0.9720
Epoch  5/10 1.9s train_loss=0.0366 train_lr=0.0032 train_accuracy=0.9879 dev_loss=0.1046 dev_accuracy=0.9764
Epoch  6/10 1.9s train_loss=0.0235 train_lr=0.0025 train_accuracy=0.9921 dev_loss=0.0965 dev_accuracy=0.9798
Epoch  7/10 1.9s train_loss=0.0144 train_lr=0.0020 train_accuracy=0.9954 dev_loss=0.0914 dev_accuracy=0.9810
Epoch  8/10 1.9s train_loss=0.0101 train_lr=0.0016 train_accuracy=0.9970 dev_loss=0.0924 dev_accuracy=0.9808
Epoch  9/10 1.9s train_loss=0.0057 train_lr=0.0013 train_accuracy=0.9986 dev_loss=0.1007 dev_accuracy=0.9820
Epoch 10/10 1.9s train_loss=0.0038 train_lr=0.0010 train_accuracy=0.9992 dev_loss=0.0926 dev_accuracy=0.9832
Next learning rate to be used: 0.001
  • python3 mnist_training.py --optimizer=Adam --learning_rate=0.01 --decay=cosine --learning_rate_final=0.0001
Epoch  1/10 1.6s train_loss=0.2362 train_lr=0.0098 train_accuracy=0.9288 dev_loss=0.1563 dev_accuracy=0.9556
Epoch  2/10 1.9s train_loss=0.1340 train_lr=0.0091 train_accuracy=0.9605 dev_loss=0.1450 dev_accuracy=0.9652
Epoch  3/10 1.9s train_loss=0.1088 train_lr=0.0080 train_accuracy=0.9688 dev_loss=0.1465 dev_accuracy=0.9612
Epoch  4/10 2.0s train_loss=0.0774 train_lr=0.0066 train_accuracy=0.9767 dev_loss=0.1184 dev_accuracy=0.9706
Epoch  5/10 1.9s train_loss=0.0569 train_lr=0.0050 train_accuracy=0.9823 dev_loss=0.1140 dev_accuracy=0.9762
Epoch  6/10 2.0s train_loss=0.0381 train_lr=0.0035 train_accuracy=0.9876 dev_loss=0.1166 dev_accuracy=0.9770
Epoch  7/10 1.9s train_loss=0.0195 train_lr=0.0021 train_accuracy=0.9939 dev_loss=0.1022 dev_accuracy=0.9800
Epoch  8/10 1.9s train_loss=0.0097 train_lr=0.0010 train_accuracy=0.9972 dev_loss=0.1059 dev_accuracy=0.9808
Epoch  9/10 1.9s train_loss=0.0055 train_lr=0.0003 train_accuracy=0.9989 dev_loss=0.1073 dev_accuracy=0.9792
Epoch 10/10 1.9s train_loss=0.0040 train_lr=0.0001 train_accuracy=0.9993 dev_loss=0.1071 dev_accuracy=0.9792
Next learning rate to be used: 0.0001

gym_cartpole

 Deadline: Mar 12, 22:00  3 points

Solve the CartPole-v1 environment from the Gymnasium library, utilizing only provided supervised training dataset of 100 examples. Start with the gym_cartpole.py template.

The solution to this task should be a model which passes evaluation on random inputs. This evaluation can be performed by running the gym_cartpole.py with --evaluate argument (optionally rendering if --render option is provided), or directly calling the evaluate_model method. In order to pass, you must achieve an average reward of at least 475 on 100 episodes. Your model should have two outputs (i.e., corresponding to a categorical distribution with 2 output classes).

When designing the model, you should consider that the size of the training data is very small and the data is quite noisy.

When submitting to ReCodEx, do not forget to also submit the trained model.

mnist_regularization

 Deadline: Mar 19, 22:00  3 points

You will learn how to implement three regularization methods in this assignment. Start with the mnist_regularization.py template and implement the following:

  • Allow using dropout with rate args.dropout. Add a dropout layer after the first Flatten and also after all Linear hidden layers (but not after the output layer).
  • Allow using AdamW with weight decay with strength of args.weight_decay, making sure the weight decay is not applied on bias.
  • Allow using label smoothing with weight args.label_smoothing.

In addition to submitting the task in ReCodEx, also run the following variations and observe the results in TensorBoard, notably the training, development and test set accuracy and loss:

  • dropout rate 0, 0.3, 0.5, 0.6, 0.8;
  • weight decay 0, 0.1, 0.3, 0.5, 1.0;
  • label smoothing 0, 0.1, 0.3, 0.5.

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  1. python3 mnist_regularization.py --epochs=1 --dropout=0.3
Epoch 1/1 0.4s train_loss=0.7775 train_accuracy=0.7704 dev_loss=0.3211 dev_accuracy=0.9122
  1. python3 mnist_regularization.py --epochs=1 --dropout=0.5 --hidden_layers 300 300
Epoch 1/1 0.4s train_loss=1.5365 train_accuracy=0.4824 dev_loss=0.5010 dev_accuracy=0.8680
  1. python3 mnist_regularization.py --epochs=1 --weight_decay=0.1
Epoch 1/1 0.4s train_loss=0.5948 train_accuracy=0.8386 dev_loss=0.2868 dev_accuracy=0.9206
  1. python3 mnist_regularization.py --epochs=1 --weight_decay=0.3
Epoch 1/1 0.4s train_loss=0.5969 train_accuracy=0.8386 dev_loss=0.2890 dev_accuracy=0.9206
  1. python3 mnist_regularization.py --epochs=1 --label_smoothing=0.1
Epoch 1/1 0.4s train_loss=0.9841 train_accuracy=0.8442 dev_loss=0.7734 dev_accuracy=0.9244
  1. python3 mnist_regularization.py --epochs=1 --label_smoothing=0.3
Epoch 1/1 0.4s train_loss=1.5040 train_accuracy=0.8458 dev_loss=1.3727 dev_accuracy=0.9312

mnist_ensemble

 Deadline: Mar 19, 22:00  2 points

Your goal in this assignment is to implement ensembling of classification models by averaging their predicted probability distributions. The mnist_ensemble.py template trains args.models individual models, and your goal is to perform an ensemble of the first model, first two models, first three models, …, all models, and evaluate their accuracy on the development set.

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  1. python3 mnist_ensemble.py --epochs=1 --models=5
Model 1, individual accuracy 96.08, ensemble accuracy 96.08
Model 2, individual accuracy 96.18, ensemble accuracy 96.48
Model 3, individual accuracy 96.02, ensemble accuracy 96.58
Model 4, individual accuracy 95.94, ensemble accuracy 96.64
Model 5, individual accuracy 96.14, ensemble accuracy 96.66
  1. python3 mnist_ensemble.py --epochs=1 --models=5 --hidden_layer_size=200
Model 1, individual accuracy 96.58, ensemble accuracy 96.58
Model 2, individual accuracy 96.70, ensemble accuracy 96.80
Model 3, individual accuracy 96.70, ensemble accuracy 97.04
Model 4, individual accuracy 96.96, ensemble accuracy 97.14
Model 5, individual accuracy 96.76, ensemble accuracy 97.12

Note that your results may be slightly different, depending on your CPU type and whether you use a GPU.

  • python3 mnist_ensemble.py --models=5
Model 1, individual accuracy 97.76, ensemble accuracy 97.76
Model 2, individual accuracy 97.90, ensemble accuracy 98.08
Model 3, individual accuracy 97.92, ensemble accuracy 98.30
Model 4, individual accuracy 98.02, ensemble accuracy 98.36
Model 5, individual accuracy 97.86, ensemble accuracy 98.38
  • python3 mnist_ensemble.py --models=5 --hidden_layer_size=200
Model 1, individual accuracy 98.10, ensemble accuracy 98.10
Model 2, individual accuracy 98.20, ensemble accuracy 98.42
Model 3, individual accuracy 97.90, ensemble accuracy 98.44
Model 4, individual accuracy 97.96, ensemble accuracy 98.46
Model 5, individual accuracy 97.90, ensemble accuracy 98.58

uppercase

 Deadline: Mar 19, 22:00  4 points+5 bonus

This assignment introduces first NLP task. Your goal is to implement a model which is given Czech lowercased text and tries to uppercase appropriate letters. To load the dataset, use the npfl138.datasets.uppercase_data module which loads (and if required also downloads) the data. While the training and the development sets are in correct case, the test set is lowercased.

This is an open-data task, where you submit only the uppercased test set together with the training script (which will not be executed, it will be only used to understand the approach you took, and to indicate teams). Explicitly, submit exactly one .txt file and at least one .py/ipynb file.

The task is also a competition. Everyone who submits a solution achieving at least 98.5% accuracy gets 4 basic points; the remaining 5 bonus points are distributed depending on relative ordering of your solutions. The accuracy is computed per-character and can be evaluated programatically using the UppercaseData.evaluate_file method, or by running python3 -m npfl138.datasets.uppercase_data command with --evaluate argument.

Start with the uppercase.py template, which uses the npfl138.datasets.uppercase_data to load the data, generate an alphabet of given size containing most frequent characters, and generate sliding window view on the data. The template also comments on possibilities of character representation.

Do not use RNNs, CNNs, or Transformer in this task (if you have doubts, contact me); fully connected layers (and therefore also embedding layers), any activations, residual connections, and any regularization layers are fine.

In the competitions, your goal is to train a model, and then predict target values on the given unannotated test set.

Submitting to ReCodEx

When submitting a competition solution to ReCodEx, you can include any number of files of any kind, and either submit them individually or compess them in a .zip file. However, there should be exactly one text file with the test set annotation (.txt) and at least one Python source (.py/ipynb) containing the model training and prediction. The Python sources are not executed, but must be included for inspection.

Competition Evaluation

  • For every submission, ReCodEx checks the above conditions (exactly one .txt, at least one .py/ipynb) and whether the given annotations can be evaluated without error. If not, it will report the corresponding error in the logs.

  • Before the first deadline, ReCodEx prints the exact achieved performance, but only if it is worse than the baseline.

    If you surpass the baseline, the assignment is marked as solved in ReCodEx and you immediately get regular points for the assignment. However, ReCodEx does not print the reached performance.

  • After the first deadline, the latest submission of every user surpassing the required baseline participates in a competition. Additional bonus points are then awarded according to the ordering of the performance of the participating submissions.

  • After the competition results announcement, ReCodEx starts to show the exact performance for all the already submitted solutions and also for the solutions submitted later.

What Is Allowed

  • You can use only the given annotated data for training and evaluation.
  • You can use the given annotated training data in any way.
  • You can use the given annotated development data for evaluation or hyperparameter tuning, but not for the training itself.
  • Additionally, you can use any unannotated or manually created data for training and evaluation.
  • The test set annotations must be the result of your system (so you cannot manually correct them; but your system can contain other parts than just trained models, like hand-written rules).
  • Do not use test set annotations in any way, if you somehow get access to them.
  • Unless stated otherwise, you can use any architecture to solve the competition task at hand, but the implementation must be created by you and you must understand it fully. You can of course take inspiration from any paper or existing implementation, but please reference it in that case.
    • You can of course use anything from the PyTorch package (but unless not stated otherwise, do not use models from torchvision, timm, torchaudio, …).
    • You can use any data augmentation (even implementations not written by you).
    • You can use any optimizer and any hyperparameter optimization method (even implementations not written by you).
  • If you utilize an already trained model, it must be trained only on the allowed training data, unless stated otherwise.

Install

  • What Python version to use

    The recommended Python version is 3.11. This version is used by ReCodEx to evaluate your solutions. Minimum required version is Python 3.10, and the newest version that currently (as of Feb 20) works is Python 3.12 (because some dependencies do not yet provide precompiled binary packages for Python 3.13).

    You can find out the version of your Python installation using python3 --version.

  • Installing to central user packages repository

    You can install all required packages to central user packages repository using python3 -m pip install --user --no-cache-dir --extra-index-url=https://download.pytorch.org/whl/cu118 npfl138.

    On Linux and Windows, the above command installs CUDA 11.8 PyTorch build, but you can change cu118 to:

    • cpu to get CPU-only (smaller) version,
    • cu124 to get CUDA 12.4 build,
    • rocm6.2.4 to get AMD ROCm 6.2.4 build (Linux only).

    On macOS, the --extra-index-url has no effect and the Metal support is installed in any case.

    To update the npfl138 package later, use python3 -m pip install --user --upgrade npfl138.

  • Installing to a virtual environment

    Python supports virtual environments, which are directories containing independent sets of installed packages. You can create a virtual environment by running python3 -m venv VENV_DIR followed by VENV_DIR/bin/pip install --no-cache-dir --extra-index-url=https://download.pytorch.org/whl/cu118 npfl138. (or VENV_DIR/Scripts/pip on Windows).

    Again, apart from the CUDA 11.8 build, you can change cu118 on Linux and Windows to:

    • cpu to get CPU-only (smaller) version,
    • cu124 to get CUDA 12.4 build,
    • rocm6.2.4 to get AMD ROCm 6.2.4 build (Linux only).

    To update the npfl138 package later, use VENV_DIR/bin/pip install --upgrade npfl138.

  • Windows installation

    • On Windows, it can happen that python3 is not in PATH, while py command is – in that case you can use py -m venv VENV_DIR, which uses the newest Python available, or for example py -3.11 -m venv VENV_DIR, which uses Python version 3.11.

    • If you encounter a problem creating the logs in the args.logdir directory, a possible cause is that the path is longer than 260 characters, which is the default maximum length of a complete path on Windows. However, you can increase this limit on Windows 10, version 1607 or later, by following the instructions.

  • MacOS installation

  • GPU support on Linux and Windows

    PyTorch supports NVIDIA GPU or AMD GPU out of the box, you just need to select appropriate --extra-index-url when installing the packages.

    If you encounter problems loading CUDA or cuDNN libraries, make sure your LD_LIBRARY_PATH does not contain paths to older CUDA/cuDNN libraries.

MetaCentrum

  • How to apply for MetaCentrum account?

    After reading the Terms and conditions, you can apply for an account here.

    After your account is created, please make sure that the directories containing your solutions are always private.

  • How to activate Python 3.10 on MetaCentrum?

    On Metacentrum, currently the newest available Python is 3.10, which you need to activate in every session by running the following command:

    module add python/python-3.10.4-intel-19.0.4-sc7snnf
    
  • How to install the required virtual environment on MetaCentrum?

    To create a virtual environment, you first need to decide where it will reside. Either you can find a permanent storage, where you have large-enough quota, or you can use scratch storage for a submitted job.

    TL;DR:

    • Run an interactive CPU job, asking for 16GB scratch space:

      qsub -l select=1:ncpus=1:mem=8gb:scratch_local=16gb -I
      
    • In the job, use the allocated scratch space as the temporary directory:

      export TMPDIR=$SCRATCHDIR
      
    • You should clear the scratch space before you exit using the clean_scratch command. You can instruct the shell to call it automatically by running:

      trap 'clean_scratch' TERM EXIT
      
    • Finally, create the virtual environment and install PyTorch in it:

      module add python/python-3.10.4-intel-19.0.4-sc7snnf
      python3 -m venv CHOSEN_VENV_DIR
      CHOSEN_VENV_DIR/bin/pip install --no-cache-dir --upgrade pip setuptools
      CHOSEN_VENV_DIR/bin/pip install --no-cache-dir --extra-index-url=https://download.pytorch.org/whl/cu118 npfl138
      
  • How to run a GPU computation on MetaCentrum?

    First, read the official MetaCentrum documentation: Basic terms, Run simple job, GPU computing, GPU clusters.

    TL;DR: To run an interactive GPU job with 1 CPU, 1 GPU, 8GB RAM, and 16GB scatch space, run:

    qsub -q gpu -l select=1:ncpus=1:ngpus=1:mem=8gb:scratch_local=16gb -I
    

    To run a script in a non-interactive way, replace the -I option with the script to be executed.

    If you want to run a CPU-only computation, remove the -q gpu and ngpus=1: from the above commands.

AIC

  • How to install required packages on AIC?

    The Python 3.11.7 is available /opt/python/3.11.7/bin/python3, so you should start by creating a virtual environment using

    /opt/python/3.11.7/bin/python3 -m venv VENV_DIR
    

    and then install the required packages in it using

    VENV_DIR/bin/pip install --no-cache-dir --extra-index-url=https://download.pytorch.org/whl/cu118 npfl138
    
  • How to run a GPU computation on AIC?

    First, read the official AIC documentation: Submitting CPU Jobs, Submitting GPU Jobs.

    TL;DR: To run an interactive GPU job with 1 CPU, 1 GPU, and 16GB RAM, run:

    srun -p gpu -c1 -G1 --mem=16G --pty bash
    

    To run a shell script requiring a GPU in a non-interactive way, use

    sbatch -p gpu -c1 -G1 --mem=16G SCRIPT_PATH
    

    If you want to run a CPU-only computation, remove the -p gpu and -G1 from the above commands.

Git

  • Is it possible to keep the solutions in a Git repository?

    Definitely. Keeping the solutions in a branch of your repository, where you merge them with the course repository, is probably a good idea. However, please keep the cloned repository with your solutions private.

  • On GitHub, do not create a public fork with your solutions

    If you keep your solutions in a GitHub repository, please do not create a clone of the repository by using the Fork button – this way, the cloned repository would be public.

    Of course, if you just want to create a pull request, GitHub requires a public fork and that is fine – just do not store your solutions in it.

  • How to clone the course repository?

    To clone the course repository, run

    git clone https://github.com/ufal/npfl138
    

    This creates the repository in the npfl138 subdirectory; if you want a different name, add it as a last parameter.

    To update the repository, run git pull inside the repository directory.

  • How to keep the course repository as a branch in your repository?

    If you want to store the course repository just in a local branch of your existing repository, you can run the following command while in it:

    git remote add course_repo https://github.com/ufal/npfl138
    git fetch course_repo
    git checkout --track course_repo/master -b BRANCH_NAME
    

    This creates a branch BRANCH_NAME, and when you run git pull in that branch, it will be updated to the current state of the course repository.

  • How to merge the course repository updates with your modified branch?

    If you want to store your solutions in your branch and gradually update this branch to track the changes in the course repository, you should start by

    git remote add course_repo https://github.com/ufal/npfl138
    git fetch course_repo
    git checkout --no-track course_repo/master -b BRANCH_NAME
    

    which creates a branch BRANCH_NAME with the current state of the course repository. However, unlike to the previous case, git pull and git push in this branch will not operate on the course repository. Therefore, you can then commit to this branch and push it to your own repository.

    To update your branch with the changes from the course repository, run

    git fetch course_repo
    git merge course_repo/master
    

    while in your branch. Of course, it might be necessary to resolve conflicts if both you and I modified the same lines in the templates.

ReCodEx

  • What files can be submitted to ReCodEx?

    You can submit multiple files of any type to ReCodEx. There is a limit of 20 files per submission, with a total size of 20MB.

  • What file does ReCodEx execute and what arguments does it use?

    Exactly one file with py suffix must contain a line starting with def main(. Such a file is imported by ReCodEx and the main method is executed (during the import, __name__ == "__recodex__").

    The file must also export an argument parser called parser. ReCodEx uses its arguments and default values, but it overwrites some of the arguments depending on the test being executed – the template should always indicate which arguments are set by ReCodEx and which are left intact.

  • What are the time and memory limits?

    The memory limit during evaluation is 1.5GB. The time limit varies, but it should be at least 10 seconds and at least twice the running time of my solution.

TensorBoard

  • Should TensorFlow be installed when using TensorBoard?

    When TensorBoard starts, it warns about a reduced feature set because of missing TensorFlow, notably

    TensorFlow installation not found - running with reduced feature set.
    

    Do not worry about the warning, there is no need to install TensorFlow.

  • Cannot start TensorBoard after installation

    If you cannot run the tensorboard command after installation, it is most likely not in your PATH. You can either:

    • start tensorboard using python3 -m tensorboard.main --logdir logs, or
    • add the directory with pip installed packages to your PATH (that directory is either bin/Scripts in your virtual environment if you use a virtual environment, or it should be ~/.local/bin on Linux and %UserProfile%\AppData\Roaming\Python\Python311 and %UserProfile%\AppData\Roaming\Python\Python311\Scripts on Windows).
  • What can be logged in TensorBoard? See the documentation of the SummaryWriter. Common possibilities are:

    • scalar values:
      summary_writer.add_scalar(name like "train/loss", value, step)
      
    • tensor values displayed as histograms or distributions:
      summary_writer.add_histogram(name like "train/output_layer", tensor, step)
      
    • images as tensors with shape [num_images, h, w, channels], where channels can be 1 (grayscale), 2 (grayscale + alpha), 3 (RGB), 4 (RGBA):
      summary_writer.add_images(name like "train/samples", images, step, dataformats="NHWC")
      
      Other dataformats are "HWC" (shape [h, w, channels]), "HW", "NCHW", "CHW".
    • possibly large amount of text (e.g., all hyperparameter values, sample translations in MT, …) in Markdown format:
      summary_writer.add_text(name like "hyperparameters", markdown, step)
      
    • audio as tensors with shape [1, samples] and values in [1,1][-1,1] range:
      summary_writer.add_audio(name like "train/samples", clip, step, [sample_rate])
      
    • traced modules using:
      summary_writer.add_graph(module, example_input_batch)
      

Requirements

To pass the practicals, you need to obtain at least 80 points, excluding the bonus points. Note that all surplus points (both bonus and non-bonus) will be transfered to the exam. In total, assignments for at least 120 points (not including the bonus points) will be available, and if you solve all the assignments (any non-zero amount of points counts as solved), you automatically pass the exam with grade 1.

To pass the exam, you need to obtain at least 60, 75, or 90 points out of 100-point exam to receive a grade 3, 2, or 1, respectively. The exam consists of 100-point-worth questions from the list below (the questions are randomly generated, but in such a way that there is at least one question from every but the first lecture). In addition, you can get surplus points from the practicals and at most 10 points for community work (i.e., fixing slides or reporting issues) – but only the points you already have at the time of the exam count. You can take the exam without passing the practicals first.

Exam Questions

Lecture 1 Questions

  • Considering a neural network with DD input neurons, a single hidden layer with HH neurons, KK output neurons, hidden activation ff and output activation aa, list its parameters (including their size) and write down how the output is computed. [5]

  • List the definitions of frequently used MLP output layer activations (the ones producing parameters of a Bernoulli distribution and a categorical distribution). Then write down three commonly used hidden layer activations (sigmoid, tanh, ReLU). [5]

  • Formulate the Universal approximation theorem. [5]

Lecture 2 Questions

  • Define maximum likelihood estimation, and show that it is equal to minimizing NLL, minimizing cross-entropy, and minimizing KL divergence. [10]

  • Define mean squared error, show how it can be derived using MLE (define pmodelp_{\textrm{model}}, show how MLE looks using pmodelp_{\textrm{model}}, and prove that the maximum likelihood estimate is equal to minimizing MSE). [5]

  • Describe gradient descent and compare it to stochastic (i.e., online) gradient descent and minibatch stochastic gradient descent. [5]

  • Formulate conditions on the sequence of learning rates used in SGD to converge to optimum almost surely. [5]

  • Write down the backpropagation algorithm. [5]

  • Write down the mini-batch SGD algorithm with momentum. Then, formulate SGD with Nesterov momentum and show the difference between them. [5]

  • Write down the AdaGrad algorithm and show that it tends to internally decay learning rate by a factor of 1/t1/\sqrt{t} in step tt. Then write down the RMSProp algorithm and explain how it solves the problem with the involuntary learning rate decay. [10]

  • Write down the Adam algorithm. Then show why the bias-correction terms (1βt)(1-\beta^t) make the estimation of the first and second moment unbiased. [10]

Lecture 3 Questions

  • Considering a neural network with DD input neurons, a single ReLU hidden layer with HH units and softmax output layer with KK units, write down the explicit formulas (i.e., without differential operators) of the gradient of all the MLP parameters (two weight matrices and two bias vectors), assuming input x\boldsymbol x, target gg and negative log likelihood loss. [10]

  • Assume a network with MSE loss generated a single output oRo \in \mathbb{R}, and the target output is gg. What is the value of the loss function itself, and what is the explicit formula (i.e., without a differential operator) of the gradient of the loss function with respect to oo? [5]

  • Assume a binary-classification network with cross-entropy loss generated a single output zRz \in \mathbb{R}, which is passed through the sigmoid output activation function, producing o=σ(z)o = \sigma(z). If the target output is gg, what is the value of the loss function itself, and what is the explicit formula (i.e., without a differential operator) of the gradient of the loss function with respect to zz? [5]

  • Assume a KK-class-classification network with cross-entropy loss generated a KK-element output zRK\boldsymbol z \in \mathbb{R}^K, which is passed through the softmax output activation function, producing o=softmax(z)\boldsymbol o=\operatorname{softmax}(\boldsymbol z). If the target distribution is g\boldsymbol g, what is the value of the loss function itself, and what is the explicit formula (i.e., without a differential operator) of the gradient of the loss function with respect to z\boldsymbol z? [5]

  • Define L2L_2 regularization and describe its effect both on the value of the loss function and on the value of the loss function gradient. [5]

  • Describe the dropout method and write down exactly how it is used during training and during inference. [5]

  • Describe how label smoothing works for cross-entropy loss, both for sigmoid and softmax activations. [5]

  • How are weights and biases initialized using the Glorot initialization? [5]