Search code examples
pythonmachine-learningdeep-learningpytorch

PyTorch - How to get learning rate during training?


While training, I'd like to know the value of learning_rate. What should I do?

It's my code, like this:

my_optimizer = torch.optim.SGD(my_model.parameters(), 
                               lr=0.001, 
                               momentum=0.99, 
                               weight_decay=2e-3)

Thank you.


Solution

  • For only one parameter group like in the example you've given, you can use this function and call it during training to get the current learning rate:

    def get_lr(optimizer):
        for param_group in optimizer.param_groups:
            return param_group['lr']