Skip to content

Commit 25bf62c

Browse files
committed
Updated individual documentation
1 parent 8694405 commit 25bf62c

File tree

1 file changed

+90
-3
lines changed

1 file changed

+90
-3
lines changed

doc/Johan_page.md

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,113 @@
1010

1111
### Dataset
1212

13-
The choices regarding the dataset were mostly done in conjunction with Jan (@hzavadil98) as we were both using the MNIST dataset. Jan had the idea to download the binary files and construct the images from those. The group decided collaboratively to make the package download the data once and store it for all of use to use. Hence, the individual implementations are fairly similar, at least for the two MNIST dataloaders. Were it not for these individual tasks, there would have been one dataloader class, initialised with two separate ranges for labels 0-3 and 4-9. However, individual dataloaders had to be created to comply with the exam description. For my implementation, the labels had to be mapped to a range starting at 0: $(4-9) \to (0,5)$ since the cross-entropy loss function in PyTorch expect this range.
13+
The choices regarding the dataset were mostly done in conjunction with Jan (@hzavadil98) as we were both using the MNIST dataset. Jan had the idea to download the binary files and construct the images from those. The group decided collaboratively to make the package download the data once and store it for all of us to use. Hence, the individual implementations are fairly similar, at least for the two MNIST dataloaders. Were it not for these individual tasks, there would have been one dataloader class, initialized with two separate ranges for labels 0-3 and 4-9. However, individual dataloaders had to be created to comply with the exam description. For my implementation, the labels had to be mapped to a range starting at 0: $(4-9) \to (0,5)$ since the cross-entropy loss function in PyTorch expect this range.
14+
15+
Dataset based on the PyTorch module ``Dataset``.
16+
17+
* ``__init__`` Initialize the dataset. Shifts the labels $(4-9) \to (0,5)$ to comply with the expectations of the cross-entropy loss function.
18+
* ``data_path``: Path to where MNIST is/should be stored.
19+
* ``sample_ids``: Array of indices specifying which samples to load.
20+
* ``train``: Train or test the model (boolean), default is False.
21+
* ``transform``: Transforms if any, default is None.
22+
* ``nr_channels``: Number of channels, default is 1.
23+
* ``__len__``: Return the length of dataloader.
24+
* ``__getitem__``: Return the item of the specified index. Read the binary file at the correct position in order to generate the sample. Parameters:
25+
* ``idx``: Index of the desired sample.
26+
27+
### Model
28+
29+
The model is a straightforward MLP that consists of 4 hidden layers, 77 neurons in each layer. The activation function is ReLU. The final output is logits.
30+
31+
The model inherits the basic class from PyTorch: ``nn.Module`` so that the only necessary methods are
32+
33+
* ``__init__``: Initialize the network with the following parameters:
34+
* ``ìmage_shape``: Shape of the image (channels, height, width).
35+
* ``num_classes``: Number of classes to predict. This controls the output dimension.
36+
* ``forward``: One forward pass of the model. Parameters:
37+
* ``x`` One batch of data.
38+
39+
For any batch size ``N`` an example would be:
40+
41+
* Grayscale MNIST picture have shape (1,28,28).
42+
* Input shape: (``N``,1,28,28)
43+
* First layer output shape: (``N``,77)
44+
* Second layer output shape: (``N``,77)
45+
* Third layer output shape: (``N``,77)
46+
* Fourth (final) layer output shape: (``N``, ``num_classes``)
47+
48+
### Metric
49+
50+
The precision metric is calculated as follows:
51+
52+
$$
53+
Precision = \frac{TP}{TP+FP},
54+
$$
55+
where $TP$ and $FP$ are the numbers of true and false positives respectively. Hence, precision is a measure of how often the model is correct whenever it predicts the target class.
56+
57+
It can be calculated in two ways:
58+
59+
* Macro-averaging: The precision is calculated for each class separately and then averaged over (default).
60+
* Micro-averaging: Find $TP$ and $FP$ for all the classes and calculate precision once with these values.
61+
62+
The precision metric is also subclass of the PyTorch ``nn.Module`` class. It has the following methods:
63+
64+
* ``__init__``: Class initialization. Creates the class variables ``y_true`` and ``y_pred`` which are used to calculate the metrics. Parameters are:
65+
* ``num_classes``: The number of classes.
66+
* ``macro_averaging``: Boolean flag that control how to average the precision. Default is false.
67+
* ``forward``: Appends the true and predicted values to the class variables. Parameters:
68+
* ``y_true``: Tensor with true values.
69+
* ``y_pred``: Tensor with predicted values.
70+
* ``_micro_avg_precision``: Computes the micro-averaged precision. Parameters:
71+
* ``y_true``: Tensor with true values.
72+
* ``y_pred``: Tensor with predicted values.
73+
* ``_macro_avg_precision``: Computes the macro-averaged precision. Parameters:
74+
* ``y_true``: Tensor with true values.
75+
* ``y_pred``: Tensor with predicted values.
76+
* ``__returnmetric__``: Return the micro/macro-averaged precision of the samples stored in the class variables ``y_true`` and ``y_pred``.
77+
* ``__reset__``: Resets the list of samples stored in the class variables ``y_true`` and ``y_pred`` so that it is empty.
1478

