Chapter 6: Introduction to Tensors & Tensor Operations

by digitaltech2.com
Introduction to Tensors

Tensors are the fundamental building blocks in PyTorch. They are multi-dimensional arrays that are similar to NumPy arrays but with additional capabilities for GPU acceleration. Understanding tensors and their operations is crucial for working effectively with PyTorch.

What are Tensors?

Tensors are data structures that generalize matrices to higher dimensions. They can be thought of as n-dimensional arrays, where ‘n’ can be any non-negative integer.

0D Tensor (Scalar): A single value.

scalar = torch.tensor(5)
print(scalar)

1D Tensor (Vector): A one-dimensional array.

vector = torch.tensor([1, 2, 3])
print(vector)

2D Tensor (Matrix): A two-dimensional array.

matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(matrix)

3D Tensor: A three-dimensional array.

tensor_3d = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(tensor_3d)

Higher-Dimensional Tensors: Tensors can have more than three dimensions, useful for representing complex data structures.

Creating Tensors

Creating tensors in PyTorch can be done in several ways, ranging from direct initialization with specific values to using various functions for generating tensors with particular properties.

Creating Tensors from Data

You can create tensors directly from Python lists or NumPy arrays.

From a List:

import torch

tensor_from_list = torch.tensor([1, 2, 3, 4])
print(tensor_from_list)

From a NumPy Array:

import numpy as np

numpy_array = np.array([1, 2, 3, 4])
tensor_from_numpy = torch.tensor(numpy_array)
print(tensor_from_numpy)
Creating Tensors with Specific Values

PyTorch provides several functions to create tensors with specific values or properties.

Zeros Tensor:

zeros_tensor = torch.zeros((3, 3))
print(zeros_tensor)

Ones Tensor:

ones_tensor = torch.ones((2, 5))
print(ones_tensor)

Random Tensor:

random_tensor = torch.rand((4, 4))
print(random_tensor)

Tensor with Specific Data Type:

int_tensor = torch.tensor([1, 2, 3, 4], dtype=torch.int32)
print(int_tensor)

Tensor Operations

Tensor operations are at the core of deep learning tasks. PyTorch provides a wide range of operations that can be performed on tensors, including arithmetic operations, indexing, slicing, and more.

Basic Arithmetic Operations

You can perform standard arithmetic operations such as addition, subtraction, multiplication, and division directly on tensors.

Addition:

import torch

tensor_a = torch.tensor([1, 2, 3])
tensor_b = torch.tensor([4, 5, 6])
tensor_sum = tensor_a + tensor_b
print(tensor_sum)

Subtraction:

tensor_diff = tensor_a - tensor_b
print(tensor_diff)

Element-wise Multiplication:

tensor_prod = tensor_a * tensor_b
print(tensor_prod)

Division:

tensor_div = tensor_a / tensor_b
print(tensor_div)
Matrix Multiplication

Matrix multiplication is a fundamental operation in neural networks and linear algebra.

Matrix Multiplication:

matrix_a = torch.tensor([[1, 2], [3, 4]])
matrix_b = torch.tensor([[5, 6], [7, 8]])
matrix_mul = torch.matmul(matrix_a, matrix_b)
print(matrix_mul)
Aggregation Operations

Aggregation operations like sum, mean, and max are often used to reduce tensor dimensions.

Sum:

tensor_sum = torch.sum(tensor_a)
print(tensor_sum)

Mean:

tensor_mean = torch.mean(tensor_a.float())
print(tensor_mean)

Max:

tensor_max = torch.max(tensor_a)
print(tensor_max)

Related Posts