MCPcopy Index your code
hub / github.com/LeapLabTHU/DAT / build_optimizer

Function build_optimizer

optimizer.py:13–41  ·  view source on GitHub ↗

Build optimizer, set weight decay of normalization to 0 by default.

(config, model)

Source from the content-addressed store, hash-verified

11import torch.optim as optim
12
13def 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
44def set_weight_decay_and_lr(

Callers 2

mainFunction · 0.90
mainFunction · 0.90

Calls 3

set_weight_decay_and_lrFunction · 0.85
no_weight_decayMethod · 0.80

Tested by

no test coverage detected