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

Assignment 1 - All About torch.Tensor

PyTorch Library allows users to work efficiently with tensors.

I chose to look into and experiment with the below 5 functions of PyTorch as they allowed me clearly visualize and manipulate data among tensors.
even though I had no prior experience with Tensors.

  • select()
  • index_fill_()
  • put_()
  • repeat()
  • index_add_()
# Import torch and other required modules
import torch

Function 1 - tensor.select

Slices the tensor along the dimension at the given index ; and returns a new tensor.

select(dim, index)

# Example 1 - working (change this)
x = torch.tensor([[1, 2, 3, 4, 5], 
                  [2, 4, 6, 8, 10]])
y = x.select(1, 3)
print(y)
tensor([4, 8])

Explanation about example 1:

Here, the select() function divides the tensor x in the dimension as the column of the tensor and at index 3. Hence, this returns us a new 1 x 2 tensor.