Chapter 6: Understanding Tensors#

6.1 Introduction to Tensors#

  • Definition: Tensors are multidimensional arrays that generalize scalars (0D), vectors (1D), and matrices (2D) to higher dimensions. They serve as the fundamental building blocks in machine learning, particularly for deep learning frameworks.

  • Role in Machine Learning:

    • Tensors represent data inputs (e.g., images, text).

    • They enable efficient computation using GPU acceleration.

    • Commonly used in libraries like TensorFlow and PyTorch.

6.2 Tensor Dimensions#

  • 0D Tensor: Scalar (e.g., 5)

  • 1D Tensor: Vector (e.g., [1, 2, 3])

  • 2D Tensor: Matrix (e.g., [[1, 2], [3, 4]])

  • 3D+ Tensor: Higher-dimensional arrays (e.g., batches of images).

6.3 Practical Code Example: Tensor Operations#

import numpy as np
import tensorflow as tf

### Using NumPy for Tensor Operations
# Create a 2D tensor (matrix)
np_tensor = np.array([[1, 2, 3], [4, 5, 6]])
print("NumPy Tensor:\n", np_tensor)

# Perform basic operations
print("Addition:\n", np_tensor + 2)
print("Multiplication:\n", np_tensor * 2)

# Reshape the tensor
reshaped_np_tensor = np_tensor.reshape(3, 2)
print("Reshaped NumPy Tensor:\n", reshaped_np_tensor)

### Using TensorFlow for Tensor Operations
# Create a TensorFlow tensor
tf_tensor = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)
print("\nTensorFlow Tensor:\n", tf_tensor)

# Perform basic operations
tf_add = tf_tensor + 2
tf_multiply = tf_tensor * 2

print("TensorFlow Addition:\n", tf_add)
print("TensorFlow Multiplication:\n", tf_multiply)

# Reshape the tensor
reshaped_tf_tensor = tf.reshape(tf_tensor, [3, 2])
print("Reshaped TensorFlow Tensor:\n", reshaped_tf_tensor)
NumPy Tensor:
 [[1 2 3]
 [4 5 6]]
Addition:
 [[3 4 5]
 [6 7 8]]
Multiplication:
 [[ 2  4  6]
 [ 8 10 12]]
Reshaped NumPy Tensor:
 [[1 2]
 [3 4]
 [5 6]]

TensorFlow Tensor:
 tf.Tensor(
[[1. 2. 3.]
 [4. 5. 6.]], shape=(2, 3), dtype=float32)
TensorFlow Addition:
 tf.Tensor(
[[3. 4. 5.]
 [6. 7. 8.]], shape=(2, 3), dtype=float32)
TensorFlow Multiplication:
 tf.Tensor(
[[ 2.  4.  6.]
 [ 8. 10. 12.]], shape=(2, 3), dtype=float32)
Reshaped TensorFlow Tensor:
 tf.Tensor(
[[1. 2.]
 [3. 4.]
 [5. 6.]], shape=(3, 2), dtype=float32)

6.4 Summary#

  • Tensors are fundamental in ML, enabling the representation of complex data structures.

  • NumPy provides easy manipulation of tensors for general-purpose computation.

  • TensorFlow leverages tensors for deep learning tasks, offering powerful GPU-accelerated operations.