Evaluating model performance

Introduction to Deep Learning with PyTorch

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

Training, validation and testing

$$

  • A dataset is typically split into three subsets:
Percent of data Role
Training 80-90% Adjusts model parameters
Validation 10-20% Tunes hyperparameters
Test 5-10% Evaluates final model performance

$$

  • Track loss and accuracy during training and validation
Introduction to Deep Learning with PyTorch

Calculating training loss

$$

For each epoch:

  • Sum the loss across all batches in the dataloader
  • Compute the mean training loss at the end of the epoch
training_loss = 0.0

for inputs, labels in trainloader: # Run the forward pass outputs = model(inputs) # Compute the loss loss = criterion(outputs, labels)
# Backpropagation loss.backward() # Compute gradients optimizer.step() # Update weights optimizer.zero_grad() # Reset gradients
# Calculate and sum the loss training_loss += loss.item()
epoch_loss = training_loss / len(trainloader)
Introduction to Deep Learning with PyTorch

Calculating validation loss

validation_loss = 0.0
model.eval() # Put model in evaluation mode


with torch.no_grad(): # Disable gradients for efficiency
for inputs, labels in validationloader: # Run the forward pass outputs = model(inputs) # Calculate the loss loss = criterion(outputs, labels) validation_loss += loss.item() epoch_loss = validation_loss / len(validationloader) # Compute mean loss
model.train() # Switch back to training mode
Introduction to Deep Learning with PyTorch

Overfitting

an example of overfitting

Introduction to Deep Learning with PyTorch

Calculating accuracy with torchmetrics

import torchmetrics


# Create accuracy metric metric = torchmetrics.Accuracy(task="multiclass", num_classes=3)
for features, labels in dataloader: outputs = model(features) # Forward pass # Compute batch accuracy (keeping argmax for one-hot labels) metric.update(outputs, labels.argmax(dim=-1))
# Compute accuracy over the whole epoch accuracy = metric.compute()
# Reset metric for the next epoch metric.reset()
Introduction to Deep Learning with PyTorch

Let's practice!

Introduction to Deep Learning with PyTorch

Preparing Video For Download...