/
uniform.py
151 lines (116 loc) · 5.08 KB
/
uniform.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
import numpy
from chainer import backend
from chainer import initializer
from chainer.utils import argument
# Original code forked from MIT licensed keras project
# https://github.com/fchollet/keras/blob/master/keras/initializations.py
class Uniform(initializer.Initializer):
"""Initializes array with a scaled uniform distribution.
Each element of the array is initialized by the value drawn
independently from uniform distribution :math:`[-scale, scale]`.
Attributes:
scale (float): A constant that determines the
scale of the uniform distribution.
dtype: Data type specifier.
rng (xp.random.RandomState): Pseudo-random number generator.
"""
def __init__(self, scale=0.05, dtype=None, **kwargs):
self.scale = scale
rng = None
if kwargs:
rng, = argument.parse_kwargs(kwargs, ('rng', rng))
self.rng = rng
super(Uniform, self).__init__(dtype)
def __call__(self, array):
if self.dtype is not None:
assert array.dtype == self.dtype,\
'{} != {}'.format(array.dtype, self.dtype)
if self.rng is None:
device = backend.get_device_from_array(array)
array[...] = device.xp.random.uniform(
low=-self.scale, high=self.scale, size=array.shape)
else:
backend.copyto(array, self.rng.uniform(
low=-self.scale, high=self.scale,
size=array.shape).astype(array.dtype, copy=False))
class LeCunUniform(initializer.Initializer):
"""Initializes array with a scaled uniform distribution.
Each element of the array is initialized by the value drawn
independently from uniform distribution :math:`[-s, s]`
where :math:`s = scale \\times \\sqrt{\\frac{3}{fan_{in}}}`.
Here :math:`fan_{in}` is the number of input units.
Reference: LeCun 98, Efficient Backprop
http://yann.lecun.com/exdb/publis/pdf/lecun-98b.pdf
Attributes:
scale (float): A constant that determines the
scale of the uniform distribution.
dtype: Data type specifier.
rng (xp.random.RandomState): Pseudo-random number generator.
"""
def __init__(self, scale=1.0, dtype=None, **kwargs):
self.scale = scale
rng = None
if kwargs:
rng, = argument.parse_kwargs(kwargs, ('rng', rng))
self.rng = rng
super(LeCunUniform, self).__init__(dtype)
def __call__(self, array):
if self.dtype is not None:
assert array.dtype == self.dtype,\
'{} != {}'.format(array.dtype, self.dtype)
fan_in, fan_out = initializer.get_fans(array.shape)
s = self.scale * numpy.sqrt(3. / fan_in)
Uniform(s, rng=self.rng)(array)
class GlorotUniform(initializer.Initializer):
"""Initializes array with a scaled uniform distribution.
Each element of the array is initialized by the value drawn
independently from uniform distribution :math:`[-s, s]`
where :math:`s = scale \\times \\sqrt{\\frac{6}{fan_{in} + fan_{out}}}`.
Here, :math:`fan_{in}` and :math:`fan_{out}` are the number of
input and output units, respectively.
Attributes:
scale (float): A constant that determines the
scale of the uniform distribution.
dtype: Data type specifier.
rng (xp.random.RandomState): Pseudo-random number generator.
"""
def __init__(self, scale=1.0, dtype=None, **kwargs):
self.scale = scale
rng = None
if kwargs:
rng, = argument.parse_kwargs(kwargs, ('rng', rng))
self.rng = rng
super(GlorotUniform, self).__init__(dtype)
def __call__(self, array):
if self.dtype is not None:
assert array.dtype == self.dtype,\
'{} != {}'.format(array.dtype, self.dtype)
fan_in, fan_out = initializer.get_fans(array.shape)
s = self.scale * numpy.sqrt(6. / (fan_in + fan_out))
Uniform(s, rng=self.rng)(array)
class HeUniform(initializer.Initializer):
"""Initializes array with scaled uniform distribution.
Each element of the array is initialized by the value drawn
independently from uniform distribution :math:`[-s, s]`
where :math:`s = scale \\times \\sqrt{\\frac{6}{fan_{in}}}`.
Here, :math:`fan_{in}` is the number of input units.
Attributes:
scale (float): A constant that determines the
scale of the uniform distribution.
dtype: Data type specifier.
rng (xp.random.RandomState): Pseudo-random number generator.
"""
def __init__(self, scale=1.0, dtype=None, **kwargs):
self.scale = scale
rng = None
if kwargs:
rng, = argument.parse_kwargs(kwargs, ('rng', rng))
self.rng = rng
super(HeUniform, self).__init__(dtype)
def __call__(self, array):
if self.dtype is not None:
assert array.dtype == self.dtype,\
'{} != {}'.format(array.dtype, self.dtype)
fan_in, fan_out = initializer.get_fans(array.shape)
s = self.scale * numpy.sqrt(6. / fan_in)
Uniform(s, rng=self.rng)(array)