|
|
@ -85,21 +85,29 @@ class Scheduler:
|
|
|
|
param_group[self.param_group_field] = value
|
|
|
|
param_group[self.param_group_field] = value
|
|
|
|
|
|
|
|
|
|
|
|
def _add_noise(self, lrs, t):
|
|
|
|
def _add_noise(self, lrs, t):
|
|
|
|
|
|
|
|
if self._is_apply_noise(t):
|
|
|
|
|
|
|
|
noise = self._calculate_noise(t)
|
|
|
|
|
|
|
|
lrs = [v + v * noise for v in lrs]
|
|
|
|
|
|
|
|
return lrs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _is_apply_noise(self, t) -> bool:
|
|
|
|
|
|
|
|
"""Return True if scheduler in noise range."""
|
|
|
|
if self.noise_range_t is not None:
|
|
|
|
if self.noise_range_t is not None:
|
|
|
|
if isinstance(self.noise_range_t, (list, tuple)):
|
|
|
|
if isinstance(self.noise_range_t, (list, tuple)):
|
|
|
|
apply_noise = self.noise_range_t[0] <= t < self.noise_range_t[1]
|
|
|
|
apply_noise = self.noise_range_t[0] <= t < self.noise_range_t[1]
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
apply_noise = t >= self.noise_range_t
|
|
|
|
apply_noise = t >= self.noise_range_t
|
|
|
|
if apply_noise:
|
|
|
|
return apply_noise
|
|
|
|
g = torch.Generator()
|
|
|
|
|
|
|
|
g.manual_seed(self.noise_seed + t)
|
|
|
|
def _calculate_noise(self, t) -> float:
|
|
|
|
if self.noise_type == 'normal':
|
|
|
|
g = torch.Generator()
|
|
|
|
while True:
|
|
|
|
g.manual_seed(self.noise_seed + t)
|
|
|
|
|
|
|
|
if self.noise_type == 'normal':
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# resample if noise out of percent limit, brute force but shouldn't spin much
|
|
|
|
# resample if noise out of percent limit, brute force but shouldn't spin much
|
|
|
|
noise = torch.randn(1, generator=g).item()
|
|
|
|
noise = torch.randn(1, generator=g).item()
|
|
|
|
if abs(noise) < self.noise_pct:
|
|
|
|
if abs(noise) < self.noise_pct:
|
|
|
|
break
|
|
|
|
return noise
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
noise = 2 * (torch.rand(1, generator=g).item() - 0.5) * self.noise_pct
|
|
|
|
noise = 2 * (torch.rand(1, generator=g).item() - 0.5) * self.noise_pct
|
|
|
|
lrs = [v + v * noise for v in lrs]
|
|
|
|
return noise
|
|
|
|
return lrs
|
|
|
|
|
|
|
|