AI Development Lesson 6: Neural Networks
Neural networks power modern AI — image recognition, language models, speech. PyTorch is the professional choice for building them.
Concepts
// Neuron: takes inputs, applies weights, bias, activation function
// Layer: collection of neurons
// Forward pass: data flows through layers to make prediction
// Loss: how wrong the prediction is
// Backpropagation: calculate gradients (blame) for each weight
// Gradient descent: update weights to reduce loss
// Epoch: one pass through all training data
// Batch: subset of training data for one update
PyTorch
import torch
import torch.nn as nn
# Define a neural network
class SimpleNet(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(input_size, hidden_size),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, output_size)
)
def forward(self, x):
return self.layers(x)
model = SimpleNet(input_size=10, hidden_size=64, output_size=1)
# Training loop
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
criterion = nn.BCEWithLogitsLoss() # binary classification
for epoch in range(100):
model.train()
optimizer.zero_grad()
output = model(X_train_tensor)
loss = criterion(output, y_train_tensor)
loss.backward()
optimizer.step()
if epoch % 10 == 0:
print(f"Epoch {epoch}: loss={loss.item():.4f}")
🏋️ Practice Task
Build a neural network to classify handwritten digits (MNIST). Use torchvision to load data. Create a 3-layer network: 784→256→128→10. Train for 5 epochs. Achieve at least 95% accuracy. Show a grid of misclassified examples.
💡 Hint: from torchvision import datasets, transforms. MNIST has 60k training images of digits 0-9, each 28×28 pixels = 784 inputs.