No history yet

Introduction to PyTorch Tensors

The Building Blocks of PyTorch

In PyTorch, everything revolves around one central concept: the tensor. Think of tensors as the fundamental building blocks for all data and computations. If you've ever worked with a list of numbers or a grid in a spreadsheet, you're already familiar with the basic idea.

A tensor is a multi-dimensional array that is the fundamental data structure used in PyTorch (and many other machine learning frameworks).

At its core, a tensor is a container for numbers. What makes it special is its ability to have multiple dimensions. Let's break this down:

  • A single number, like 5, is a 0-dimensional tensor, also known as a scalar.
  • A list of numbers, like [1, 2, 3], is a 1-dimensional tensor, or a vector.
  • A grid of numbers, like a spreadsheet with rows and columns, is a 2-dimensional tensor, or a matrix.

You can keep adding dimensions. For example, a color image can be represented by a 3D tensor: one dimension for the image's height, one for its width, and one for the red, green, and blue (RGB) color channels.

Creating Your First Tensors

Creating a tensor in PyTorch is straightforward. You can convert a standard Python list into a tensor. Let's see how to create a 2D tensor (a matrix).

import torch

# Create a 2D tensor from a Python list
my_matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])

print(my_matrix)

Once you have a tensor, you can easily check its properties. The shape tells you the size of the tensor along each dimension. For our matrix, the shape is (2, 3) because it has 2 rows and 3 columns.

# Check the shape of the tensor
print(my_matrix.shape)

PyTorch also provides functions to create tensors of specific types, which is useful when you need placeholders for data.

# A 3x2 tensor filled with zeros
zeros_tensor = torch.zeros(3, 2)

# A 2x4 tensor filled with ones
ones_tensor = torch.ones(2, 4)

# A 2x2 tensor with random numbers between 0 and 1
random_tensor = torch.rand(2, 2)

print(zeros_tensor)
print(ones_tensor)
print(random_tensor)

Basic Tensor Math

You can perform standard mathematical operations on tensors. These operations are typically applied element-wise. This means the operation is performed on each element of the tensor individually. For this to work, the tensors usually need to have the same shape.

Let's create two tensors and add them together.

a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[10, 20], [30, 40]])

# Element-wise addition
sum_tensor = a + b

print(sum_tensor)

Each element in a was added to the corresponding element in b. The element at the first row and first column of a (which is 1) was added to the element at the first row and first column of b (which is 10) to get 11. The same happened for all other positions.

Multiplication works the same way.

# Element-wise multiplication
product_tensor = a * b

print(product_tensor)

Understanding tensors, their shapes, and how to perform basic operations is the first major step in mastering PyTorch. All the complex neural networks you'll encounter are built upon these simple, powerful data structures.

Quiz Questions 1/6

What is the fundamental building block for all data and computations in PyTorch?

Quiz Questions 2/6

A single number, like 42, is considered what kind of tensor?