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

Tensors

Tensors are mathematical entity that can vagely be considered as a matrix. It can be a 0-D matrix(scalar), a 1-D matrix(a vector) or a 3-D matrix. All matrices can be considered as tensor but not all matrices can be a tensor. Pytorch is the machine learning library based on the torch package which is used for processing tensors.Rank in tensors represents the number of axes. In the case of a scalar, there are no axes and so rank is 0.

import torch
#Declaring scalar the tensor
t1 = torch.tensor(6.) 
t1
tensor(6.)

Here we use the torch.tensor() function for creating the scalar tensor. In the case of a scalar, there are no axes and so rank is 0. We used the '6.'(equivalent to 6.0) notation which indicate that we want to create a tensor with datatype torch.float32(). We can check the dataype of the tensor using the ".dtype function" we can the exection of the dtype function from the given example:-

t1.dtype
torch.float32