Skip to content

Instantly share code, notes, and snippets.

@hatzel
Created October 25, 2019 09:56
Show Gist options
  • Save hatzel/8965ebf1231e3cf3c67caecbbb028dec to your computer and use it in GitHub Desktop.
Save hatzel/8965ebf1231e3cf3c67caecbbb028dec to your computer and use it in GitHub Desktop.
Quickly Visualize PyTorch Learning Schedulers
import torch
from torch.optim.lr_scheduler import CosineAnnealingLR
from torch.optim import SGD
import matplotlib.pyplot as plt
STEPS = 100
optimizer = SGD([torch.tensor(1)], lr=1)
# Use a scheduler of your choice below.
# Great for debugging your own schedulers!
scheduler = CosineAnnealingLR(optimizer, STEPS)
lrs = []
for _ in range(100):
optimizer.step()
lrs.append(scheduler.get_lr())
scheduler.step()
plt.plot(lrs)
plt.show()
@kevinsu628
Copy link

Thanks for the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment