Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
Some Handy PyTorch Functions
I have compiled some PyTorch Functions which could come handy at times.
- rsqrt()
- topk()
- frac()
- eye()
- masked-select()
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.rsqrt()
This function returns a tensor which contains the reciprocal of the square root of each element in the input.
It takes the input as an argument and you can also specify the output vector.
# Example 1 - working
a = [1, 4, 9]
a = torch.Tensor(a)
torch.rsqrt(a)
tensor([1.0000, 0.5000, 0.3333])