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

Title Here

An short introduction about PyTorch and about the chosen functions.

  • torch.expand
  • function 2
  • function 3
  • function 4
  • function 5

Before we begin, let's install and import PyTorch

# Import torch and other required modules
import torch

Function 1 - torch.expand

This method will expand the size of torch tensor according to the given argument.

# Example 1 - working
x = torch.tensor([4])
print(x)
print('size of x: {}'.format(x.size()))
print('type of x: {}'.format(x.dtype))


y = x.expand(3,2) # will results 3 rows and two columns of new tensor
print(y)
tensor([4]) size of x: torch.Size([1]) type of x: torch.int64 tensor([[4, 4], [4, 4], [4, 4]])

We can see that the size of zero dimension tensor has become [3,2] tensor because we use .expand to expand the shape of that tensor.