forked from p-koo/deepomics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.py
205 lines (152 loc) · 5.6 KB
/
init.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import numpy as np
import tensorflow as tf
__all__ = [
"Constant",
"Uniform",
"Normal",
"TruncatedNormal",
"GlorotUniform",
"GlorotNormal",
"HeUniform",
"HeNormal",
"Orthogonal"
]
class Initializer(object):
"""Base class for parameter tensor initializers."""
def __call__(self, shape):
return self.generate(shape)
def generate(self, shape):
raise NotImplementedError()
class Constant(Initializer):
"""Consant"""
def __init__(self, value=0.05, dtype=tf.float32, **kwargs):
self.value = value
self.dtype = dtype
self.kwargs = kwargs
def generate(self, shape):
return tf.constant(shape=shape, value=self.value, dtype=self.dtype, **self.kwargs)
class Uniform(Initializer):
"""Uniform random number"""
def __init__(self, minval=-0.1, maxval=0.1, dtype=tf.float32, **kwargs):
self.minval = minval
self.maxval = maxval
self.dtype = dtype
self.kwargs = kwargs
def generate(self, shape):
return tf.random_uniform(shape=shape, minval=self.minval, maxval=self.maxval, dtype=self.dtype, **self.kwargs)
class Normal(Initializer):
"""Normal distribution"""
def __init__(self, mean=0.0, stddev=0.1, dtype=tf.float32, **kwargs):
self.mean = mean
self.stddev = stddev
self.dtype = dtype
self.kwargs = kwargs
def generate(self, shape):
return tf.random_normal(shape=shape, mean=self.mean, stddev=self.stddev, dtype=self.dtype, **self.kwargs)
class TruncatedNormal(Initializer):
"""Truncated normal distribution"""
def __init__(self, mean=0.0, stddev=0.1, dtype=tf.float32, **kwargs):
self.mean = mean
self.stddev = stddev
self.dtype = dtype
self.kwargs = kwargs
def generate(self, shape):
return tf.truncated_normal(shape=shape, mean=self.mean, stddev=self.stddev, dtype=self.dtype, **self.kwargs)
def get_fans(shape):
"""Get number of input neurons (fan_in) and output neurons (fan_out)"""
if len(shape) == 2:
fan_in = shape[0]
fan_out = shape[1]
elif len(shape) == 4:
receptive_field_size = np.prod(shape[:2])
fan_in = shape[-2] * receptive_field_size
fan_out = shape[-1] * receptive_field_size
else:
# no specific assumptions
fan_in = np.sqrt(np.prod(shape))
fan_out = np.sqrt(np.prod(shape))
return fan_in, fan_out
class GlorotUniform(Initializer):
"""
Glorot Uniform init
References
----------
.. [1] Xavier Glorot and Yoshua Bengio (2010):
Understanding the difficulty of training deep feedforward neural
networks. International conference on artificial intelligence and
statistics.
"""
def __init__(self, dtype=tf.float32, **kwargs):
self.dtype = dtype
self.kwargs = kwargs
def generate(self, shape):
fan_in, fan_out = get_fans(shape)
stddev = np.sqrt(6. / (fan_in + fan_out))
return tf.random_uniform(shape=shape, minval=-stddev, maxval=stddev, dtype=self.dtype, **self.kwargs)
class GlorotNormal(Initializer):
"""
Glorot Normal init
References
----------
.. [1] Xavier Glorot and Yoshua Bengio (2010):
Understanding the difficulty of training deep feedforward neural
networks. International conference on artificial intelligence and
statistics.
"""
def __init__(self, mean=0.0, dtype=tf.float32, **kwargs):
self.mean = mean
self.dtype = dtype
self.kwargs = kwargs
def generate(self, shape):
fan_in, fan_out = get_fans(shape)
stddev = np.sqrt(2. / (fan_in + fan_out))
return tf.truncated_normal(shape=shape, mean=self.mean, stddev=stddev, dtype=self.dtype, **self.kwargs)
class HeUniform(Initializer):
"""
He Uniform init
References
----------
.. [1] Kaiming He et al. (2015):
Delving deep into rectifiers: Surpassing human-level performance on
imagenet classification. arXiv preprint arXiv:1502.01852.
"""
def __init__(self, dtype=tf.float32, **kwargs):
self.dtype = dtype
self.kwargs = kwargs
def generate(self, shape):
fan_in, fan_out = get_fans(shape)
stddev = np.sqrt(6. / fan_in)
return tf.random_uniform(shape=shape, minval=-stddev, maxval=stddev, dtype=self.dtype, **self.kwargs)
class HeNormal(Initializer):
"""
He Normal init
References
----------
.. [1] Kaiming He et al. (2015):
Delving deep into rectifiers: Surpassing human-level performance on
imagenet classification. arXiv preprint arXiv:1502.01852.
"""
def __init__(self, mean=0.0, dtype=tf.float32, **kwargs):
self.mean = mean
self.dtype = dtype
self.kwargs = kwargs
def generate(self, shape):
fan_in, fan_out = get_fans(shape)
stddev = np.sqrt(2. / fan_in)
return tf.truncated_normal(shape=shape, mean=self.mean, stddev=stddev, dtype=self.dtype, **self.kwargs)
class Orthogonal(Initializer):
"""
Orthogonal init
"""
def __init__(self, gain=1.1, dtype=tf.float32, **kwargs):
self.gain = gain
self.dtype = dtype
self.kwargs = kwargs
def generate(self, shape):
flat_shape = (shape[0], np.prod(shape[1:]))
a = np.random.normal(0.0, 1.0, flat_shape)
u, _, v = np.linalg.svd(a, full_matrices=False)
# pick the one with the correct shape
q = u if u.shape == flat_shape else v
q = q.reshape(shape)
return tf.cast(self.gain * q[:shape[0], :shape[1]], dtype=q.dtype)