Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
In this chapter, we will dive into the essential concept of
neural networks and how to build them using PyTorch. Neural networks are
at the core of deep learning, and understanding how to construct and train
these models is critical for any machine learning practitioner. We will begin
by covering the fundamental components of neural networks, followed by a step-by-step
guide to building a simple feedforward neural network for image classification.
The goal is to understand the theory behind neural networks while applying it
practically using PyTorch.
By the end of this chapter, you will have a solid
understanding of how to construct, train, and evaluate basic neural networks in
PyTorch. You will also be able to extend these concepts to more complex models
such as Convolutional Neural Networks (CNNs) and Recurrent Neural
Networks (RNNs).
3.1 Introduction to Neural Networks
A neural network is a collection of layers of
interconnected neurons (also known as artificial neurons or perceptrons). The
main objective of a neural network is to learn a mapping between the input and
output data by adjusting the weights through a process called training.
Neural networks can be visualized as a series of layers:
Each neuron in a layer is connected to neurons in the
subsequent layer by weights. During training, these weights are updated
through the process of backpropagation, which is powered by gradient descent.
Basic Terminology in Neural Networks:
Key Components of Neural Networks:
3.2 Building a Simple Feedforward Neural Network
In PyTorch, neural networks are constructed by subclassing torch.nn.Module
and defining the architecture in the __init__ and forward() methods.
Let’s begin by building a simple fully connected neural
network to classify images from the MNIST dataset (a dataset of
handwritten digits).
Step 1: Load and Preprocess the MNIST Dataset
Before building the model, we need to load the MNIST dataset
and preprocess it. We will use the torchvision library to load and transform
the data.
Code Sample:
import
torch
from
torch.utils.data import DataLoader
from
torchvision import datasets, transforms
#
Define data transformation (convert images to tensor and normalize)
transform
= transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
#
Download and load the MNIST training and test datasets
train_dataset
= datasets.MNIST('.', train=True, download=True, transform=transform)
test_dataset
= datasets.MNIST('.', train=False, download=True, transform=transform)
#
Load data into batches using DataLoader
train_loader
= DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader
= DataLoader(test_dataset, batch_size=64, shuffle=False)
Explanation:
Step 2: Define the Neural Network Architecture
Next, we will define the architecture of our neural network.
The network will have one hidden layer and one output layer.
Code Sample:
import
torch.nn as nn
import
torch.optim as optim
#
Define the neural network class
class
SimpleNN(nn.Module):
def __init__(self):
super(SimpleNN, self).__init__()
# Input layer (28*28 flattened) ->
Hidden layer (128 neurons) -> Output layer (10 classes)
self.fc1 = nn.Linear(28*28, 128)
self.fc2 = nn.Linear(128, 10) # 10 output units for 10 digits
def forward(self, x):
x = x.view(-1, 28*28) # Flatten the input (batch_size, 28, 28)
-> (batch_size, 28*28)
x = torch.relu(self.fc1(x)) # Apply ReLU activation to hidden layer
x = self.fc2(x) # Output layer (no activation function)
return x
#
Instantiate the model
model
= SimpleNN()
Explanation:
Step 3: Define the Loss Function and Optimizer
We will use CrossEntropyLoss for the loss function,
which is suitable for multi-class classification tasks, and Adam for
optimization.
Code Sample:
#
Define loss function and optimizer
criterion
= nn.CrossEntropyLoss() # Cross-entropy
loss for multi-class classification
optimizer
= optim.Adam(model.parameters(), lr=0.001)
# Adam optimizer with learning rate 0.001
Explanation:
Step 4: Training the Model
Now, we’ll train the model using the training data. In each
epoch, we will:
Code Sample:
#
Training loop
num_epochs
= 5
for
epoch in range(num_epochs):
model.train() # Set the model to training mode
running_loss = 0.0
for data, target in train_loader:
optimizer.zero_grad() # Zero the gradients from the previous step
output = model(data) # Forward pass
loss = criterion(output, target) # Calculate the loss
loss.backward() # Backpropagation
optimizer.step() # Update the model parameters
running_loss += loss.item()
print(f"Epoch {epoch+1}/{num_epochs},
Loss: {running_loss/len(train_loader)}")
Explanation:
Step 5: Evaluating the Model
After training, we evaluate the model using the test
dataset. The accuracy is calculated by comparing the predicted classes with the
actual labels.
Code Sample:
#
Evaluation loop
model.eval() # Set the model to evaluation mode
correct
= 0
total
= 0
with
torch.no_grad(): # Disable gradient
calculation for inference
for data, target in test_loader:
output = model(data) # Forward pass
_, predicted = torch.max(output, 1) # Get the predicted class
total += target.size(0) # Total number of samples
correct += (predicted == target).sum().item() # Count correct predictions
accuracy
= 100 * correct / total
print(f'Test
Accuracy: {accuracy:.2f}%')
Explanation:
3.3 Common PyTorch Functions for Neural Networks
Function |
Description |
Example |
nn.Linear(in_features,
out_features) |
Defines a fully
connected (dense) layer with specified input and output dimensions. |
nn.Linear(28*28, 128) |
torch.relu() |
Applies the
ReLU activation function element-wise. |
x =
torch.relu(x) |
optimizer.zero_grad() |
Clears old gradients,
necessary before a new backward pass. |
optimizer.zero_grad() |
loss.backward() |
Computes the
gradient of the loss with respect to the parameters. |
loss.backward() |
optimizer.step() |
Updates the model's
parameters based on computed gradients. |
optimizer.step() |
model.eval() |
Sets the
model to evaluation mode, disabling layers like dropout. |
model.eval() |
torch.no_grad() |
Disables gradient
calculation, useful for inference. |
with torch.no_grad(): |
3.4 Summary
In this chapter, we learned how to:
This foundational knowledge will allow you to extend your work to more advanced models like Convolutional Neural Networks (CNNs) and Recurrent Neural Networks (RNNs) in the upcoming chapters.
BackPyTorch is an open-source deep learning framework developed by Facebook’s AI Research lab (FAIR), known for its dynamic computation graph and flexibility.
PyTorch uses dynamic computation graphs, making it more flexible and easier to debug, while TensorFlow traditionally used static computation graphs, although TensorFlow 2.0 now supports dynamic graphs.
You can install PyTorch via pip with pip install torch torchvision torchaudio or through conda with conda install pytorch torchvision torchaudio cpuonly -c pytorch.
A tensor is a multi-dimensional array similar to a NumPy array but optimized for GPU acceleration, making it the core data structure in PyTorch.
autograd is PyTorch’s automatic differentiation system that computes gradients for backpropagation during training.
You can define a neural network by subclassing torch.nn.Module and defining the network architecture in the __init__ and forward methods.
Transfer learning involves using a pre-trained model on a large dataset and fine-tuning it for a specific task. In PyTorch, you can use pre-trained models from torchvision.models and modify the final layer.
You can evaluate a model using the model.eval() mode and run the model on test data to compute metrics like accuracy or loss.
Models are saved using torch.save(model.state_dict(), 'model.pth') and loaded with model.load_state_dict(torch.load('model.pth')).
Yes, PyTorch models can be deployed using tools like TorchServe for server-side deployment, or converted to TensorFlow Lite or ONNX for mobile and embedded applications.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)