The 5 basic Pytorch tensor operations!
Introduction To Pytorch
Pytorch is an open source machine learning or deep learning library. It is a scientific computing package based on Python that facilitate the development of deep learning projects. PyTorch is flexible as it supports dunamic computation graphs that allows the change of network behaviour on the fly.
Tensors are the most basic building block of any Deep Learning library. They are multidimensional arrays similar in function and properties to Numpy arrays. It can be in the form of 0-dimension (scalar), 1-dimension (vector) and 2-dimension (matrix).
The types of tensors in PyTorch includes:
- FloatTensor: 32-bit float
- DoubleTensor: 64-bit float
- HalfTensor: 16-bit float
- IntTensor: 32-bit int
- LongTensor: 64-bit int
The 5 tensor operation functions that we are going to look at are:
- torch.sigmoid
- torch.argmax
- torch.sort
- torch.topk
- torch.mul
# Import torch and other required modules
import torch
Function 1 - torch.sigmoid
This function takes an input tensor and returns a new tensor with the sigmoid of the elements of input.
# Get the sigmoid value of the inputs
x = torch.randn(4)
torch.sigmoid(x)
tensor([0.0385, 0.6693, 0.6473, 0.8204])
Create 1-d tensor with random numbers and get the sigmoid for each number.