Fundamental Tensor Operations in PyTorch
Lesson Overview
Hello there! In today's lesson, we delve deeper into PyTorch Tensors and their fundamental operations such as addition and multiplication. We will also learn about broadcasting, a powerful feature in PyTorch that makes it possible to perform operations between tensors of different shapes. Let's get started!
Introduction to Tensor Operations
In our previous lesson, we learned about PyTorch Tensors — the basic building blocks in PyTorch that hold data. They resemble arrays in NumPy but come with additional features that are essential for machine learning tasks like GPU acceleration. Now, we will learn the different operations that can be performed on tensors and better understand how these computations happen deep inside neural networks.
Fundamentally, tensor operations include arithmetical operations such as addition, subtraction, multiplication, and division. Two essential forms of multiplication in tensors are element-wise multiplication and matrix multiplication. We can also perform operations between a scalar and a tensor, or between tensors of different shapes — thanks to a technique called Broadcasting.
Tensor Addition
In tensor addition, we simply add the corresponding elements in the tensors. PyTorch provides the torch.add() function that takes two tensors as inputs and returns a new tensor which is the result of adding the input tensors. It is also as easy as using the + operator.
Let's go over an example:
The output of the above code will be:
This output shows the result of adding two tensors element-wise. Each element from tensor_a is added to the corresponding element in tensor_b, resulting in a new tensor of the same shape.
Element-wise and Matrix Multiplication
Next, let's learn about tensor multiplication. One type is element-wise multiplication where each element in one tensor is multiplied with the corresponding element in the other tensor. We can use the torch.mul() function or the * operator for this.
The other type is matrix multiplication. This is similar to the matrix multiplication in linear algebra where the dot product of rows and columns is calculated. For this, we use the torch.matmul() function.
Below is the Python code demonstrative of these operations:
The output of the above code will be:
These outputs demonstrate two different multiplication techniques. In element-wise multiplication, each element of tensor_a is multiplied with the corresponding element of tensor_b. In matrix multiplication, the dot product between tensor_a and tensor_c results in a new tensor shape based on matrix product rules.
