WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PyTorch · Intermediate · question 33 of 100

Describe the role of the torch.optim.lr_scheduler module in PyTorch and provide an example of using a learning rate scheduler.?

📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

In deep learning, setting a proper learning rate is crucial for the training process to be successful. If the learning rate is too high, the model may fail to converge or overshoot the optimal solution. If it is too low, the training may take too long or get stuck in a suboptimal solution. Ideally, you want to gradually decrease the learning rate during training as the model approaches a minimum in the loss function. This is where learning rate schedulers come in.

The ‘torch.optim.lr_scheduler‘ module in PyTorch provides several methods for scheduling the learning rate during training. These methods define how the learning rate changes as a function of the epoch number or the number of training steps.

Here is an example of using the StepLR scheduler:

import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler

optimizer = optim.SGD(model.parameters(), lr=0.1)
scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)

for epoch in range(num_epochs):
    # Train the model
    train_loss = train(model, train_loader, optimizer, epoch)
    
    # Adjust the learning rate
    scheduler.step()
    
    # Evaluate the model
    val_loss, val_accuracy = evaluate(model, val_loader)

In this example, we first instantiate an SGD optimizer with an initial learning rate of 0.1. We then create a ‘StepLR‘ scheduler object with a step size of 30 epochs and a decay factor of 0.1. During training, every 30 epochs the learning rate will be multiplied by 0.1.

After each epoch, we call the ‘scheduler.step()‘ method to update the learning rate based on the current epoch number. Notice that we update the learning rate before evaluating the model, which is important since we want to evaluate the model with the current learning rate.

Other schedulers that ‘torch.optim.lr_scheduler‘ provides include CosineAnnealingLR, ReduceLROnPlateau, and CyclicLR. Each of these schedulers have their own use cases and can be very useful for improving the performance of a model.

Overall, the ‘torch.optim.lr_scheduler‘ module provides a useful tool to fine-tune the learning rate during training, allowing a model to converge more quickly and produce better results.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview — then scores it.
📞 Practice PyTorch — free 15 min
📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

All 100 PyTorch questions · All topics