1579
## Experiences with running someone else's code
1680

81+
This was an interesting experience as things did not go exactly as expected. I was initially unable to run with my assigned dataset (USPS 0-6). I have never encountered this error before, but with the help of Erik (IT guy) and Christian, a conflict with the ``urllib`` package and the newest python version was identifies. Christian recognized a solution, and suggested a fix which solved my problem. Hence, communicating well with the author of the code I was trying to run proved essential when I encountered errors.
82+
1783
## Experiences having someone else to run my code
1884

85+
I have not heard anything from whoever ran my code, so I assume everything went well.
86+
1987
## I learned how to use these tools during this course
2088

89+
Coming from a non-machine-learning background where the coding routines are slightly different in the sense of code organization and the use of tools like "docker" and "WandB", the learning curve has been steep. I am grateful to have been given the chance to collaborate with skilled peers, from whom I have learned a lot.
90+
2191
### Git-stuff
2292

93+
My novel experience with git prior to this project consisted of mainly having a local repository where I pulled/pushed to a GitHub repo, using one branch only; version control for myself only. Here I learned to utilize the features of git when collaborating with others, which include:
94+
95+
* Operating with Issues and assignments of work division.
96+
* Work on separate branches and have merge-protection on the main branch.
97+
* Using pull requests where we had to review each other's code before merging into main.
98+
* Use GitHub actions to automate workflows like unit testing and documentation building.
99+
* Using tags and creating releases.
100+
* Making the repository function like a package and make it installable.
101+
* Writing clear and documented code and building documentations.
102+
23103
### WandB
24104

105+
It was insightful to learn the basics of using Weights and Biases to track the progress when training and evaluating models. I have used Tensorboard (slightly) prior to this, but WandB seems like a better option.
106+
25107
### Docker, Kubernetes and Springfield
26108

109+
Completely new to all of this. Spend quite some time trying to understand what docker and kubernetes is since we were supposed to run the experiment on the local cluster Springfield. There was a whole process setting everything up, making the ssh secrets and get everything to work. I have some prior experience with SSH protocols, but never used any container software, nor schedulers like kubernetes.
110+
27111
### Proper documentation
28112

113+
Writing good documentation is always necessary and having training in this was fruitful. Combining this with GitHub action and Sphinx made it far easier to have an updated version of the documentation readily available.
114+
29115
### Nice ways of testing code
30116

117+
The combination of having testing part of the GitHub action workflow and using the more advanced features of ``pytest`` (like parametrized testing) was new to be, a very nice thing to learn. It automated testing and made it significantly more easy to make sure that any code we pushed to the main branch was performing well, and did not lose any unintended functionality.
118+
31119
### UV
32120

33-
## General thoughts on collaboration
121+
I switched to UV as my package manager for this project, and it is VERY good. Really fast and versatile.
34122

35-
As someone new to this University and how the IT-setup is here, this project was very fruitful. The positive thing about working with peers that are highly skilled in the relevant field is that the learning curve is steep, and the take home for me was significant. The con is that I constantly felt behind, spend half the time just understanding the changes implemented by others and felt that my contributions to the overall project was less significant. However, working with skilled peers have boosted my understanding about how things work around here, especially git (more fancy commands that add, commit, push and pull) and docker, cluster operations, kubernetes and logging metrics on Weights and Biases.

0 commit comments

Comments
 (0)