In this tutorial, we will continue our exploration of PyTorch by diving into tensor arithmetics. An essential part of working with PyTorch is understanding how to perform arithmetics operations with tensors, which are the fundamental building blocks of PyTorch. By mastering tensor arithmetics, you will be able to perform complex mathematical operations necessary for building machine learning models and conducting data analysis.
Before we begin, make sure you have PyTorch installed. If you haven’t already installed PyTorch, you can do so by following the instructions on the official PyTorch website.
Creating Tensors
Before we can perform arithmetics operations with tensors, we first need to create tensors. In PyTorch, tensors are similar to NumPy arrays and can be thought of as multi-dimensional arrays. Let’s start by creating some tensors:
import torch
# Create a 1D tensor (vector)
a = torch.tensor([1, 2, 3])
print(a)
# Create a 2D tensor (matrix)
b = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(b)
# Create a 3D tensor
c = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(c)
In the code above, we have created three tensors of different dimensions: a 1D tensor a
, a 2D tensor b
, and a 3D tensor c
. You can create tensors with any number of dimensions by passing a list of values to the torch.tensor()
function.
Arithmetic Operations
Now that we have created some tensors, let’s explore various arithmetic operations that we can perform with tensors. PyTorch supports a wide range of arithmetic operations such as addition, subtraction, multiplication, division, and more. Here are some examples:
# Addition
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a + b
print(c) # Output: tensor([5, 7, 9])
# Subtraction
d = a - b
print(d) # Output: tensor([-3, -3, -3])
# Element-wise multiplication
e = a * b
print(e) # Output: tensor([4, 10, 18])
# Element-wise division
f = b / a
print(f) # Output: tensor([4., 2.5, 2.])
# Matrix multiplication
g = torch.tensor([[1, 2], [3, 4]])
h = torch.tensor([[5, 6], [7, 8]])
i = torch.matmul(g, h)
print(i) # Output: tensor([[19, 22], [43, 50]])
In the code above, we have demonstrated different arithmetic operations that can be performed with tensors. We have performed addition, subtraction, element-wise multiplication, element-wise division, and matrix multiplication. It is essential to note that these operations are performed element-wise by default, except for matrix multiplication, which requires using the torch.matmul()
function.
In-Place Operations
PyTorch also supports in-place operations, which are operations that modify the tensor in place. These operations are denoted by ending the operation with an underscore (_
). Here are some examples of in-place operations:
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
# In-place addition
a.add_(b)
print(a) # Output: tensor([5, 7, 9])
# In-place subtraction
a.sub_(b)
print(a) # Output: tensor([1, 2, 3])
# In-place element-wise multiplication
a.mul_(b)
print(a) # Output: tensor([4, 10, 18])
# In-place element-wise division
a.div_(b)
print(a) # Output: tensor([1, 2, 3])
In the code above, we have demonstrated how to perform in-place operations such as in-place addition, subtraction, element-wise multiplication, and element-wise division using the postfix underscore (_
) notation.
Conclusion
In this tutorial, we have covered the basics of tensor arithmetics in PyTorch. We have explored how to create tensors of different dimensions and perform various arithmetic operations such as addition, subtraction, element-wise multiplication, element-wise division, and matrix multiplication. We have also learned about in-place operations that modify tensors in place. By mastering tensor arithmetics, you will be well-equipped to work with tensors and perform complex mathematical operations in PyTorch.