1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import numpy as np |
5
|
|
|
from deepy.core.env import env |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
def get_fans(shape): |
9
|
|
|
fan_in = shape[0] if len(shape) == 2 else np.prod(shape[1:]) |
10
|
|
|
fan_out = shape[1] if len(shape) == 2 else shape[0] |
11
|
|
|
return fan_in, fan_out |
12
|
|
|
|
13
|
|
|
class WeightInitializer(object): |
14
|
|
|
""" |
15
|
|
|
Initializer for creating weights. |
16
|
|
|
""" |
17
|
|
|
|
18
|
|
|
def __init__(self, seed=None): |
19
|
|
|
if not seed: |
20
|
|
|
self.rand = env.numpy_rand |
21
|
|
|
else: |
22
|
|
|
self.rand = np.random.RandomState(seed) |
23
|
|
|
|
24
|
|
|
def sample(self, shape): |
25
|
|
|
""" |
26
|
|
|
Sample parameters with given shape. |
27
|
|
|
""" |
28
|
|
|
raise NotImplementedError |
29
|
|
|
|
30
|
|
|
class UniformInitializer(WeightInitializer): |
31
|
|
|
""" |
32
|
|
|
Uniform weight sampler. |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
def __init__(self, scale=None, svd=False, seed=None): |
36
|
|
|
super(UniformInitializer, self).__init__(seed) |
37
|
|
|
self.scale = scale |
38
|
|
|
self.svd = svd |
39
|
|
|
|
40
|
|
|
def sample(self, shape): |
41
|
|
|
if not self.scale: |
42
|
|
|
scale = np.sqrt(6. / sum(get_fans(shape))) |
43
|
|
|
else: |
44
|
|
|
scale = self.scale |
45
|
|
|
weight = self.rand.uniform(-1, 1, size=shape) * scale |
46
|
|
|
if self.svd: |
47
|
|
|
norm = np.sqrt((weight**2).sum()) |
48
|
|
|
ws = scale * weight / norm |
49
|
|
|
_, v, _ = np.linalg.svd(ws) |
50
|
|
|
ws = scale * ws / v[0] |
51
|
|
|
return weight |
52
|
|
|
|
53
|
|
|
class GaussianInitializer(WeightInitializer): |
54
|
|
|
""" |
55
|
|
|
Gaussian weight sampler. |
56
|
|
|
""" |
57
|
|
|
|
58
|
|
|
def __init__(self, mean=0, deviation=0.01, seed=None): |
59
|
|
|
super(GaussianInitializer, self).__init__(seed) |
60
|
|
|
self.mean = mean |
61
|
|
|
self.deviation = deviation |
62
|
|
|
|
63
|
|
|
def sample(self, shape): |
64
|
|
|
weight = self.rand.normal(self.mean, self.deviation, size=shape) |
65
|
|
|
return weight |
66
|
|
|
|
67
|
|
|
class IdentityInitializer(WeightInitializer): |
68
|
|
|
""" |
69
|
|
|
Initialize weight as identity matrices. |
70
|
|
|
""" |
71
|
|
|
|
72
|
|
|
def __init__(self, scale=1): |
73
|
|
|
super(IdentityInitializer, self).__init__() |
74
|
|
|
self.scale = 1 |
75
|
|
|
|
76
|
|
|
def sample(self, shape): |
77
|
|
|
assert len(shape) == 2 |
78
|
|
|
return np.eye(*shape) * self.scale |
79
|
|
|
|
80
|
|
|
class XavierGlorotInitializer(WeightInitializer): |
81
|
|
|
""" |
82
|
|
|
Xavier Glorot's weight initializer. |
83
|
|
|
See http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf |
84
|
|
|
""" |
85
|
|
|
|
86
|
|
|
def __init__(self, uniform=False, seed=None): |
87
|
|
|
""" |
88
|
|
|
Parameters: |
89
|
|
|
uniform - uniform distribution, default Gaussian |
90
|
|
|
seed - random seed |
91
|
|
|
""" |
92
|
|
|
super(XavierGlorotInitializer, self).__init__(seed) |
93
|
|
|
self.uniform = uniform |
94
|
|
|
|
95
|
|
|
def sample(self, shape): |
96
|
|
|
scale = np.sqrt(2. / sum(get_fans(shape))) |
97
|
|
|
if self.uniform: |
98
|
|
|
return self.rand.uniform(-1, 1, size=shape) * scale |
99
|
|
|
else: |
100
|
|
|
return self.rand.randn(*shape) * scale |
101
|
|
|
|
102
|
|
|
class KaimingHeInitializer(WeightInitializer): |
103
|
|
|
""" |
104
|
|
|
Kaiming He's initialization scheme, especially made for ReLU. |
105
|
|
|
See http://arxiv.org/abs/1502.01852. |
106
|
|
|
""" |
107
|
|
|
def __init__(self, uniform=False, seed=None): |
108
|
|
|
""" |
109
|
|
|
Parameters: |
110
|
|
|
uniform - uniform distribution, default Gaussian |
111
|
|
|
seed - random seed |
112
|
|
|
""" |
113
|
|
|
super(KaimingHeInitializer, self).__init__(seed) |
114
|
|
|
self.uniform = uniform |
115
|
|
|
|
116
|
|
|
def sample(self, shape): |
117
|
|
|
fan_in, fan_out = get_fans(shape) |
118
|
|
|
scale = np.sqrt(2. / fan_in) |
119
|
|
|
if self.uniform: |
120
|
|
|
return self.rand.uniform(-1, 1, size=shape) * scale |
121
|
|
|
else: |
122
|
|
|
return self.rand.randn(*shape) * scale |
123
|
|
|
|
124
|
|
|
class OrthogonalInitializer(WeightInitializer): |
125
|
|
|
""" |
126
|
|
|
Orthogonal weight initializer. |
127
|
|
|
""" |
128
|
|
|
def __init__(self, scale=1.1, seed=None): |
129
|
|
|
""" |
130
|
|
|
Parameters: |
131
|
|
|
scale - scale |
132
|
|
|
seed - random seed |
133
|
|
|
""" |
134
|
|
|
super(OrthogonalInitializer, self).__init__(seed) |
135
|
|
|
self.scale = scale |
136
|
|
|
|
137
|
|
|
def sample(self, shape): |
138
|
|
|
flat_shape = (shape[0], np.prod(shape[1:])) |
139
|
|
|
a = np.random.normal(0.0, 1.0, flat_shape) |
140
|
|
|
u, _, v = np.linalg.svd(a, full_matrices=False) |
141
|
|
|
q = u if u.shape == flat_shape else v |
142
|
|
|
q = q.reshape(shape) |
143
|
|
|
return self.scale * q[:shape[0], :shape[1]] |
144
|
|
|
|