Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
5 Basic Methametical Operations Using Pytorch
Pytorch is an open source machine learning libary of python. It is based on tensors. It is very commonly used for Natural Language Processing. Tensors are any number , metrics, vector or ndimentional array. A Pytorch Tensor is identical to NumPy array and PyTorch Provides many functions to operate those arrays.
Here we will be focusing on 5 Methametical Operations achievable using PyTorch on Tensors.
- abs
- add
- div
- exp
- trunc
If you dont have PyTorch installed then follow below steps to 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.abs
This computes absolute value of each element in input.
out = |input|
# Example 1 - Lets define a tensor a negative integer
t1 = torch.tensor(-100)