00. PyTorch Fundamentals Exercises¶
1. Documentation reading¶
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 documentation on
torch.Tensor
. - The documentation on
torch.cuda
.
In [1]:
Copied!
# No code solution (reading)
# No code solution (reading)
2. Create a random tensor with shape (7, 7)
.¶
In [2]:
Copied!
# Import torch
# Create random tensor
# Import torch
# Create random tensor
3. Perform a matrix multiplication on the tensor from 2 with another random tensor with shape (1, 7)
(hint: you may have to transpose the second tensor).¶
In [3]:
Copied!
# Create another random tensor
# Perform matrix multiplication
# Create another random tensor
# Perform matrix multiplication
4. Set the random seed to 0
and do 2 & 3 over again.¶
The output should be:
(tensor([[1.8542],
[1.9611],
[2.2884],
[3.0481],
[1.7067],
[2.5290],
[1.7989]]), torch.Size([7, 1]))
In [4]:
Copied!
# Set manual seed
# Create two random tensors
# Matrix multiply tensors
# Set manual seed
# Create two random tensors
# Matrix multiply tensors
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 for torch.cuda
for this one)¶
- If there is, set the GPU random seed to
1234
.
In [5]:
Copied!
# Set random seed on the GPU
# Set random seed on the GPU
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). Set torch.manual_seed(1234)
when creating the tensors (this doesn't have to be the GPU random seed). The output should be something like:¶
Device: cuda
(tensor([[0.0290, 0.4019, 0.2598],
[0.3666, 0.0583, 0.7006]], device='cuda:0'),
tensor([[0.0518, 0.4681, 0.6738],
[0.3315, 0.7837, 0.5631]], device='cuda:0'))
In [6]:
Copied!
# Set random seed
# Check for access to GPU
# Create two random tensors on GPU
# Set random seed
# Check for access to GPU
# Create two random tensors on GPU
7. Perform a matrix multiplication on the tensors you created in 6 (again, you may have to adjust the shapes of one of the tensors).¶
The output should look like:
(tensor([[0.3647, 0.4709],
[0.5184, 0.5617]], device='cuda:0'), torch.Size([2, 2]))
In [7]:
Copied!
# Perform matmul on tensor_A and tensor_B
# Perform matmul on tensor_A and tensor_B
8. Find the maximum and minimum values of the output of 7.¶
In [8]:
Copied!
# Find max
# Find min
# Find max
# Find min
9. Find the maximum and minimum index values of the output of 7.¶
In [9]:
Copied!
# Find arg max
# Find arg min
# Find arg max
# Find arg min
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.¶
The output should look like:
tensor([[[[0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297,
0.3653, 0.8513]]]]) torch.Size([1, 1, 1, 10])
tensor([0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297, 0.3653,
0.8513]) torch.Size([10])
In [10]:
Copied!
# Set seed
# Create random tensor
# Remove single dimensions
# Print out tensors and their shapes
# Set seed
# Create random tensor
# Remove single dimensions
# Print out tensors and their shapes