Writing our first training loop

Introduction to Deep Learning with PyTorch

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

Training a neural network

  1. Create a model
  2. Choose a loss function
  3. Define a dataset
  4. Set an optimizer
  5. Run a training loop:
    • Calculate loss (forward pass)
    • Compute gradients (backpropagation)
    • Updating model parameters
Introduction to Deep Learning with PyTorch

Introducing the Data Science Salary dataset

 experience_level  employment_type  remote_ratio  company_size  salary_in_usd  
        0                0               0.5             1            0.036 
        1                0               1.0             2            0.133     
        2                0               0.0             1            0.234  
        1                0               1.0             0            0.076  
        2                0               1.0             1            0.170

$$

  • Features: categorical, target: salary (USD)
  • Final output: linear layer
  • Loss: regression-specific
Introduction to Deep Learning with PyTorch

Mean Squared Error Loss

$$

  • MSE loss is the mean of the squared difference between predictions and ground truth
def mean_squared_loss(prediction, target):
  return np.mean((prediction - target)**2)

$$

  • in PyTorch:
criterion = nn.MSELoss()
# Prediction and target are float tensors
loss = criterion(prediction, target)
Introduction to Deep Learning with PyTorch

Before the training loop

# Create the dataset and the dataloader
dataset = TensorDataset(torch.tensor(features).float(),
                        torch.tensor(target).float())


dataloader = DataLoader(dataset, batch_size=4, shuffle=True)
# Create the model model = nn.Sequential(nn.Linear(4, 2), nn.Linear(2, 1))
# Create the loss and optimizer criterion = nn.MSELoss() optimizer = optim.SGD(model.parameters(), lr=0.001)
Introduction to Deep Learning with PyTorch

The training loop

for epoch in range(num_epochs):

for data in dataloader:
# Set the gradients to zero optimizer.zero_grad()
# Get feature and target from the data loader feature, target = data
# Run a forward pass pred = model(feature) # Compute loss and gradients loss = criterion(pred, target) loss.backward()
# Update the parameters optimizer.step()
Introduction to Deep Learning with PyTorch

Let's practice!

Introduction to Deep Learning with PyTorch

Preparing Video For Download...