Tensors

Note

To solve these exercises, consulting the torch function reference can be helpful.

Question 1: Tensor creation and manipulation

Recreate this torch tensor:

torch_tensor
 1  2  3
 4  5  6
[ CPULongType{2,3} ]
Hint First create an R matrix and then convert it using torch_tensor().

Next, create a view of the tensor so it looks like this:

torch_tensor
 1  2
 3  4
 5  6
[ CPULongType{3,2} ]
Hint Use the $view() method and pass the desired shape as a vector.

Check programmatically that you successfully created a view, and not a copy.

Hint See what happens when you modify one of the tensors.

Question 2: More complex reshaping

Consider the following tensor:

x <- torch_tensor(1:6)
x
torch_tensor
 1
 2
 3
 4
 5
 6
[ CPULongType{6} ]

Reshape it so it looks like this.

torch_tensor
 1  3  5
 2  4  6
[ CPULongType{2,3} ]
Hint First reshape to (2, 3) and then $permute() the two dimensions.

Question 3: Broadcasting

Consider the following vectors:

x1 <- torch_tensor(c(1, 2))
x1
torch_tensor
 1
 2
[ CPUFloatType{2} ]
x2 <- torch_tensor(c(3, 7))
x2
torch_tensor
 3
 7
[ CPUFloatType{2} ]

Predict the result (shape and values) of the following operation by applying the broadcasting rules.

x1 + x2$reshape(c(2, 1))

Question 4: Handling Singleton dimensions

A common operation in deep learning is to add or get rid of singleton dimensions, i.e., dimensions of size 1. As this is so common, torch offers a $squeeze() and $unsqueeze() method to add and remove singleton dimensions.

Use these two functions to first remove the second dimension and then add one in the first position.

x <- torch_randn(2, 1)
x
torch_tensor
-0.1115
 0.1204
[ CPUFloatType{2,1} ]

Question 5: Matrix multiplication

Generate a random matrix \(A\) of shape (10, 5) and a random matrix \(B\) of shape (10, 5) by sampling from a standard normal distribution.

Hint Use torch_randn(nrow, ncol) to generate random matrices.

Can you multiply these two matrices with each other and if so, in which order? If not, generate two random matrices with compatible shapes and multiply them.

Question 6: Uniform sampling

Generate 10 random variables from a uniform distribution (using only torch functions) in the interval \([10, 20]\). Use torch_rand() for this (which does not allow for min and max parameters).

Hint Add the lower bound and multiply with the width of the interval.

Then, calculate the mean of the values that are larger than 15.

Question 7: Don’t touch this

Consider the code below:

f <- function(x) {
  x[1] <- torch_tensor(-99)
  return(x)
}
x <- torch_tensor(1:3)
y <- f(x)
x
torch_tensor
-99
  2
  3
[ CPULongType{3} ]

Implement a new different version of this function that returns the same tensor but does not change the value of the input tensor in-place.

Hint The $clone() method might be helpful.