Sling Academy
Home/PyTorch/PyTorch Classification from Scratch: Building a Dense Neural Network

PyTorch Classification from Scratch: Building a Dense Neural Network

Last updated: December 14, 2024

In this article, we'll walk you through the process of building a dense neural network for classification using PyTorch, a popular deep learning library. Our goal is to create a model from scratch that classifies images into predefined categories. We will use the FashionMNIST dataset to demonstrate how to set up your data, define a model, train it, and evaluate its performance.

Prerequisites

Before we begin, make sure you have a basic understanding of Python and have installed PyTorch. You can install it by running the command:

pip install torch torchvision

Step 1: Importing Libraries

First, we need to import the necessary libraries:

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

The above code imports torch for tensor computations, torch.nn for building neural networks, and torchvision for handling image data.

Step 2: Preparing the Dataset

The FashionMNIST dataset can be easily loaded using torchvision:

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
])
trainset = torchvision.datasets.FashionMNIST(
    root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True)
testset = torchvision.datasets.FashionMNIST(
    root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=32, shuffle=False)

We normalize the images to have a mean of 0.5 and a standard deviation of 0.5, making the training process more stable.

Step 3: Defining the Neural Network

Here, we define a simple dense neural network:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(28 * 28, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, 10)

    def forward(self, x):
        x = x.view(-1, 28 * 28)
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x

The network consists of two hidden layers with 512 and 256 neurons, each followed by a ReLU activation function, and an output layer predicting the class of the image.

Step 4: Define Loss Function and Optimizer

We use CrossEntropyLoss and the Adam optimizer:

net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)

The loss function measures how well the predicted values are aligning with the actual labels, and the optimizer updates the model's weights to reduce the loss.

Step 5: Training the Network

Now, we'll train the model over several epochs:

for epoch in range(5):
    running_loss = 0.0
    for inputs, labels in trainloader:
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
    print(f"Epoch {epoch+1}, Loss: {running_loss/len(trainloader)}")

For each epoch, we iterate over the dataset, passing inputs through the network, computing the loss, and updating the network weights.

Step 6: Evaluating the Model

Finally, let's evaluate how the model performs on the test data:

correct = 0
total = 0
with torch.no_grad():
    for inputs, labels in testloader:
        outputs = net(inputs)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f'Accuracy: {100 * correct / total}%')

We feed the test data through the model and check the accuracy of the predictions compared to the true labels.

Conclusion

In this guide, we've covered the essential steps required to implement a dense neural network in PyTorch from scratch for image classification. While this example uses a simple architecture and dataset, the concepts discussed can be easily extended to more complex scenarios and models.

Next Article: Efficient PyTorch Inference for Real-Time Neural Network Classification

Previous Article: Troubleshooting Neural Network Classification Issues in PyTorch

Series: PyTorch Neural Network Classification

PyTorch

You May Also Like

  • Addressing "UserWarning: floor_divide is deprecated, and will be removed in a future version" in PyTorch Tensor Arithmetic
  • In-Depth: Convolutional Neural Networks (CNNs) for PyTorch Image Classification
  • Implementing Ensemble Classification Methods with PyTorch
  • Using Quantization-Aware Training in PyTorch to Achieve Efficient Deployment
  • Accelerating Cloud Deployments by Exporting PyTorch Models to ONNX
  • Automated Model Compression in PyTorch with Distiller Framework
  • Transforming PyTorch Models into Edge-Optimized Formats using TVM
  • Deploying PyTorch Models to AWS Lambda for Serverless Inference
  • Scaling Up Production Systems with PyTorch Distributed Model Serving
  • Applying Structured Pruning Techniques in PyTorch to Shrink Overparameterized Models
  • Integrating PyTorch with TensorRT for High-Performance Model Serving
  • Leveraging Neural Architecture Search and PyTorch for Compact Model Design
  • Building End-to-End Model Deployment Pipelines with PyTorch and Docker
  • Implementing Mixed Precision Training in PyTorch to Reduce Memory Footprint
  • Converting PyTorch Models to TorchScript for Production Environments
  • Deploying PyTorch Models to iOS and Android for Real-Time Applications
  • Combining Pruning and Quantization in PyTorch for Extreme Model Compression
  • Using PyTorch’s Dynamic Quantization to Speed Up Transformer Inference
  • Applying Post-Training Quantization in PyTorch for Edge Device Efficiency