Lesson 3: Building a Simple NN with PyTorch
Write your first deep neural network from scratch using PyTorch.
Writing raw mathematical gradients from scratch is tedious and error-prone. Modern deep learning relies on high-level frameworks. Let's learn how to build neural networks using **PyTorch**, the primary deep learning library for researchers worldwide.
Introduction to PyTorch
PyTorch is built around two core features: **Tensors** (similar to NumPy arrays but with GPU acceleration support) and **Autograd** (automatic differentiation that tracks mathematical calculations to automatically compute gradients).
Defining Layers with torch.nn
The torch.nn module provides pre-packaged modules for standard neural network structures. A linear layer (fully connected layer) is defined as nn.Linear(in_features, out_features):
import torch import torch.nn as nn # Linear layer mapping 3 inputs to 2 outputs layer = nn.Linear(3, 2)
Stacking Layers with nn.Sequential
To stack layers in a clean forward pipeline, we use nn.Sequential. It chains layers together so the output of one layer automatically becomes the input of the next.
Gradient Calculations with backward()
When you make a forward prediction and calculate the loss, running loss.backward()automatically calculates all gradients via the backpropagation chain rule. PyTorch tracks every operation, saving you from writing tedious calculus.
Coding Challenge: PyTorch Linear Classifier
Let's simulate a standard PyTorch single-layer setup in Python!
- Import
torchandtorch.nn as nn. - Define a simple model using
nn.Sequentialcontaining annn.Linear(10, 2)layer followed by a Sigmoid activation (you can simulate this with custom output rules). - Write code to pass an input tensor of size
(1, 10)containing ones (torch.ones(1, 10)) and print the output shape.
Hint: Output shape should be torch.Size([1, 2])!