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

5 Tensor functions

Torch is an open-source machine learning Python library used for deep learning implementations like computer vision (using TorchVision) and natural language processing. It was developed by Facebook’s AI research lab (FAIR) in 2016 and has since been adopted across the fields of data science and ML. We will discuss 5 torch functions which are mentioned below:-

  • 1 :- torch.arange
  • 2 :- torch.is_floating_point
  • 3 :- torch.reshape
  • 4 :- torch.max
  • 5 :- torch.dot

Before we begin, let's install and import PyTorch

# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
# Import torch and other required modules
import torch

Function 1 - torch.arange

Torch.arange function is used to create tensor of range described. It returns a 1-D tensor
​ with values from the interval [start, end) taken with common difference step beginning from start.

# Example 1-
a=torch.arange(1,10,0.5,dtype=torch.double)
a
tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000, 5.0000,
        5.5000, 6.0000, 6.5000, 7.0000, 7.5000, 8.0000, 8.5000, 9.0000, 9.5000],
       dtype=torch.float64)