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

Some of the functions used with torch module

Torch basics

Pytorch is a deep learning library similar to tensorflow. It can help in creating tensors as perfect as numpy and we can even pass through without noticing the difference. It has more of an easy use rather than tensorflow at its beginning stage. It is used by the help of the torch module in python.

Some of the functions used with torch that are mentioned here are:

  • arange()
  • sigmoid()
  • mul()
  • sort()
  • mean()
## Import torch and other required modules
import torch

Function 1:

torch.arange(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, required_grad=False)

arange function is used to create an evenly spaced floating point integers in a 1-D tensor
The values given with = specifies the default value of the particular part to be filled in the arange function.
The parameters inside the arange that we frequently use are start,end and step.

  • start- This is used to give a value to begin with
  • end- This is where the end of these values has to be specified
  • step- The equal spacing in between these values
tensor_val = torch.arange(-1.3,4.2,0.3)
tensor_val
tensor([-1.3000, -1.0000, -0.7000, -0.4000, -0.1000,  0.2000,  0.5000,  0.8000,
         1.1000,  1.4000,  1.7000,  2.0000,  2.3000,  2.6000,  2.9000,  3.2000,
         3.5000,  3.8000,  4.1000])

Floating point values can be provided in start, end and even step so that equal spacing could be provided in between the values
of start and end