Function 3 – expand(*size)

  • Returns a new view of the self tensor with singleton dimensions expanded to a larger size.
  • Passing -1 as the size for a dimension means not changing the size of that dimension.
  • Tensor can be also expanded to a larger number of dimensions, and the new ones will be appended at the front. For the new dimensions, the size cannot be set to -1.
  • Expanding a tensor does not allocate new memory, but only creates a new view on the existing tensor where a dimension of size one is expanded to a larger size by setting the stride to 0. Any dimension of size 1 can be expanded to an arbitrary value without allocating new memory.

Syntax and parameter: 

expand(*size)

Example 1 :
if we have a singleton-dimension matrix then we can expand it’s singleton-dimension, using expand()

Python3




a = torch.tensor([[5], [3], [8], [4]])
 
a.size()
 
a.expand(4, 8)


lool at tensor a,

tensor([[5, 5, 5, 5, 5, 5, 5, 5],

       [3, 3, 3, 3, 3, 3, 3, 3],

       [8, 8, 8, 8, 8, 8, 8, 8],

       [4, 4, 4, 4, 4, 4, 4, 4]])

Initially, a was a matrix that contains a singleton dimension, now using expand() we expanded it’s singleton dimension into 8

Example 2:

Python3




b = torch.tensor([[7], [3], [4]])
 
b.size()
 
b.expand(3, -1)


In the above example, we put the second size as -1 which means we don’t want to expand it’s dimension.

Example 3(common mistake) :

Python3




c = torch.tensor([[4, 6], [9, 6]])
 
c.size()  # this matrix didn't any singleton dimension
 
c.expand(2, 6)


This throws an error because according to the definition of expand() we have to match one size to the non-singleton dimension but as we have two singleton dimensions so we can’t fulfill the condition. Error….

RuntimeError: The expanded size of the tensor (6) must match the existing size (2) at non-singleton dimension 1. Target sizes: [2, 6]. Tensor sizes: [2,  2]

Pytorch Functions – tensor(), fill_diagnol(), append(), index_copy()

This article aims to share some PyTorch functions that will help you a lot in your deep learning and data science journey. Each function will be explained using two write examples and one example where you can’t use those functions. So let’s get started.

PyTorch is an open-source machine learning library, it contains a tensor library that enables to create a scalar, a vector, a matrix or in short we can create an n-dimensional matrix. It is used in computer vision and natural language processing, primarily developed by Facebook’s Research Lab. It is open-source software and released under the modified BSD (Barkley Software Distribution) license.

Our Four functions: 

  • torch.tensor()
  • fill_diagonal_()
  • append(*size)
  • index_copy()

Similar Reads

Function 1 – torch.tensor()

This function enables us to create PyTorch tensors. Tensor could be anything i.e. it could be a scalar, it could be a 1-dimensional matrix, it could be an n-dimensional matrix....

Function 2 – fill_diagonal_()

...

Function 3 – expand(*size)

...

Function 4 – index_copy_(dim, index, tensor) ? Tensor

...