(optim_config, lr_scheduler_config, epochs, step_each_epoch,
model)
| 34 | |
| 35 | |
| 36 | def build_optimizer(optim_config, lr_scheduler_config, epochs, step_each_epoch, |
| 37 | model): |
| 38 | from . import lr |
| 39 | |
| 40 | config = copy.deepcopy(optim_config) |
| 41 | |
| 42 | if isinstance(model, nn.Module): |
| 43 | # a model was passed in, extract parameters and add weight decays to appropriate layers |
| 44 | weight_decay = config.get('weight_decay', 0.0) |
| 45 | filter_bias_and_bn = (config.pop('filter_bias_and_bn') |
| 46 | if 'filter_bias_and_bn' in config else False) |
| 47 | if weight_decay > 0.0 and filter_bias_and_bn: |
| 48 | no_weight_decay = {} |
| 49 | if hasattr(model, 'no_weight_decay'): |
| 50 | no_weight_decay = model.no_weight_decay() |
| 51 | parameters = param_groups_weight_decay(model, weight_decay, |
| 52 | no_weight_decay) |
| 53 | config['weight_decay'] = 0.0 |
| 54 | # print('debug adamw') |
| 55 | else: |
| 56 | parameters = model.parameters() |
| 57 | else: |
| 58 | # iterable of parameters or param groups passed in |
| 59 | parameters = model |
| 60 | |
| 61 | optim = getattr(torch.optim, config.pop('name'))(params=parameters, |
| 62 | **config) |
| 63 | |
| 64 | lr_config = copy.deepcopy(lr_scheduler_config) |
| 65 | scheduler_name = lr_config.pop('name') |
| 66 | |
| 67 | lr_config.update({ |
| 68 | 'epochs': epochs, |
| 69 | 'step_each_epoch': step_each_epoch, |
| 70 | 'lr': config['lr'] |
| 71 | }) |
| 72 | lr_scheduler = getattr(lr, scheduler_name)(**lr_config)(optimizer=optim) |
| 73 | |
| 74 | return optim, lr_scheduler |
no test coverage detected