Cosine annealing scheduler with optional warmup and freeze periods. Supports linear warmup followed by cosine annealing decay with optional initial freeze period for stable training.
| 158 | return tuple(x) |
| 159 | |
| 160 | class CosineScheduler(object): |
| 161 | """ |
| 162 | Cosine annealing scheduler with optional warmup and freeze periods. |
| 163 | |
| 164 | Supports linear warmup followed by cosine annealing decay with optional |
| 165 | initial freeze period for stable training. |
| 166 | """ |
| 167 | |
| 168 | def __init__(self, base_value, final_value, total_iters, warmup_iters=0, start_warmup_value=0, freeze_iters=0): |
| 169 | """ |
| 170 | Initialize cosine scheduler. |
| 171 | |
| 172 | Args: |
| 173 | base_value (float): Initial value after warmup |
| 174 | final_value (float): Final value at end of schedule |
| 175 | total_iters (int): Total number of iterations |
| 176 | warmup_iters (int): Number of warmup iterations |
| 177 | start_warmup_value (float): Starting value for warmup |
| 178 | freeze_iters (int): Number of initial freeze iterations |
| 179 | """ |
| 180 | super().__init__() |
| 181 | self.final_value = final_value |
| 182 | self.total_iters = total_iters |
| 183 | |
| 184 | freeze_schedule = np.zeros((freeze_iters)) |
| 185 | |
| 186 | warmup_schedule = np.linspace(start_warmup_value, base_value, warmup_iters) |
| 187 | |
| 188 | iters = np.arange(total_iters - warmup_iters - freeze_iters) |
| 189 | schedule = final_value + 0.5 * (base_value - final_value) * (1 + np.cos(np.pi * iters / len(iters))) |
| 190 | self.schedule = np.concatenate((freeze_schedule, warmup_schedule, schedule)) |
| 191 | |
| 192 | assert len(self.schedule) == self.total_iters |
| 193 | |
| 194 | def __getitem__(self, it): |
| 195 | """ |
| 196 | Get scheduled value for given iteration. |
| 197 | |
| 198 | Args: |
| 199 | it (int): Current iteration |
| 200 | |
| 201 | Returns: |
| 202 | float: Scheduled value for this iteration |
| 203 | """ |
| 204 | if it >= self.total_iters: |
| 205 | return self.final_value |
| 206 | else: |
| 207 | return self.schedule[it] |
nothing calls this directly
no outgoing calls
no test coverage detected