-
Notifications
You must be signed in to change notification settings - Fork 1
/
layer.py
265 lines (205 loc) · 7.96 KB
/
layer.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import numpy as np
import theano
import theano.tensor as T
from blocks.bricks import MLP, Activation, Identity, Initializable, Sequence, Feedforward
from blocks.bricks.base import application
from blocks.utils import shared_floatx_zeros
from blocks.roles import add_role, WEIGHTS, BIASES
from blocks.initialization import IsotropicGaussian
class Layer :
@application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
raise NotImplementedError(str(type(self))
+ " does not implement apply.")
def inv_fprop(self, state):
raise NotImplementedError(str(type(self))
+"does not implement inv_fprop")
def get_fprop_and_log_det_jacobian(self, state_below):
raise NotImplementedError(str(type(self))
+"does not implement get_fprop_and_log_det_jacobian ")
def sparsity(self, alpha):
return 0.
class Homothety(Initializable,Feedforward, Layer):
def __init__(self, dim, **kwargs):
super(Homothety,self).__init__(**kwargs)
assert type(dim) is int
self.dim=dim
@property
def D(self):
return self.params[0]
def _allocate(self):
D=shared_floatx_zeros((self.dim,), name='D')
add_role(D, WEIGHTS)
self.params.append(D)
def _initialize(self):
D, =self.params
self.weights_init.initialize(D,self.rng)
@application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
D, = self.params
state = input_ * T.exp(D).flatten()
return state
def get_dim(self, name):
return self.dim
def inv_fprop(self, state):
"""
Inversion of the Homothety forward propagation.
Parameters
----------
state : tensor_like, member of self.output_space
The state above the layer
Returns
-------
state_below : tensor_like
The resulting state below
"""
D, = self.params
state_below = state * T.exp(-D).flatten()
return state_below
def get_fprop_and_log_det_jacobian(self, state_below):
"""
Get the state of the layer and the log-Jacobian determinant of the
transformation.
Parameters
----------
state_below : tensor_like, member of self.input_space
A minibatch of states below.
Returns
-------
state : tensor_like, member of self.output_space
A minibatch of states of this layer.
log_det_jac : tensor_like
Log Jacobian determinant of the transformation
"""
D, = self.params
return self.apply(state_below), self.D.sum()
class Reordering(Initializable, Layer):
def __init__(self, dim, mode='tile', ** kwargs):
super(Reordering, self).__init__(** kwargs)
assert mode in ['tile', 'reverse']
assert type(dim) is int
self.dim = dim
self.mode = mode
half_dim = int(self.dim/2)
self.permutation =np.arange(self.dim)
if self.mode=='tile':
tmp=self.permutation.copy()
self.permutation[:half_dim]=tmp[::2]
self.permutation[-half_dim:] = tmp[1::2]
elif self.mode =='reverse':
self.permutation = self.permutation[::-1]
self.params=[]
@application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
return input_[:,self.permutation]
def inv_fprop(self, state):
"""
Inversion of the Reordering forward propagation.
Parameters
----------
state : tensor_like, member of self.output_space
The state above the layer
Returns
-------
state_below : tensor_like
The resulting state below
"""
state_below = state[:, np.argsort(self.permutation)]
return state_below
def get_fprop_and_log_det_jacobian(self, state_below):
"""
Get the state of the layer and the log-Jacobian determinant of the
transformation.
Parameters
----------
state_below : tensor_like, member of self.input_space
A minibatch of states below.
Returns
-------
state : tensor_like, member of self.output_space
A minibatch of states of this layer.
log_det_jac : tensor_like
Log Jacobian determinant of the transformation
"""
return self.apply(state_below), 0.
class CouplingFunction(Sequence,Initializable,Layer):
def __init__(self, dim, activations, split=0.5, reverse=True, **kwargs):
self.activations = activations
application_methods =[bricks.apply for bricks in activations]
super(CouplingFunction, self).__init__(application_methods=application_methods,**kwargs)
assert (split>0 and split <1) #percentage of the data
self.split=split
self.reverse=reverse
self.dim = int(self.split*dim)
self.dims=[self.dim for i in xrange(len(self.activations)+1)]
self.permutation = T.arange(dim)
if self.reverse:
self.permutation= self.permutation[::-1]
def _push_allocation_config(self):
for function in self.activations:
#check that function has push_allocation_config() as a method
if hasattr(function,'push_allocation_config'):
function.push_allocation_config()
def get_dim(self, name):
return dim
def function(self, x):
input_=x
for layer in self.activations:
result_temp = layer.apply(input_)
input_=result_temp
return input_
@application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
#first split the input
#apply the coupling function
# exchange data
shape = input_.shape[1]
#index = T.cast(T.round(self.split*shape),'int32')
index = self.dim
coupling_out = self.function(input_[:,:index])
state = T.inc_subtensor(input_[:, index:], coupling_out)
#define permutation
state = state[:, self.permutation]
return state
def inv_fprop(self, output_):
shape = output_.shape[1]
index = self.dim
state_below = output_
state_below = state_below[:, T.argsort(self.permutation)]
coupling_out = -self.function(state_below[:, :index])
state_below = T.inc_subtensor(state_below[:, index:],
coupling_out)
return state_below
def get_fprop_and_log_det_jacobian(self, state_below):
log_det_jac = 0.
return self.apply(state_below), log_det_jac
class Branch(Sequence,Initializable,Layer):
def __init__(self, dim, branch_to_rebuild=0, weights_init=IsotropicGaussian(0.01), **kwargs):
self.branches=[Homothety(dim, weights_init=weights_init, use_bias=False) for i in xrange(2)]
application_methods =[bricks.apply for bricks in self.branches]
assert type(dim) is int
assert branch_to_rebuild>=0 and branch_to_rebuild<len(self.branches)
self.dim=dim
super(Branch,self).__init__(application_methods=application_methods, **kwargs)
self.branch_to_rebuild=branch_to_rebuild
def _push_allocation_config(self):
for branches in self.branches :
branches.push_allocation_config()
@application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
return self.branches[0].apply(input_) #+ self.branches[1].apply(input_) #TODO : more branches, pas de code en dur
def inv_fprop(self, state):
return self.branches[self.branch_to_rebuild].inv_fprop(state)
def get_fprop_and_log_det_jacobian(self, state_below):
D0, = self.branches[0].params
D1, = self.branches[1].params
return self.apply(state_below), (D0+D1).sum()
def set_branch_to_rebuild(index):
assert index >=0 and index <len(self.branches)
self.branch_to_rebuild=index
@application(inputs=['input_'], outputs=['output'])
def apply_branch(self, input_):
return self.branches[self.branch_to_rebuild].apply(input_)
def sparsity(self, alpha):
D, =self.branches[self.branch_to_rebuild].params
return alpha*(D**2).sum()