PyTorch Fundamentals
Exercise 1
A big part of deep learning (and learning to code in general) is getting familiar with the documentation of a certain framework you're using. We'll be using the PyTorch documentation a lot throughout the rest of this course. So I'd recommend spending 10-minutes reading the following (it's okay if you don't get some things for now, the focus is not yet full understanding, it's awareness):
- The tensor datatype has a lot of methods and attributes available, will go back when needing more information
- Utilizing CUDA threads is a very good way to speed up the software, instead of running all operations on the CPU. On my current machine (MacBook Air M2) I do not have CUDA cores available, but will dive into this when I have my desktop PC running again from my office.
Exercise 2
Create a random tensor with shape
(7, 7)
.
Create this random tensor with torch.rand()
:
Exercise 3
Perform matrix multiplication on the tensor from Exercise 2 with another random tensor with shape
(1, 7)
To perform matrix multiplication we do this with torch.matmul
. But we have to transpose the second tensor
since the number of rows do not align with the number or columns in the first tensor. This is done with the
transpose()
before making the matrix multiplication:
Exercise 4
Set the random seed to
0
and do exercises 2 and 3 again
We are setting the random seed on the CPU with torch.random.manual_seed()
. Then we perform the same as in
exercises 2 and 3:
Exercise 5
Speaking of random seeds, we saw how to set it with
torch.manual_seed()
but is there a GPU equivalent? (hint: you'll need to look into the documentation fortorch.cuda
for this one)
Since I do not have CUDA cores available we skip this exercise for now. But you set the random seed
on one CUDA core with torch.cuda.manual_seed()
Exercise 6
Create two random tensors of shape
(2, 3)
and send them both to the GPU (you'll need access to a GPU for this). Settorch.manual_seed(1234)
when creating the tensors (this doesn't have to be the GPU random seed).
As in exercise 5, we do not have CUDA cores available. But we can check this with torch.cuda.is_available()
. So
I do this and sets the seed on the CUDA cores if they are available, if not I seed the CPU and do what is asked and
returning the tensors, since they are used in the next exercise:
Exercise 7
Perform a matrix multiplication on the tensors you created in exercise 6 (again, you may have to adjust the shapes of one of the tensors).
Since the number of rows in the second tensor not is matching the number of columns in the first tensor
I have to transpose again before doing the matrix multiplication with torch.matmul
. I also return the
matrix product since I have to use this in exercises 8 and 9:
Exercise 8
Find the maximum and minimum values of the output of exercise 7
This is done with the method max()
and min()
which is attributes
to the tensor class:
Exercise 9
Find the maximum and minimum index values of the output of exercise 7.
This is done with the argmin()
and argmax()
methods:
Exercise 10
Make a random tensor with shape
(1, 1, 1, 10)
and then create a new tensor with all the 1 dimensions removed to be left with a tensor of shape(10)
. Set the seed to 7 when you create it and print out the first tensor and it's shape as well as the second tensor and it's shape.
Once again I set the seed with torch.random.manual_seed()
and then I am defining the first multi dimensional tensor with the
size
argument. I then create a new tensor by selecting the first indices in the first tensor