when using learning rate scheduler from PyTorch, it appears that it does not step
Yes I used the notebook above and the only thing I did is to add callback to the trainer. See code below
from pytorch_lightning.callbacks import LearningRateMonitor
def test_x(tmpdir):
# init model
model = BoringModel()
lr_monitor = LearningRateMonitor(logging_interval='step')
# Initialize a trainer
trainer = pl.Trainer(
max_epochs=1,
progress_bar_refresh_rate=20,
callbacks=[lr_monitor]
)
# Train the model ⚡
trainer.fit(model, train, val)
trainer.test(test_dataloaders=test)
After the code run, open the log using tensorboard
%load_ext tensorboard
%tensorboard --logdir lightning_logs --reload_interval 1
and look for the lr-SGD curve and I find it flat instead of gradually decrease 10% (gamma=0.1 by default) every 10 steps.
Here is the link for my notebook
https://colab.research.google.com/drive/10l3Kz-9rOP7lu5qrLurJj-2m-lio8SmE?usp=sharing
learning rate schedule should step
Note: Bugs with code are solved faster ! Colab Notebook should be made public !
IDE: Please, use our python bug_report_model.py template.
Colab Notebook: Please copy and paste the output from our environment collection script (or fill out the checklist below manually).
You can get the script and run it with:
wget https://raw.githubusercontent.com/PyTorchLightning/pytorch-lightning/master/tests/collect_env_details.py
# For security purposes, please check the contents of collect_env_details.py before running it.
python collect_env_details.py
When I print the learning rates to console with
optimizer.param_groups[0]['lr']
I can see it change, so it must be a visualization issue.
@rohitgr7 have you worked on the LRMonitor? Any ideas what could be wrong with the visualization?
@awaelchli thanks for looking into it. Could you share the code you print the learning rate to console so I can do more testing?
you need to update:
def configure_optimizers(self):
optimizer = torch.optim.SGD(self.layer.parameters(), lr=0.1)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10)
lr_scheduler = {'scheduler': lr_scheduler, 'interval': 'step'}
#lr_scheduler = get_linear_schedule_with_warmup(optimizer, 100, 1000)
return [optimizer], [lr_scheduler]
here interval: 'step' tells when to update the lr and LearningRateMonitor(logging_interval='step') tells when to log the lr.
@junwen-austin I just added this line to the training_step
print(self.trainer.optimizers[0].param_groups[0]['lr'])
@junwen-austin as per @rohitgr7 's answer you need to run multiple epoch to see the updates or change the scheduling from epoch to step using the dict key in the code sample above.
@awaelchli @rohitgr7 Thank you so much!
Most helpful comment
you need to update:
here
interval: 'step'tells when to update the lr andLearningRateMonitor(logging_interval='step')tells when to log the lr.