MCPcopy Create free account
hub / github.com/OpenGVLab/UniFormerV2 / construct_optimizer

Function construct_optimizer

slowfast/models/optimizer_backbone.py:14–126  ·  view source on GitHub ↗

Construct a stochastic gradient descent or ADAM optimizer with momentum. Details can be found in: Herbert Robbins, and Sutton Monro. "A stochastic approximation method." and Diederik P.Kingma, and Jimmy Ba. "Adam: A Method for Stochastic Optimization." Args: mod

(model, cfg)

Source from the content-addressed store, hash-verified

12
13
14def construct_optimizer(model, cfg):
15 """
16 Construct a stochastic gradient descent or ADAM optimizer with momentum.
17 Details can be found in:
18 Herbert Robbins, and Sutton Monro. "A stochastic approximation method."
19 and
20 Diederik P.Kingma, and Jimmy Ba.
21 "Adam: A Method for Stochastic Optimization."
22
23 Args:
24 model (model): model to perform stochastic gradient descent
25 optimization or ADAM optimization.
26 cfg (config): configs of hyper-parameters of SGD or ADAM, includes base
27 learning rate, momentum, weight_decay, dampening, and etc.
28 """
29 bn_parameters = []
30 non_bn_parameters = []
31 zero_parameters = []
32 head_bn_parameters = []
33 head_non_bn_parameters = []
34 head_zero_parameters = []
35 skip = {}
36 if hasattr(model, "no_weight_decay"):
37 skip = model.no_weight_decay()
38
39 logger.info(f'LR Ration for backbone is {cfg.SOLVER.BACKBONE_LR_RATIO}')
40
41 total_num = 0
42 for name, m in model.named_modules():
43 is_bn = isinstance(m, torch.nn.modules.batchnorm._NormBase)
44 for p in m.parameters(recurse=False):
45 if not p.requires_grad:
46 continue
47 total_num += 1
48 if is_bn:
49 if 'backbone.' in name:
50 bn_parameters.append(p)
51 else:
52 head_bn_parameters.append(p)
53 elif name in skip or (
54 (len(p.shape) == 1 or name.endswith(".bias"))
55 and cfg.SOLVER.ZERO_WD_1D_PARAM
56 ):
57 if 'backbone.' in name:
58 zero_parameters.append(p)
59 else:
60 head_zero_parameters.append(p)
61 else:
62 if 'backbone.' in name:
63 non_bn_parameters.append(p)
64 else:
65 head_non_bn_parameters.append(p)
66
67 optim_params = [
68 {"params": bn_parameters, "weight_decay": cfg.BN.WEIGHT_DECAY, 'lr': cfg.SOLVER.BASE_LR * cfg.SOLVER.BACKBONE_LR_RATIO},
69 {"params": non_bn_parameters, "weight_decay": cfg.SOLVER.WEIGHT_DECAY, 'lr': cfg.SOLVER.BASE_LR * cfg.SOLVER.BACKBONE_LR_RATIO},
70 {"params": zero_parameters, "weight_decay": 0.0, 'lr': cfg.SOLVER.BASE_LR * cfg.SOLVER.BACKBONE_LR_RATIO},
71 {"params": head_bn_parameters, "weight_decay": cfg.BN.WEIGHT_DECAY},

Callers

nothing calls this directly

Calls 1

no_weight_decayMethod · 0.45

Tested by

no test coverage detected