-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitializer.py
25 lines (22 loc) · 1.01 KB
/
initializer.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
import numpy as np
# Initialize Weights (and biases)
def initialize_weight(shape, bias=True, initializer="xavier"):
"""
Initialize weights according to initializer
[1] [ReLU] He Initialization : https://arxiv.org/pdf/1502.01852
[2] [Tanh] Xavier(Caffe version)
[3] [Sigmoid] Xavier Initialization : http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf
"""
if (initializer=="He"): #
w = np.random.randn(*shape) * np.sqrt(2/shape[0])
b = np.random.randn(1, shape[1]) * np.sqrt(2/shape[0])
elif (initializer=="xavier"):
w = np.random.randn(*shape) * np.sqrt(1/shape[0])
b = np.random.randn(1, shape[1]) * np.sqrt(1/shape[0])
elif (initializer=="xavier_orig"):
assert shape[0] > shape[1] # Original two-sided Xavier initialization
w = np.random.randn(*shape) * np.sqrt(2/(shape[0]-shape[1]))
b = np.random.randn(1, shape[1]) * np.sqrt(2/(shape[0]-shape[1]))
if (~bias):
b = np.zeros((1, shape[1]))
return w, b