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

Function construct_optimizer

slowfast/models/optimizer_backbone_special.py:14–141  ·  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 special_bn_parameters = []
33 special_non_bn_parameters = []
34 special_zero_parameters = []
35 skip = {}
36 if hasattr(model, "no_weight_decay"):
37 skip = model.no_weight_decay()
38
39 special_list = cfg.SOLVER.SPECIAL_LIST
40 special_ration = cfg.SOLVER.SPECIAL_RATIO
41 logger.info(f'Special parameter list: {special_list}')
42 logger.info(f'LR Ration for special parameter is {special_ration}')
43
44 total_num = 0
45 for name, m in model.named_modules():
46 is_bn = isinstance(m, torch.nn.modules.batchnorm._NormBase)
47 for p in m.parameters(recurse=False):
48 if not p.requires_grad:
49 continue
50 total_num += 1
51 if is_bn:
52 flag = False
53 for s in special_list:
54 if s in name:
55 special_bn_parameters.append(p)
56 flag = True
57 break
58 if not flag:
59 bn_parameters.append(p)
60 elif name in skip or (
61 (len(p.shape) == 1 or name.endswith(".bias"))
62 and cfg.SOLVER.ZERO_WD_1D_PARAM
63 ):
64 flag = False
65 for s in special_list:
66 if s in name:
67 special_zero_parameters.append(p)
68 flag = True
69 break
70 if not flag:
71 zero_parameters.append(p)

Callers

nothing calls this directly

Calls 1

no_weight_decayMethod · 0.45

Tested by

no test coverage detected