(self,
optimizer,
max_lr,
total_steps=None,
epochs=None,
steps_per_epoch=None,
pct_start=0.3,
anneal_strategy='cos',
cycle_momentum=True,
base_momentum=0.85,
max_momentum=0.95,
div_factor=25.,
final_div_factor=1e4,
last_epoch=-1)
| 1178 | """ |
| 1179 | |
| 1180 | def __init__(self, |
| 1181 | optimizer, |
| 1182 | max_lr, |
| 1183 | total_steps=None, |
| 1184 | epochs=None, |
| 1185 | steps_per_epoch=None, |
| 1186 | pct_start=0.3, |
| 1187 | anneal_strategy='cos', |
| 1188 | cycle_momentum=True, |
| 1189 | base_momentum=0.85, |
| 1190 | max_momentum=0.95, |
| 1191 | div_factor=25., |
| 1192 | final_div_factor=1e4, |
| 1193 | last_epoch=-1): |
| 1194 | |
| 1195 | # Validate optimizer |
| 1196 | if not isinstance(optimizer, Optimizer): |
| 1197 | raise TypeError('{} is not an Optimizer'.format( |
| 1198 | type(optimizer).__name__)) |
| 1199 | self.optimizer = optimizer |
| 1200 | |
| 1201 | # Validate total_steps |
| 1202 | if total_steps is None and epochs is None and steps_per_epoch is None: |
| 1203 | raise ValueError("You must define either total_steps OR (epochs AND steps_per_epoch)") |
| 1204 | elif total_steps is not None: |
| 1205 | if total_steps <= 0 or not isinstance(total_steps, int): |
| 1206 | raise ValueError("Expected non-negative integer total_steps, but got {}".format(total_steps)) |
| 1207 | self.total_steps = total_steps |
| 1208 | else: |
| 1209 | if epochs <= 0 or not isinstance(epochs, int): |
| 1210 | raise ValueError("Expected non-negative integer epochs, but got {}".format(epochs)) |
| 1211 | if steps_per_epoch <= 0 or not isinstance(steps_per_epoch, int): |
| 1212 | raise ValueError("Expected non-negative integer steps_per_epoch, but got {}".format(steps_per_epoch)) |
| 1213 | self.total_steps = epochs * steps_per_epoch |
| 1214 | self.step_size_up = float(pct_start * self.total_steps) - 1 |
| 1215 | self.step_size_down = float(self.total_steps - self.step_size_up) - 1 |
| 1216 | |
| 1217 | # Validate pct_start |
| 1218 | if pct_start < 0 or pct_start > 1 or not isinstance(pct_start, float): |
| 1219 | raise ValueError("Expected float between 0 and 1 pct_start, but got {}".format(pct_start)) |
| 1220 | |
| 1221 | # Validate anneal_strategy |
| 1222 | if anneal_strategy not in ['cos', 'linear']: |
| 1223 | raise ValueError("anneal_strategy must by one of 'cos' or 'linear', instead got {}".format(anneal_strategy)) |
| 1224 | elif anneal_strategy == 'cos': |
| 1225 | self.anneal_func = self._annealing_cos |
| 1226 | elif anneal_strategy == 'linear': |
| 1227 | self.anneal_func = self._annealing_linear |
| 1228 | |
| 1229 | # Initialize learning rate variables |
| 1230 | max_lrs = self._format_param('max_lr', self.optimizer, max_lr) |
| 1231 | if last_epoch == -1: |
| 1232 | for idx, group in enumerate(self.optimizer.param_groups): |
| 1233 | group['initial_lr'] = max_lrs[idx] / div_factor |
| 1234 | group['max_lr'] = max_lrs[idx] |
| 1235 | group['min_lr'] = group['initial_lr'] / final_div_factor |
| 1236 | |
| 1237 | # Initialize momentum variables |
nothing calls this directly
no test coverage detected