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.

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.

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)
  """

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?

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?

Sample solution:

One solution consists of:

  1. change the shape of new_sample for (1, 25)
  2. 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?

Sample solution:

One solution consists of:

  1. Creating a tensor of ones of size (4, 1)
  2. use torch.cat to concatenate on dimension 1
ones = torch.ones(size=(4, 1))
solution = torch.cat(tensors=(t1, ones), dim=1)