Introduction to Deep Learning with PyTorch Chapter 1: Introduction Chapter 2: Gradient Descent Chapter 3: PyTorch Tensors [3.1] Introduction [3.2] Installing PyTorch [3.3] Let's create tensors! [3.4] Quiz - Tensor Initialisation [3.5] Let's play with tensors! [3.6] Quick exercises [3.7] Useful tensor methods [3.8] Reshaping tensors [3.9] Concatenating tensors [3.10] Extracting sub-tensors [3.11] Quiz - Manipulating tensors Chapter 4: PyTorch for Automatic Gradient Descent Chapter 5: Training a Linear Model with PyTorch Chapter 6: Introduction to Deep Learning Chapter 7: Building and Training a simple Classification Model Chapter 8: Building and Training an AutoEncoder Chapter 9: Summary Chapter 3: PyTorch Tensors Quiz - Manipulating tensors face Luca Grillotti Here are a few questions for you to test whether you are comfortable manipulating tensors. 1 2 3 4 5 ❮ ❯ Question 1 Suppose we define two tensors t1 and t2 as follows t1 = torch.randn(size=(2, 3, 4, 9)) t2 = t1.view(6, -1, 3) What is the value of t2.size()[1]? (size of t2 for dimension 0). Write Error if you think this code is not valid. Answer 12 Explanation: when the value -1 is specified in view, it is automatically replaced by PyTorch so that: the total number of elements in the tensor remains the same. if we write x the value corresponding to t2.size()[1], then x satisfies the following equality: 2*3*4*9=6*x*3. This is why you cannot put more than one -1 argument in view. Check answer! Question 2 Suppose you have a tensor t_images containing several images of size: 28x28 pixels. t_images is thus of size (N_batch, 1, 28, 28) Write a function flatten_images that convert any tensor of size (N_batch, 1, 28, 28) into a tensor containing flattened images (i.e. a tensor of size: (N_batch, 1 * 28 * 28)). def flatten_images(tensor_images): """ Args: tensor_images (Tensor): assumed to be of size (N_batch, 1, 28, 28) where: N_batch is unknown Returns: same tensor as given as input, but of size (N_batch, 1 * 28 * 28) """ Answer Toggle sample solutions Sample solution: Here is a possible implementation: def flatten_images(tensor_images): """ Args: tensor_images (Tensor): assumed to be of size (N_batch, 1, 28, 28) where: N_batch is unknown Returns: same tensor as given as input, but of size (N_batch, 1 * 28 * 28) """ return tensor_images.view(-1, 1 * 28 * 28) Question 3 You are given a random tensor of size (4, 2) t1 = torch.randn(size=(4, 2)) How, in one line of code, can you replace the element in the second row with zeros? Answer Toggle sample solutions Sample solution: Here is a possible implementation: row_index = 1 t1[row_index, :] = 0 Question 4 You are given a tensor tensor_samples with 19 sequences of length 25 (i.e. the tensor is of size (19, 25)). Suppose we want to add a new sample new_sample to tensor_samples. tensor_samples = torch.randn(size=(19, 25)) new_sample = torch.randn(size=(25,)) How should we proceed? Answer Toggle sample solutions Sample solution: One solution consists of: change the shape of new_sample for (1, 25) use torch.cat new_sample = new_sample.view(1, 25) solution = torch.cat(tensors=(tensor_samples, new_sample), dim=0) Question 5 You are given a tensor t1 equal to: torch.Tensor([[0, 1], [2, 3], [4, 5], [6, 7]]) We would like to add a column of 1s, to get: torch.Tensor([[0., 1., 1.], [2., 3., 1.], [4., 5., 1.], [6., 7., 1.]]) How should we proceed? Answer Toggle sample solutions Sample solution: One solution consists of: Creating a tensor of ones of size (4, 1) use torch.cat to concatenate on dimension 1 ones = torch.ones(size=(4, 1)) solution = torch.cat(tensors=(t1, ones), dim=1)