Build optimizer, set weight decay of normalization to 0 by default.
(config, model)
| 11 | import torch.optim as optim |
| 12 | |
| 13 | def build_optimizer(config, model): |
| 14 | """ |
| 15 | Build optimizer, set weight decay of normalization to 0 by default. |
| 16 | """ |
| 17 | skip = {} |
| 18 | skip_keywords = {} |
| 19 | if hasattr(model, 'no_weight_decay'): |
| 20 | skip = model.no_weight_decay() |
| 21 | if hasattr(model, 'no_weight_decay_keywords'): |
| 22 | skip_keywords = model.no_weight_decay_keywords() |
| 23 | |
| 24 | if hasattr(model, 'lower_lr_kvs'): |
| 25 | lower_lr_kvs = model.lower_lr_kvs |
| 26 | else: |
| 27 | lower_lr_kvs = {} |
| 28 | |
| 29 | parameters = set_weight_decay_and_lr( |
| 30 | model, skip, skip_keywords, lower_lr_kvs, config.TRAIN.BASE_LR) |
| 31 | |
| 32 | opt_lower = config.TRAIN.OPTIMIZER.NAME.lower() |
| 33 | optimizer = None |
| 34 | if opt_lower == 'sgd': |
| 35 | optimizer = optim.SGD(parameters, momentum=config.TRAIN.OPTIMIZER.MOMENTUM, nesterov=True, |
| 36 | lr=config.TRAIN.BASE_LR, weight_decay=config.TRAIN.WEIGHT_DECAY) |
| 37 | elif opt_lower == 'adamw': |
| 38 | optimizer = optim.AdamW(parameters, eps=config.TRAIN.OPTIMIZER.EPS, betas=config.TRAIN.OPTIMIZER.BETAS, |
| 39 | lr=config.TRAIN.BASE_LR, weight_decay=config.TRAIN.WEIGHT_DECAY) |
| 40 | |
| 41 | return optimizer |
| 42 | |
| 43 | |
| 44 | def set_weight_decay_and_lr( |
no test coverage detected