In this section, we will cover the fundamental operations you can perform on tensors in PyTorch. Tensors are the core data structure in PyTorch, similar to arrays in NumPy but with additional capabilities for GPU acceleration. Understanding how to manipulate tensors is crucial for building and training neural networks.

Key Concepts

  1. Creating Tensors
  2. Basic Tensor Operations
  3. Tensor Indexing and Slicing
  4. Tensor Reshaping
  5. Mathematical Operations on Tensors

  1. Creating Tensors

You can create tensors in several ways in PyTorch:

From Lists or NumPy Arrays

import torch
import numpy as np

# From a list
tensor_from_list = torch.tensor([1, 2, 3, 4])
print(tensor_from_list)

# From a NumPy array
np_array = np.array([1, 2, 3, 4])
tensor_from_np = torch.tensor(np_array)
print(tensor_from_np)

Using Built-in Functions

# Creating a tensor of zeros
zeros_tensor = torch.zeros((2, 3))
print(zeros_tensor)

# Creating a tensor of ones
ones_tensor = torch.ones((2, 3))
print(ones_tensor)

# Creating a tensor with random values
random_tensor = torch.rand((2, 3))
print(random_tensor)

  1. Basic Tensor Operations

Addition and Subtraction

a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])

# Addition
c = a + b
print(c)

# Subtraction
d = a - b
print(d)

Multiplication and Division

# Element-wise multiplication
e = a * b
print(e)

# Element-wise division
f = a / b
print(f)

Matrix Multiplication

# Matrix multiplication
matrix_a = torch.tensor([[1, 2], [3, 4]])
matrix_b = torch.tensor([[5, 6], [7, 8]])

matrix_c = torch.matmul(matrix_a, matrix_b)
print(matrix_c)

  1. Tensor Indexing and Slicing

Indexing

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

# Accessing a single element
print(tensor[0, 0])  # Output: 1

# Accessing a row
print(tensor[1])  # Output: tensor([4, 5, 6])

Slicing

# Slicing a sub-tensor
sub_tensor = tensor[0:2, 1:3]
print(sub_tensor)  # Output: tensor([[2, 3], [5, 6]])

  1. Tensor Reshaping

Reshape and View

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

# Reshaping the tensor
reshaped_tensor = tensor.view(3, 2)
print(reshaped_tensor)

# Another way to reshape
reshaped_tensor = tensor.reshape(3, 2)
print(reshaped_tensor)

Transpose

# Transposing the tensor
transposed_tensor = tensor.t()
print(transposed_tensor)

  1. Mathematical Operations on Tensors

Basic Math Functions

tensor = torch.tensor([1.0, 2.0, 3.0])

# Square root
sqrt_tensor = torch.sqrt(tensor)
print(sqrt_tensor)

# Exponential
exp_tensor = torch.exp(tensor)
print(exp_tensor)

# Logarithm
log_tensor = torch.log(tensor)
print(log_tensor)

Aggregation Functions

tensor = torch.tensor([1.0, 2.0, 3.0])

# Sum
sum_tensor = torch.sum(tensor)
print(sum_tensor)

# Mean
mean_tensor = torch.mean(tensor)
print(mean_tensor)

Practical Exercises

Exercise 1: Create and Manipulate Tensors

  1. Create a tensor from a list of numbers [10, 20, 30, 40, 50].
  2. Reshape the tensor into a 5x1 matrix.
  3. Perform element-wise multiplication with another tensor of ones with the same shape.
  4. Calculate the sum of all elements in the resulting tensor.

Solution

# Step 1: Create a tensor from a list
tensor = torch.tensor([10, 20, 30, 40, 50])
print(tensor)

# Step 2: Reshape the tensor into a 5x1 matrix
reshaped_tensor = tensor.view(5, 1)
print(reshaped_tensor)

# Step 3: Perform element-wise multiplication
ones_tensor = torch.ones((5, 1))
result_tensor = reshaped_tensor * ones_tensor
print(result_tensor)

# Step 4: Calculate the sum of all elements
sum_result = torch.sum(result_tensor)
print(sum_result)

Exercise 2: Tensor Indexing and Slicing

  1. Create a 3x3 tensor with values from 1 to 9.
  2. Extract the second row.
  3. Extract the first two columns of the tensor.

Solution

# Step 1: Create a 3x3 tensor
tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(tensor)

# Step 2: Extract the second row
second_row = tensor[1]
print(second_row)

# Step 3: Extract the first two columns
first_two_columns = tensor[:, 0:2]
print(first_two_columns)

Conclusion

In this section, we covered the basics of tensor operations in PyTorch, including creating tensors, performing basic operations, indexing and slicing, reshaping, and applying mathematical functions. These fundamental skills are essential for working with PyTorch and building neural networks. In the next module, we will dive into building neural networks from scratch.

© Copyright 2024. All rights reserved