Pytorch Tensors

Learn about Python tensor: what they are and how do we code them?

Before we can use PyTorch we need to import the Python torch module. Luckily, Educative’s code widget has PyTorch ready for us to use simply by importing it. There’s no need to go through a complicated installation process.

Press + to interact
import torch

Plain Python vs. PyTorch

A good way to start understanding PyTorch is to compare its basic units of information with plain Python.

Normal Python variables

In plain Python, we use variables to store numbers. We can use these variables like mathematical symbols to calculate new values and store these in new variables if we want to.

Have a look at the following plain Python code:

Press + to interact
# normal python variables
x = 3.5
y = x*x + 2
print(x, y)

We create a variable called x and give it a value 3.53.5. We then create a new variable called y and give it a value that is calculated from the expression ...