Return a learning rate scheduler Parameters: optimizer -- the optimizer of the network opt (option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions. opt.lr_policy is the name of learning rate policy: linear
(optimizer, opt)
| 36 | |
| 37 | |
| 38 | def get_scheduler(optimizer, opt): |
| 39 | """Return a learning rate scheduler |
| 40 | |
| 41 | Parameters: |
| 42 | optimizer -- the optimizer of the network |
| 43 | opt (option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions. |
| 44 | opt.lr_policy is the name of learning rate policy: linear | step | plateau | cosine |
| 45 | |
| 46 | For 'linear', we keep the same learning rate for the first <opt.n_epochs> epochs |
| 47 | and linearly decay the rate to zero over the next <opt.n_epochs_decay> epochs. |
| 48 | For other schedulers (step, plateau, and cosine), we use the default PyTorch schedulers. |
| 49 | See https://pytorch.org/docs/stable/optim.html for more details. |
| 50 | """ |
| 51 | if opt.lr_policy == 'linear': |
| 52 | def lambda_rule(epoch): |
| 53 | lr_l = 1.0 - max(0, epoch + opt.epoch_count - opt.n_epochs) / float(opt.n_epochs_decay + 1) |
| 54 | return lr_l |
| 55 | scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule) |
| 56 | elif opt.lr_policy == 'step': |
| 57 | scheduler = lr_scheduler.StepLR(optimizer, step_size=opt.lr_decay_iters, gamma=0.1) |
| 58 | elif opt.lr_policy == 'plateau': |
| 59 | scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.2, threshold=0.01, patience=5) |
| 60 | elif opt.lr_policy == 'cosine': |
| 61 | scheduler = lr_scheduler.CosineAnnealingLR(optimizer, T_max=opt.n_epochs, eta_min=0) |
| 62 | else: |
| 63 | return NotImplementedError('learning rate policy [%s] is not implemented', opt.lr_policy) |
| 64 | return scheduler |
| 65 | |
| 66 | |
| 67 | def init_weights(net, init_type='normal', init_gain=0.02): |
nothing calls this directly
no outgoing calls
no test coverage detected