Everything you need:
Both model and data must be on the same device
Designed for classification tasks
Perfect for choosing a digit from 0-9
Adam: adapts learning rate as it trains
def train_one_epoch(model, dataloader, loss_fn, optimizer, device):
model.train() # Set to training mode
running_loss = 0.0
correct = 0
total = 0
for batch_idx, (data, targets) in enumerate(dataloader):
# Move to device
data, targets = data.to(device), targets.to(device)
# Training steps
optimizer.zero_grad()
outputs = model(data)
loss = loss_fn(outputs, targets)
loss.backward()
optimizer.step()
# Track progress
running_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
if batch_idx % 100 == 0:
print(f'Loss: {loss.item():.4f}, '
f'Accuracy: {100.*correct/total:.2f}%')With 60,000 training images and batch size 64:
Watch the numbers:
def evaluate(model, dataloader, device):
model.eval() # Set to evaluation mode
correct = 0
total = 0
with torch.no_grad(): # Disable gradient tracking
for data, targets in dataloader:
data, targets = data.to(device), targets.to(device)
outputs = model(data)
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
return 100. * correct / total10 epochs = 10 full passes through training data
After each epoch: evaluate on test set
By epoch 10: Loss: tiny, Accuracy: high (often 95%+)
When accuracy stops improving: Model is done learning, May not need all 10 epochs
You’ve learned:
nn.Module“Don’t tell me the moon is shining; show me the glint of light on broken glass.”
CUE: START THE LAB HERE
“The eye sees only what the mind is prepared to comprehend.”
CUE: START THE ASSIGNMENT HERE
In Module 3: Data Management we learn: