lhallee commited on
Commit
122a96e
·
verified ·
1 Parent(s): cba994c

Upload vb_layers_initialize.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. vb_layers_initialize.py +86 -86
vb_layers_initialize.py CHANGED
@@ -1,86 +1,86 @@
1
- """Utility functions for initializing weights and biases."""
2
-
3
- # Copyright 2021 AlQuraishi Laboratory
4
- # Copyright 2021 DeepMind Technologies Limited
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
-
18
- import math
19
-
20
- import torch
21
-
22
-
23
- def _calculate_fan(linear_weight_shape, fan="fan_in"):
24
- fan_out, fan_in = linear_weight_shape
25
-
26
- if fan == "fan_in":
27
- f = fan_in
28
- elif fan == "fan_out":
29
- f = fan_out
30
- elif fan == "fan_avg":
31
- f = (fan_in + fan_out) / 2
32
- else:
33
- raise ValueError("Invalid fan option")
34
-
35
- return f
36
-
37
-
38
- def trunc_normal_init_(weights, scale=1.0, fan="fan_in"):
39
- shape = weights.shape
40
- f = _calculate_fan(shape, fan)
41
- scale = scale / max(1, f)
42
- std = math.sqrt(scale)
43
- with torch.no_grad():
44
- torch.nn.init.trunc_normal_(weights, mean=0.0, std=std, a=-2 * std, b=2 * std)
45
-
46
-
47
- def lecun_normal_init_(weights):
48
- trunc_normal_init_(weights, scale=1.0)
49
-
50
-
51
- def he_normal_init_(weights):
52
- trunc_normal_init_(weights, scale=2.0)
53
-
54
-
55
- def glorot_uniform_init_(weights):
56
- torch.nn.init.xavier_uniform_(weights, gain=1)
57
-
58
-
59
- def final_init_(weights):
60
- with torch.no_grad():
61
- weights.fill_(0.0)
62
-
63
-
64
- def gating_init_(weights):
65
- with torch.no_grad():
66
- weights.fill_(0.0)
67
-
68
-
69
- def bias_init_zero_(bias):
70
- with torch.no_grad():
71
- bias.fill_(0.0)
72
-
73
-
74
- def bias_init_one_(bias):
75
- with torch.no_grad():
76
- bias.fill_(1.0)
77
-
78
-
79
- def normal_init_(weights):
80
- torch.nn.init.kaiming_normal_(weights, nonlinearity="linear")
81
-
82
-
83
- def ipa_point_weights_init_(weights):
84
- with torch.no_grad():
85
- softplus_inverse_1 = 0.541324854612918
86
- weights.fill_(softplus_inverse_1)
 
1
+ """Utility functions for initializing weights and biases."""
2
+
3
+ # Copyright 2021 AlQuraishi Laboratory
4
+ # Copyright 2021 DeepMind Technologies Limited
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ import math
19
+
20
+ import torch
21
+
22
+
23
+ def _calculate_fan(linear_weight_shape, fan="fan_in"):
24
+ fan_out, fan_in = linear_weight_shape
25
+
26
+ if fan == "fan_in":
27
+ f = fan_in
28
+ elif fan == "fan_out":
29
+ f = fan_out
30
+ elif fan == "fan_avg":
31
+ f = (fan_in + fan_out) / 2
32
+ else:
33
+ raise ValueError("Invalid fan option")
34
+
35
+ return f
36
+
37
+
38
+ def trunc_normal_init_(weights, scale=1.0, fan="fan_in"):
39
+ shape = weights.shape
40
+ f = _calculate_fan(shape, fan)
41
+ scale = scale / max(1, f)
42
+ std = math.sqrt(scale)
43
+ with torch.no_grad():
44
+ torch.nn.init.trunc_normal_(weights, mean=0.0, std=std, a=-2 * std, b=2 * std)
45
+
46
+
47
+ def lecun_normal_init_(weights):
48
+ trunc_normal_init_(weights, scale=1.0)
49
+
50
+
51
+ def he_normal_init_(weights):
52
+ trunc_normal_init_(weights, scale=2.0)
53
+
54
+
55
+ def glorot_uniform_init_(weights):
56
+ torch.nn.init.xavier_uniform_(weights, gain=1)
57
+
58
+
59
+ def final_init_(weights):
60
+ with torch.no_grad():
61
+ weights.fill_(0.0)
62
+
63
+
64
+ def gating_init_(weights):
65
+ with torch.no_grad():
66
+ weights.fill_(0.0)
67
+
68
+
69
+ def bias_init_zero_(bias):
70
+ with torch.no_grad():
71
+ bias.fill_(0.0)
72
+
73
+
74
+ def bias_init_one_(bias):
75
+ with torch.no_grad():
76
+ bias.fill_(1.0)
77
+
78
+
79
+ def normal_init_(weights):
80
+ torch.nn.init.kaiming_normal_(weights, nonlinearity="linear")
81
+
82
+
83
+ def ipa_point_weights_init_(weights):
84
+ with torch.no_grad():
85
+ softplus_inverse_1 = 0.541324854612918
86
+ weights.fill_(softplus_inverse_1)