Topic 07 / 8
Deep Learning Fundamentals with PyTorch
What is a Neural Network?
Neural networks are powerful models inspired by the human brain. They consist of layers of interconnected “neurons”. Data flows through the input layer, gets transformed by hidden layers, and produces an answer at the output layer. They are exceptionally good at complex tasks like image recognition and natural language processing.
Tensors: The Building Blocks
PyTorch operates on Tensors, which are exactly like NumPy arrays but can run on GPUs for massive acceleration.
import torch
# Create a tensor on the GPU (if available)
device = "cuda" if torch.cuda.is_available() else "cpu"
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device)Building a Simple Network
In PyTorch, you define networks by subclassing nn.Module and defining the layers in __init__ and the forward pass in forward().
import torch.nn as nn
class SimpleNet(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(10, 64) # 10 inputs, 64 hidden neurons
self.relu = nn.ReLU() # Activation function
self.fc2 = nn.Linear(64, 1) # 64 hidden, 1 output
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
model = SimpleNet()The Training Loop
Unlike scikit-learn’s simple .fit(), PyTorch requires you to write the training loop manually. This gives you absolute control over the training process.
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
loss_fn = nn.MSELoss()
for epoch in range(100):
# 1. Forward pass
predictions = model(X_train)
# 2. Calculate loss
loss = loss_fn(predictions, y_train)
# 3. Backpropagation (calculate gradients)
optimizer.zero_grad()
loss.backward()
# 4. Update weights
optimizer.step()