In this chapter, we will explore the concept of tensors, which are fundamental data structures in deep learning and mathematical frameworks like TensorFlow and PyTorch. We will cover what tensors are, provide examples, and demonstrate how to work with tensors using Python’s NumPy library.
A tensor is a generalization of scalars, vectors, and matrices. It is a multi-dimensional array that can hold data of various types. Tensors have different ranks or orders, which correspond to the number of dimensions they have.
Let’s look at some examples to better understand tensors:
A scalar tensor is a single value. It has no dimensions.
import numpy as np
scalar_tensor = np.array(5) # Creating a scalar tensor with the value 5
print(scalar_tensor)A vector tensor is a one-dimensional array.
vector_tensor = np.array([1, 2, 3, 4, 5]) # Creating a vector tensor
print(vector_tensor)A matrix tensor is a two-dimensional array.
matrix_tensor = np.array([[1, 2, 3], [4, 5, 6]]) # Creating a matrix tensor
print(matrix_tensor)Higher-rank tensors have more dimensions. Here’s an example of a rank-3 tensor:
rank_3_tensor = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Creating a rank-3 tensor
print(rank_3_tensor)NumPy is a powerful library for numerical computing in Python. It provides efficient operations for working with tensors. You can perform various operations on tensors, such as addition, multiplication, and reshaping.
# Tensor addition
tensor_a = np.array([1, 2, 3])
tensor_b = np.array([4, 5, 6])
tensor_sum = tensor_a + tensor_b
print(tensor_sum)
# Tensor multiplication (element-wise)
tensor_product = tensor_a * tensor_b
print(tensor_product)
# Reshaping a tensor
matrix_tensor = np.array([[1, 2, 3], [4, 5, 6]])
reshaped_tensor = np.reshape(matrix_tensor, (3, 2)) # Reshape to a 3x2 matrix
print(reshaped_tensor)In this chapter, we have introduced tensors, explained their concept, provided examples of different tensor ranks, and demonstrated how to work with tensors using NumPy. Tensors are fundamental to deep learning, and understanding their properties and operations is crucial for building and training neural networks.