-
Notifications
You must be signed in to change notification settings - Fork 0
/
Initialization.py
142 lines (114 loc) · 5.32 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import numpy as np
########################################################################################################################
"""
GROUP: Parameter Initialization
-Handles the initialization of the neural networks weights and biases
EXTERNAL FUNCTIONS:
1) initialize_parameters: initializes the weights and bias according to the init_type selected
INTERNAL FUNCTIONS:
1)initializ1e_parameters_xavier: uses the "Xavier" algorith for initialization
2)initializ1e_parameters_he: uses the "He" algorithm for initialization
3)initialize_parameters_random: initializes the weights by randNum[0,1)*scale and the biases to zero
"""
########################################################################################################################
###EXTERNAL FUNCTIONS###
def initialize_parameters(layers_dims, init_type, weight_scale=1, seed=3):
"""
Description:
initializes the weights and bias according to the init_type selected.
Arguments:
layer_dims -- python array (list) containing the size of each layer.
init_type -- the type of initialize method to use.
Optional Arguments:
weight_scale -- the scale to which an initialization technique will assign weights
seed -- seed use to intialize the numpy random function
Returns:
parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL":
W1 -- weight matrix of shape (layers_dims[1], layers_dims[0])
b1 -- bias vector of shape (layers_dims[1], 1)
...
WL -- weight matrix of shape (layers_dims[L], layers_dims[L-1])
bL -- bias vector of shape (layers_dims[L], 1)
"""
if init_type == "random":
parameters = initialize_parameters_random(layers_dims,weight_scale,seed)
elif init_type == "he":
parameters = initialize_parameters_he(layers_dims,seed)
elif init_type == "xaiver":
parameters = initialize_parameters_xavier(layers_dims,seed)
else:
print("ERROR: intitialize_parameters - no init_type was selected")
print("init_type=" + init_type)
sys.exit(1)
return parameters
###INTERNAL FUNCTIONS###
def initialize_parameters_xavier(layers_dims,seed):
"""
Description:
Xavier initialization uses a scaling factor for the weights of `sqrt(1./layers_dims[l-1])`
Arguments:
layer_dims -- python array (list) containing the size of each layer.
seed -- seed use to intialize the numpy random function
Returns:
parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL":
W1 -- weight matrix of shape (layers_dims[1], layers_dims[0])
b1 -- bias vector of shape (layers_dims[1], 1)
...
WL -- weight matrix of shape (layers_dims[L], layers_dims[L-1])
bL -- bias vector of shape (layers_dims[L], 1)
"""
np.random.seed(seed)
parameters = {}
L = len(layers_dims)
for l in range(1, L):
parameters['W' + str(l)] = np.random.randn(layers_dims[l], layers_dims[l-1]) * np.sqrt(1.0/layers_dims[l-1])
parameters['b' + str(l)] = np.zeros((layers_dims[l],1))
return parameters
def initialize_parameters_he(layers_dims,seed):
"""
Description:
He initialization is a published technique in 2015 similiar to Xavier initialization.
-Xavier initialization uses a scaling factor for the weights of `sqrt(1./layers_dims[l-1])`
-He initialization would use `sqrt(2./layers_dims[l-1])
He initialization recommended for layers with a ReLU activation.
Arguments:
layer_dims -- python array (list) containing the size of each layer.
seed -- seed use to intialize the numpy random function
Returns:
parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL":
W1 -- weight matrix of shape (layers_dims[1], layers_dims[0])
b1 -- bias vector of shape (layers_dims[1], 1)
...
WL -- weight matrix of shape (layers_dims[L], layers_dims[L-1])
bL -- bias vector of shape (layers_dims[L], 1)
"""
np.random.seed(seed)
parameters = {}
L = len(layers_dims)
for l in range(1, L):
parameters['W' + str(l)] = np.random.randn(layers_dims[l], layers_dims[l-1]) * np.sqrt(2.0/layers_dims[l-1])
parameters['b' + str(l)] = np.zeros((layers_dims[l],1))
return parameters
def initialize_parameters_random(layers_dims,weight_scale,seed):
"""
Description:
initializes the weights of all neurons randomly between [0,1)*scale and their biases to zero.
Arguments:
layer_dims -- python array (list) containing the size of each layer.
weight_scale -- scalar to adjust the weight of the random numbers
seed -- seed use to intialize the numpy random function
Returns:
parameters -- python dictionary containing your parameters "W1", "b1", ..., "WL", "bL":
W1 -- weight matrix of shape (layers_dims[1], layers_dims[0])
b1 -- bias vector of shape (layers_dims[1], 1)
...
WL -- weight matrix of shape (layers_dims[L], layers_dims[L-1])
bL -- bias vector of shape (layers_dims[L], 1)
"""
np.random.seed(seed) # This seed makes sure your "random" numbers will be the as ours
parameters = {}
L = len(layers_dims) # integer representing the number of layers
for l in range(1, L):
parameters['W' + str(l)] = np.random.randn(layers_dims[l],layers_dims[l-1])*weight_scale
parameters['b' + str(l)] = np.zeros((layers_dims[l],1))
return parameters