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

PyTorch

PyTorch is an open source machine learning library for Python and is completely based on Torch. The main advantages of pytorch are its python support, multiGPU support, dynamic computation graphs, custom data loaders and simplified preprocessors. Lets discuss about some interesting pytorch functions. They are

  • squeeze
  • split
  • amax
  • flip
  • transpose
  • take
  • trace
  • clamp
  • repeat_interleave
  • unbind
# Import torch and other required modules
import torch

Function 1 - TORCH.SQUEEZE

  • squeezing a tensor removes the dimensions or axes that have a length of one.
Syntax : torch.squeeze( input, dim)

Parameters :

  • input (Tensor) – the input tensor.
  • dim (int, optional) – if given, the input will be squeezed only in this dimension.
# Example 1 - working 

x = torch.zeros(2,1)
print(x)
print(x.size())
y = torch.squeeze(x,1)
print(y)
y.size()

tensor([[0.], [0.]]) torch.Size([2, 1]) tensor([0., 0.])
torch.Size([2])

Explanation : In this example a 2x1 matrix is created with all the elements are set to zero. Then squeeze function is called which leads to dimension reduction. Here column has dimension has 1, so it will be reduced.