-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitialization.py
33 lines (26 loc) · 1.02 KB
/
initialization.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
import math
from torch import nn
def init_weights(dist_func, module, constant=None, dist='uniform', **kwargs):
if dist not in dist_func.keys():
raise Exception('distribution {} not found'.format(dist))
for name, param in module.named_parameters():
if param.dim() == 1:
if constant is not None:
nn.init.constant_(param, constant)
else:
stdv = 1.0 / math.sqrt(param.numel())
nn.init.uniform_(param, -stdv, stdv)
else:
dist_func[dist](param, **kwargs)
def init_xavier(module, constant=None, dist='uniform', **kwargs):
dist_func = {
'uniform': nn.init.xavier_uniform_,
'normal': nn.init.xavier_normal_
}
init_weights(dist_func, module, constant, dist, **kwargs)
def init_kaiming(module, constant=None, dist='uniform', **kwargs):
dist_func = {
'uniform': nn.init.kaiming_uniform_,
'normal': nn.init.kaiming_normal_
}
init_weights(dist_func, module, constant, dist, **kwargs)