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

PyTorch Basics: Tensors & Gradients

# import torch
import torch

Tensors

At its core, PyTorch is a library for processing tensors. A tensor is a number, vector, matrix or any n-dimensional array. Let's create a tensor with a single number:

# Number
t1=torch.tensor(5.)
t1
# t1 = torch.tensor(4.)
# t1
tensor(5.)

4. is a shorthand for 4.0. It is used to indicate to Python (and PyTorch) that you want to create a floating point number. We can verify this by checking the dtype attribute of our tensor: