-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.py
614 lines (305 loc) · 13.5 KB
/
module.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from torch import FloatTensor
from torch import LongTensor
from initializer import *
class Module(object):
def __init__(self):
self.next_module=None
self.gradient=None
self.output=None
self.phase="train"
def forward(self , *input):
# If the layer before did not do forward propagation,then force the layer before do the forward propagation first
if (self.prev_module is not None) and (self.prev_module.output is None):
self.prev_module.forward(False)
def backward(self , *gradwrtoutput):
# If the layer after did not do backward propagation,then force the layer after do the backward propagation first
if (self.next_module is not None) and (self.next_module.gradient is None):
self.next_module.backward(False)
def param(self):
return []
def update(self,lr):
pass
def change_to_test(self):
self.phase="test"
def change_to_train(self):
self.phase="train"
class Linear(Module):
def __init__(self,output_dimension,prev_module=None,weight_initializer=gaussian_initializer(),bias_initializer=zeros_initializer()):
super(Linear,self).__init__()
self.__w=None
self.__b=None
self.prev_module=prev_module
if self.prev_module is not None:
self.prev_module.next_module=self
self.output_dimension=output_dimension
self.__b_gradient=0
self.__w_gradient=0
self.weight_initializer=weight_initializer
self.bias_initializer=bias_initializer
def forward(self,pro=False):
'''
Parameter:
pro :If True, automatically do the next layers forward propagation after finishing the computation of this layer.Default is False.
'''
super(Linear,self).forward()
if self.__w is None:
self.__w=FloatTensor(self.output_dimension,list(self.prev_module.output.size())[0])
self.__w=self.weight_initializer.initialize(self.__w)
if self.__b is None:
self.__b=FloatTensor(self.output_dimension, 1).zero_()
self.__b=self.bias_initializer.initialize(self.__b)
# Compute the output by output=Wx+b
self.output=self.__w.mm(self.prev_module.output)+self.__b
if self.next_module is not None:
if pro is True:
self.next_module.forward(True)
def param(self):
return [(self.__w, self.__w_gradient),(self.__b,self.__b_gradient)]
def backward(self,pro=False):
'''
Parameter:
pro :If True, automatically do the last layers backward propagation after finishing the computation of this layer.Default is False.
'''
# Make sure all the networks finished the forward process and the layers after have already finished the backward process.
super(Linear,self).forward()
super(Linear,self).backward()
self.gradient=self.__w.t()
if self.next_module is not None:
# Gradient of b is just the gradient back propagate by the next layer
self.__b_gradient=self.next_module.gradient
# Gradient of w is the gradient back propagate by the next layer and the gradient comes from matrix multiply,which is x
self.__w_gradient=self.next_module.gradient.mm(self.prev_module.output.t())
# Gradient of this layer is the gradient back propagate by the next layer and the gradient comes from matrix multiply,which is W
self.gradient=self.gradient.mm(self.next_module.gradient)
if self.prev_module is not None:
if pro is True:
self.prev_module.backward(True)
def update(self,lr):
# Compute the average gradient in the gradient list and apply them to the parameters
self.__w=self.__w-lr*self.__w_gradient/list(self.prev_module.output.size())[1]
self.__b=self.__b-lr*self.__b_gradient.mean(1,True)
class Inputholder(Module):
'''
This layer is a holder of data.Every neural network should begin with this layer and use the input_value property to pass train or test data.
'''
def __init__(self,input_value=None):
super(Inputholder,self).__init__()
self.prev_module=None
self.input_value=input_value
def forward(self,pro=False):
super(Inputholder,self).forward()
self.output=FloatTensor(self.input_value).t()
if self.next_module is not None:
if pro is True:
self.next_module.forward(True)
def backward(self,pro=False):
super(Inputholder,self).forward()
super(Inputholder,self).backward()
if self.next_module is not None:
self.gradient=self.next_module.gradient
'''if self.prev_module is not None:
if pro is True:
self.prev_module.backward(True)
'''
class ReLU(Module):
def __init__(self,prev_module=None):
super(ReLU,self).__init__()
self.prev_module=prev_module
if self.prev_module is not None:
self.prev_module.next_module=self
def forward(self,pro=False):
super(ReLU,self).forward()
self.output=0.5*(self.prev_module.output+self.prev_module.output.abs())
if self.next_module is not None:
if pro is True:
self.next_module.forward(True)
def backward(self,pro=False):
super(ReLU,self).forward()
super(ReLU,self).backward()
if self.next_module is not None:
self.gradient=0.5*(self.prev_module.output.sign()+1)
self.gradient=self.gradient*self.next_module.gradient
if self.prev_module is not None:
if pro is True:
self.prev_module.backward(True)
class Tanh(Module):
def __init__(self,prev_module=None):
super(Tanh,self).__init__()
self.prev_module=prev_module
if self.prev_module is not None:
self.prev_module.next_module=self
def forward(self,pro=False):
super(Tanh,self).forward()
self.output=self.prev_module.output.tanh()
if self.next_module is not None:
if pro is True:
self.next_module.forward(True)
def backward(self,pro=False):
super(Tanh,self).forward()
super(Tanh,self).backward()
if self.next_module is not None:
self.gradient=1-(self.prev_module.output.tanh())**2
self.gradient=self.gradient*self.next_module.gradient
if self.prev_module is not None:
if pro is True:
self.prev_module.backward(True)
class Sigmoid(Module):
def __init__(self,prev_module=None):
super(Sigmoid,self).__init__()
self.prev_module=prev_module
if self.prev_module is not None:
self.prev_module.next_module=self
def forward(self,pro=False):
super(Sigmoid,self).forward()
self.output=self.prev_module.output.sigmoid()
if self.next_module is not None:
if pro is True:
self.next_module.forward(True)
def backward(self,pro=False):
super(Sigmoid,self).forward()
super(Sigmoid,self).backward()
if self.next_module is not None:
self.gradient=self.output*(1-self.output)
self.gradient=self.gradient*self.next_module.gradient
if self.prev_module is not None:
if pro is True:
self.prev_module.backward(True)
class LossMSE(Module):
def __init__(self,prev_module=None,target=None):
super(LossMSE,self).__init__()
self.prev_module=prev_module
if self.prev_module is not None:
self.prev_module.next_module=self
if target is not None:
self.target=FloatTensor(target)
def forward(self,pro=False):
super(LossMSE,self).forward()
# Only do forward process in training time
if self.phase =="train":
self.output=((self.prev_module.output-self.target)**2).sum(0,True)
if self.next_module is not None:
if pro is True:
self.next_module.forward(True)
def backward(self,pro=False):
super(LossMSE,self).forward()
super(LossMSE,self).backward()
self.gradient=2*(self.prev_module.output-self.target)
if self.next_module is not None:
self.gradient=self.gradient*(self.next_module.gradient)
if self.prev_module is not None:
if pro is True:
self.prev_module.backward(True)
class Softmax(Module):
def __init__(self,prev_module=None):
super(Softmax,self).__init__()
self.prev_module=prev_module
if self.prev_module is not None:
self.prev_module.next_module=self
def forward(self,pro=False):
super(Softmax,self).forward()
self.output=(self.prev_module.output-self.prev_module.output.max(0,True)[0]).exp()
self.output=self.output/self.output.sum(0,True)
if self.next_module is not None:
if pro is True:
self.next_module.forward(True)
def backward(self,pro=False):
super(Softmax,self).forward()
super(Softmax,self).backward()
if self.next_module is not None:
# Change axis to batch first
self.gradient=self.output.t().clone()
s=list(self.gradient.size())[0]
# Rearange to contiguous to view
self.gradient = self.gradient.contiguous()
# Make a column vector and a row vector to compute the matrix for computing gradient
self.gradient_1 = self.gradient.view(s,-1,1).clone()
self.gradient = self.gradient.contiguous()
self.gradient_2 = self.gradient.view(s,1,-1).clone()
# Compute the gradient matrix
self.gradient = self.gradient_1.bmm(self.gradient_2)
self.gradient = -1*self.gradient.bmm(self.next_module.gradient.t().contiguous().view(s,-1,1))
# Multiply also the gradient afterwards and add the extra gradient for i=j
self.gradient = self.gradient.contiguous().view(s,-1).t()+self.next_module.gradient*self.output
if self.prev_module is not None:
if pro is True:
self.prev_module.backward(True)
class LossCrossEntropy(Module):
def __init__(self,prev_module=None,target=None):
super(LossCrossEntropy,self).__init__()
self.prev_module=prev_module
if self.prev_module is not None:
self.prev_module.next_module=self
self.eps=1e-20
if target is not None:
self.target=FloatTensor(target)
def forward(self,pro=False):
super(LossCrossEntropy,self).forward()
# Only do forward process in training time
if self.phase =="train":
self.output=-1*((self.prev_module.output+self.eps).log()*self.target).sum(0,True)
if self.next_module is not None:
if pro is True:
self.next_module.forward(True)
def backward(self,pro=False):
super(LossCrossEntropy,self).forward()
super(LossCrossEntropy,self).backward()
self.gradient=-1*(1/(self.prev_module.output+self.eps)*self.target)
if self.next_module is not None:
self.gradient=self.gradient*(self.next_module.gradient)
if self.prev_module is not None:
if pro is True:
self.prev_module.backward(True)
class Sequential(Module):
def __init__(self,*args):
super(Sequential,self).__init__()
self.prev_module = None
# Get all the layers
self.args = list(args)
# Connect all the layers
for lp,ln in zip(self.args[0:-1],self.args[1:]):
ln.prev_module = lp
lp.next_module = ln
self.args[0].prev_module=self.prev_module
self.args[-1].next_module=self.next_module
def forward(self,pro=False):
super(Sequential,self).forward()
# Put the input_value to the inputholder
if self.input_value is not None:
self.args[0].input_value = self.input_value
# Put the target to the loss layer
if self.target is not None:
self.args[-1].target = self.target
# Do forward pass
self.args[0].forward(True)
# Get the output from the last layer
self.output = self.args[-1].output
def backward(self,pro=False):
super(Sequential,self).forward()
super(Sequential,self).backward()
# Do backward
self.args[-1].backward(True)
# Get the gradient from the last layer
self.gradient = self.args[0].gradient
def param(self):
params = {}
# Find param from all the layers
for index,l in enumerate(self.args):
p = l.param()
if p != []:
# Put the type and the number of the layer as a index, and store the parameters
params[type(l).__name__+" layer "+str(index)]= p
return params
def update(self,lr):
for l in self.args:
l.update(lr)
def get_layer(self,index):
# Return the middle layer
return self.args[index]
def change_to_test(self):
self.phase="test"
for l in self.args:
l.change_to_test()
def change_to_train(self):
self.phase="train"
for l in self.args:
l.change_to_train()