MCPcopy Create free account
hub / github.com/OpenTalker/StyleHEAT / weights_init

Function weights_init

utils/init_weight.py:4–57  ·  view source on GitHub ↗

r"""Initialize weights in the network. Args: init_type (str): The name of the initialization scheme. gain (float): The parameter that is required for the initialization scheme. bias (object): If not ``None``, specifies the initialization parameter

(init_type='normal', gain=0.02, bias=None)

Source from the content-addressed store, hash-verified

2
3
4def weights_init(init_type='normal', gain=0.02, bias=None):
5 r"""Initialize weights in the network.
6
7 Args:
8 init_type (str): The name of the initialization scheme.
9 gain (float): The parameter that is required for the initialization
10 scheme.
11 bias (object): If not ``None``, specifies the initialization parameter
12 for bias.
13
14 Returns:
15 (obj): init function to be applied.
16 """
17
18 def init_func(m):
19 r"""Init function
20
21 Args:
22 m: module to be weight initialized.
23 """
24 class_name = m.__class__.__name__
25 if hasattr(m, 'weight') and (
26 class_name.find('Conv') != -1 or
27 class_name.find('Linear') != -1 or
28 class_name.find('Embedding') != -1):
29 if init_type == 'normal':
30 init.normal_(m.weight.data, 0.0, gain)
31 elif init_type == 'xavier':
32 init.xavier_normal_(m.weight.data, gain=gain)
33 elif init_type == 'xavier_uniform':
34 init.xavier_uniform_(m.weight.data, gain=1.0)
35 elif init_type == 'kaiming':
36 init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
37 elif init_type == 'orthogonal':
38 init.orthogonal_(m.weight.data, gain=gain)
39 elif init_type == 'none':
40 m.reset_parameters()
41 else:
42 raise NotImplementedError(
43 'initialization method [%s] is '
44 'not implemented' % init_type)
45 if hasattr(m, 'bias') and m.bias is not None:
46 if bias is not None:
47 bias_type = getattr(bias, 'type', 'normal')
48 if bias_type == 'normal':
49 bias_gain = getattr(bias, 'gain', 0.5)
50 init.normal_(m.bias.data, 0.0, bias_gain)
51 else:
52 raise NotImplementedError(
53 'initialization method [%s] is '
54 'not implemented' % bias_type)
55 else:
56 init.constant_(m.bias.data, 0.0)
57 return init_func

Calls

no outgoing calls

Tested by

no test coverage detected