Skip to content

Instantly share code, notes, and snippets.

@devforfu
Created December 9, 2018 10:32
Show Gist options
  • Save devforfu/f8abf04888b92dfc94d169d34e182895 to your computer and use it in GitHub Desktop.
Save devforfu/f8abf04888b92dfc94d169d34e182895 to your computer and use it in GitHub Desktop.
Cosine annealing schedule
class CosineAnnealingSchedule:
"""
The schedule class that returns a learning rate multiplier from range [0.0, 1.0]
"""
def __init__(self, eta_min=0.0, eta_max=1.0, t_max=100, t_mult=2):
self.eta_min = eta_min
self.eta_max = eta_max
self.t_max = t_max
self.t_mult = t_mult
self.iter = 0
def update(self, **kwargs):
self.iter += 1
eta_min, eta_max, t_max = self.eta_min, self.eta_max, self.t_max
t = self.iter % t_max
eta = eta_min + 0.5 * (eta_max - eta_min) * (1 + math.cos(math.pi * t / t_max))
if t == 0:
self.iter = 0
self.t_max *= self.t_mult
return eta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment