-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.py
70 lines (65 loc) · 2.59 KB
/
optimizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import torch.nn as nn
import torch.optim as optim
from typing import Callable, Union
def get_optimizer(net: nn.Module, lr: float, optim_name: Union[str, Callable] = 'sgd', **kwargs):
group_weights, group_bias = [], []
for name, params in net.named_parameters():
if params.requires_grad is False:
continue
elif 'bias' in name:
group_bias.append(params)
else:
group_weights.append(params)
if isinstance(optim_name, str):
optim_name = optim_name.lower()
if optim_name == 'sgd':
optimizer = optim.SGD(
params=group_bias,
lr=lr,
momentum=kwargs.get('momentum', 0), # 动量法的累计梯度的系数
dampening=kwargs.get('dampening', 0), # 动量法中当前梯度的系数值(1-dampening)
weight_decay=0.0, # 针对bias不进行惩罚性限制
nesterov=kwargs.get('nesterov', False) # 牛顿动量法
)
optimizer.add_param_group(
param_group={
'params': group_weights,
'lr': lr * 0.5,
'weight_decay': kwargs.get('weight_decay', 0.0)
}
)
elif optim_name == 'adam':
optimizer = optim.Adam(params=group_bias, lr=lr)
optimizer.add_param_group(
param_group={
'params': group_weights,
'lr': lr * 0.5,
'weight_decay': kwargs.get('weight_decay', 0.0)
}
)
elif optim_name == 'adamw':
optimizer = optim.AdamW(params=group_bias, lr=lr)
optimizer.add_param_group(
param_group={
'params': group_weights,
'lr': lr * 0.5,
'weight_decay': kwargs.get('weight_decay', 0.0)
}
)
else:
raise ValueError(f'当前优化器不支持:{optim_name}')
return optimizer
else:
return optim_name
def get_scheduler(opt, name: Union[str, Callable] = 'linear'):
if name is None:
return None
elif isinstance(name, str):
name = name.lower()
if name == 'linear':
scheduler = optim.lr_scheduler.LinearLR(opt, start_factor=1.0, end_factor=0.1, total_iters=5)
else:
raise ValueError(f'当前优化器不支持:{name}')
else:
scheduler = optim.lr_scheduler.LambdaLR(opt, name)
return scheduler