Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
5 Common and Useful Mathematical Operations Using PyTorch
Python Torch package contains data structures for multi-dimensional tensors and mathematical operations over these are defined. It has a CUDA(Compute Unified Device Architecture) counterpart that allows users to run tensor computations using the power of the GPU with compute capability >= 3.0.
Here, we introduce some common but powerful Mathematical functions that are available in the Torch package.
- torch.std()
- torch.sin()
- torch.round()
- torch.unique()
- torch.sort()
Before we begin, let's install and import PyTorch
!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.htm
Looking in links: https://download.pytorch.org/whl/torch_stable.html
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (1.18.5)
Requirement already satisfied: torch==1.7.0+cpu in /usr/local/lib/python3.6/dist-packages (1.7.0+cpu)
Requirement already satisfied: torchvision==0.8.1+cpu in /usr/local/lib/python3.6/dist-packages (0.8.1+cpu)
Requirement already satisfied: torchaudio==0.7.0 in /usr/local/lib/python3.6/dist-packages (0.7.0)
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.6/dist-packages (from torch==1.7.0+cpu) (3.7.4.3)
Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from torch==1.7.0+cpu) (0.16.0)
Requirement already satisfied: dataclasses in /usr/local/lib/python3.6/dist-packages (from torch==1.7.0+cpu) (0.8)
Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.6/dist-packages (from torchvision==0.8.1+cpu) (7.0.0)
# Import torch and other required modules
import torch
Function 1 - torch.std
torch.std(input, unbiased=True) → Tensor
- Returns the standard deviation of all the elements in the 'input' tensor.
- If 'unbiased' is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.
Parameters
- input(Tensor) - the input tensor
- unbiased(bool) - whether to use the unbiased estimation or not
# Example 1 - working
a = torch.randn(1, 3)
torch.std(a)
tensor(0.0840)