Learn practical skills, build real-world projects, and advance your career

Assignment 1 to play with some Tensor Operations

For the course Deep Learning with PyTorch: Zero to GANs

An short introduction about PyTorch and about the chosen functions.

  • function 1 repeat(*sizes) → Tensor: Repeats this tensor along the specified dimensions.
  • function 2 torch.prod(): Returns the product of all elements in the input tensor.
  • function 3 reshape_as(other) → Tensor: Returns this tensor as the same shape as other.
  • function 4 resize_(*sizes, memory_format=torch.contiguous_format) → Tensor: Resizes self tensor to the specified size.
  • function 5
# Import torch and other required modules
import torch

Function 1 - repeat(*sizes)

Repeats this tensor along the specified dimensions.

sizes (torch.Size or int...) – The number of times to repeat this tensor along each dimension

# Example 1 - working
# With a vector
x = torch.tensor([3., 4, 5, 6, 7])
x.repeat(2,3)
tensor([[3., 4., 5., 6., 7., 3., 4., 5., 6., 7., 3., 4., 5., 6., 7.],
        [3., 4., 5., 6., 7., 3., 4., 5., 6., 7., 3., 4., 5., 6., 7.]])

We can see that the tensor (vector) was repeated 2 times in the first (rows) and 3 times (columns)