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

Putting some torch -light on Pytorch tensors

Pytorch is an open source machine learning library based on the Torch library.Here I am going to explain 5 differnt functions related to Pytorch tensors.

  • torch.randn
  • torch.add
  • torch.asin
  • torch.deg2rad
  • torch.fmod

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.randn

torch.randn(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).

The shape of the tensor is defined by the variable argument size.

size (int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.

# Example 1 - working
torch.randn(4)
tensor([ 0.4389,  0.4387,  0.0225, -0.5833])