Beginner’s Guide to Neural Networks using PyTorch: A Step-by-Step Tutorial with Sample CodeWhat is a neural network

YAJ M
3 min readJun 21, 2023

--

Introduction:

Neural networks have revolutionized the field of artificial intelligence, enabling powerful applications such as image recognition, natural language processing, and speech synthesis. PyTorch, a popular deep-learning framework, provides an intuitive and flexible platform for building and training neural networks. In this tutorial, we will walk you through the process of creating a basic neural network using PyTorch, explaining each step along the way. By the end, you will have a solid understanding of how neural networks work and be ready to explore more complex architectures.

Prerequisites:

To follow this tutorial, you should have a basic understanding of Python programming and some familiarity with linear algebra and calculus concepts. Additionally, make sure you have PyTorch installed on your machine. You can install it via pip by running the command: pip install torch.

Step 1:

Importing the necessary libraries Let’s start by importing the required libraries. PyTorch is our primary dependency, but we will also import the torchvision library for convenient access to popular datasets and models.

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.datasets as datasets
import torchvision.transforms as transforms

Step 2:

Loading and Preprocessing the Data For this tutorial, we will use the MNIST dataset, which consists of grayscale images of handwritten digits (0–9). We’ll load the dataset and apply some basic preprocessing, including normalization and resizing.

# Define transformation pipeline
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])

# Load the MNIST training set
train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)

# Create a data loader
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)

Step 3:

Building the Neural Network Architecture Next, we define the architecture of our neural network. For this tutorial, we will create a simple feedforward network with two fully connected layers and a ReLU activation function.

class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.fc1 = nn.Linear(784, 128)
self.fc2 = nn.Linear(128, 10)
self.relu = nn.ReLU()

def forward(self, x):
x = x.view(x.size(0), -1)
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x

# Create an instance of the NeuralNetwork class
model = NeuralNetwork()

Step 4:

Defining the Loss Function and Optimizer To train our neural network, we need to specify a loss function and an optimizer. For this classification task, we will use the cross-entropy loss and the stochastic gradient descent (SGD) optimizer.

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

Step 5:

Training the Neural Network Now, we can proceed to train our neural network. We will iterate over the training dataset for a specified number of epochs, performing forward and backward passes to update the model’s weights.

num_epochs = 10

for epoch in range(num_epochs):
for batch_idx, (data, targets) in enumerate(train_loader):
optimizer.zero_grad()

# Forward pass
outputs = model(data)
loss = criterion(outputs, targets)

# Backward pass
loss.backward()
optimizer.step()

if batch_idx % 100 == 0:
print(f"Epoch {epoch+1}/{num_epochs}, Step {batch_idx}/{len(train_loader)}, Loss: {loss.item():.4f}")

Step 6:

Evaluating the Model After training, it’s essential to evaluate the performance of our model on unseen data. Here, we will use the MNIST test set to calculate the accuracy of our model.

test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)

def test_model():
model.eval()
correct = 0
total = 0

with torch.no_grad():
for data, targets in test_loader:
outputs = model(data)
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += (predicted == targets).sum().item()

print(f"Test Accuracy: {(100 * correct / total):.2f}%")

# Call the test_model function to evaluate the trained model
test_model()

Conclusion:

Congratulations! You have successfully built and trained a basic neural network using PyTorch. We covered the essential steps, from loading and preprocessing data to defining the network architecture, training, and evaluating the model. Neural networks offer endless possibilities, and PyTorch makes it accessible to implement and experiment with various architectures. Keep exploring and refining your models to tackle exciting challenges in the field of deep learning!

Remember, this tutorial only scratched the surface of what you can achieve with neural networks and PyTorch. As you delve deeper into the world of deep learning, you’ll encounter more complex architectures, advanced optimization techniques, and powerful applications. Happy coding!

--

--

YAJ M
YAJ M

Written by YAJ M

I am a passionate individual with expertise in Convolutional Neural Networks (CNNs) and computer vision

No responses yet