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

5 Most useful PyTorch Tensor Methods every beginner must know.

An short introduction about PyTorch and about the chosen functions.

  • arange -> Returns a 1-D tensor with values [start, end] with a [step] difference.

  • full -> Creates a tensor of size size filled with fill_value.

  • take -> Returns a new tensor with the elements of input at the given indices.

  • std_mean -> Returns standard deviation and mean of input tensor

  • pow -> Takes the power of each element in input with exponent and returns a tensor with the result.

# Install PyTorch and other dependencies
!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
Looking in links: https://download.pytorch.org/whl/torch_stable.html Requirement already satisfied: numpy in /opt/conda/lib/python3.8/site-packages (1.19.2) Requirement already satisfied: torch==1.7.0+cpu in /opt/conda/lib/python3.8/site-packages (1.7.0+cpu) Requirement already satisfied: torchvision==0.8.1+cpu in /opt/conda/lib/python3.8/site-packages (0.8.1+cpu) Requirement already satisfied: torchaudio==0.7.0 in /opt/conda/lib/python3.8/site-packages (0.7.0) Requirement already satisfied: dataclasses in /opt/conda/lib/python3.8/site-packages (from torch==1.7.0+cpu) (0.6) Requirement already satisfied: typing-extensions in /opt/conda/lib/python3.8/site-packages (from torch==1.7.0+cpu) (3.7.4.3) Requirement already satisfied: future in /opt/conda/lib/python3.8/site-packages (from torch==1.7.0+cpu) (0.18.2) Requirement already satisfied: pillow>=4.1.1 in /opt/conda/lib/python3.8/site-packages (from torchvision==0.8.1+cpu) (8.0.0)
# Import torch and other required modules
import torch

Function 1 - torch.arange

This function returns a 1-D Vector with values starting from [start] interval and ending with [end] interval excluded.
(with a step value if provided)

# Example 1 
t1 = torch.arange(10)
print(t1)
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])