-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.py
385 lines (284 loc) · 11.8 KB
/
nodes.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import numpy as np
from abc import ABCMeta, abstractmethod
class Connection:
def __init__(self, name="", init_value=None):
self._name = name
self._init_value = init_value
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
@property
def init_value(self):
return self._init_value
def __str__(self):
return self._name
class ConnectionData:
def __init__(self, value=None, gradient=None):
self._value = value
self._gradient = gradient
@property
def value(self):
return self._value
@value.setter
def value(self, v):
self._value = v
@property
def gradient(self):
return self._gradient
@gradient.setter
def gradient(self, value):
if self.gradient is None or value is None:
self._gradient = value
else:
self._gradient += value
def reset_gradient(self, to_value=None):
self._gradient = to_value
class Variable(Connection):
def __init__(self, name="", init_value=None, shape=None):
super().__init__(name, init_value)
self._shape = shape
class Constant(Connection):
def __init__(self, init_value=None, name=""):
super().__init__(name, init_value)
def __str__(self):
return 'constant={}'.format(self._init_value)
class Node:
__metaclass__ = ABCMeta
def __init__(self, name, inputs=[], outputs=[]):
self.inputs = inputs
self.outputs = outputs
self.name = name
@abstractmethod
def forward(self, data_bag):
[data_bag[i].reset_gradient() for i in self.inputs]
@abstractmethod
def backward(self, data_bag): pass
def forward_backward(self, data_bag):
self.forward(data_bag)
self.backward(data_bag)
def __str__(self):
return self.name
class SumNode(Node):
def __init__(self, in1: Connection, in2: Connection, out: Connection):
super().__init__("sum", [in1, in2], [out])
self.in1 = in1
self.in2 = in2
self.out = out
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = data_bag[self.in1].value + data_bag[self.in2].value
def backward(self, data_bag):
data_bag[self.in1].gradient = data_bag[self.out].gradient
data_bag[self.in2].gradient = data_bag[self.out].gradient
class MultiplyNode(Node):
def __init__(self, in1: Connection, in2: Connection, out: Connection):
super().__init__("multiply", [in1, in2], [out])
self.in1 = in1
self.in2 = in2
self.out = out
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = data_bag[self.in1].value * data_bag[self.in2].value
def backward(self, data_bag):
data_bag[self.in1].gradient = data_bag[self.in2].value * data_bag[self.out].gradient
data_bag[self.in2].gradient = data_bag[self.in1].value * data_bag[self.out].gradient
class DivNode(Node):
def __init__(self, in1: Connection, in2: Connection, out: Connection):
super().__init__("div", [in1, in2], [out])
self.in1 = in1
self.in2 = in2
self.out = out
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = data_bag[self.in1].value / data_bag[self.in2].value
def backward(self, data_bag):
data_bag[self.in1].gradient = (1 / data_bag[self.in2].value) * data_bag[self.out].gradient
data_bag[self.in2].gradient = data_bag[self.in1].value * (-1 / data_bag[self.in2].value ** 2) * data_bag[
self.out].gradient
class ExpNode(Node):
def __init__(self, in1: Connection, out: Connection):
super().__init__("exp", [in1], [out])
self.in1 = in1
self.out = out
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = np.exp(data_bag[self.in1].value)
def backward(self, data_bag):
data_bag[self.in1].gradient = np.exp(data_bag[self.in1].value) * data_bag[self.out].gradient
class SqrtNode(Node):
def __init__(self, in1: Connection, out: Connection):
super().__init__("sqrt", [in1], [out])
self.in1 = in1
self.out = out
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = np.sqrt(data_bag[self.in1].value)
def backward(self, data_bag):
data_bag[self.in1].gradient = (.5/np.sqrt(data_bag[self.in1].value)) * data_bag[self.out].gradient
class LogNode(Node):
def __init__(self, in1: Connection, out: Connection):
super().__init__("log", [in1], [out])
self.in1 = in1
self.out = out
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = np.log(data_bag[self.in1].value)
def backward(self, data_bag):
data_bag[self.in1].gradient = (1 / data_bag[self.in1].value) * data_bag[self.out].gradient
class ExpressionNode(Node):
def __init__(self, in1: Connection, out: Connection, expression):
super().__init__("eval", [in1], [out])
self.in1 = in1
self.out = out
self.expression = expression
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = self.expression(data_bag[self.in1].value)
def backward(self, data_bag):
pass
class ReduceSumNode(Node):
def __init__(self, in1: Connection, out: Connection, axis=None):
super().__init__("reduce_sum", [in1], [out])
self.in1 = in1
self.out = out
self.axis = axis
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = np.sum(data_bag[self.in1].value, self.axis)
def backward(self, data_bag):
data_bag[self.in1].gradient = np.ones(shape=data_bag[self.in1].value.shape) * data_bag[self.out].gradient
def __str__(self):
return super().__str__() + "(axis={})".format(self.axis)
class BroadcastNode(Node):
def __init__(self, in1: Connection, out: Connection, axis=1):
super().__init__("broadcast", [in1], [out])
self.in1 = in1
self.out = out
self.axis = axis
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = np.expand_dims(data_bag[self.in1].value, self.axis)
def backward(self, data_bag):
data_bag[self.in1].gradient = np.sum(data_bag[self.out].gradient, axis=self.axis)
def __str__(self):
return super().__str__() + "(axis={})".format(self.axis)
class MatrixMultiplyNode(Node):
def __init__(self, in1: Connection, in2: Connection, out: Connection):
super().__init__("mat_mul", [in1, in2], [out])
self.in1 = in1
self.in2 = in2
self.out = out
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = data_bag[self.in1].value.dot(data_bag[self.in2].value)
def backward(self, data_bag):
data_bag[self.in1].gradient = data_bag[self.out].gradient.dot(data_bag[self.in2].value.T)
data_bag[self.in2].gradient = data_bag[self.in1].value.T.dot(data_bag[self.out].gradient)
class MaxNode(Node):
def __init__(self, in1: Connection, in2: Connection, out: Connection):
super().__init__("max", [in1, in2], [out])
self.in1 = in1
self.in2 = in2
self.out = out
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = np.maximum(data_bag[self.in1].value, data_bag[self.in2].value)
def backward(self, data_bag):
data_bag[self.in1].gradient = np.array(data_bag[self.in1].value > data_bag[self.in2].value, dtype=np.float32) * \
data_bag[self.out].gradient
data_bag[self.in2].gradient = np.array(data_bag[self.in2].value > data_bag[self.in1].value, dtype=np.float32) * \
data_bag[self.out].gradient
class TransposeNode(Node):
def __init__(self, in1: Connection, out: Connection, axes):
super().__init__("transpose", [in1], [out])
self.in1 = in1
self.out = out
self.axes = axes
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = np.transpose(data_bag[self.in1].value, self.axes)
def backward(self, data_bag):
data_bag[self.in1].gradient = np.transpose(data_bag[self.out].gradient, self.axes)
def __str__(self):
return super().__str__() + "(axes={})".format(self.axes)
class ReshapeNode(Node):
def __init__(self, in1: Connection, out: Connection, newshape):
super().__init__("reshape", [in1], [out])
self.in1 = in1
self.out = out
self.newshape = newshape
def forward(self, data_bag):
super().forward(data_bag)
data_bag[self.out].value = np.reshape(data_bag[self.in1].value, self.newshape)
def backward(self, data_bag):
data_bag[self.in1].gradient = np.reshape(data_bag[self.out].gradient, data_bag[self.in1].value.shape)
def __str__(self):
return super().__str__() + "(shape={})".format(self.newshape)
class Tensor3dToCol(Node):
def __init__(self, in1: Connection, out: Connection, receptive_field_size, stride=1, padding=1):
super().__init__("tensor_3d_to_col", [in1], [out])
self.in1 = in1
self.out = out
self.receptive_field_size = receptive_field_size
self.stride = stride
self.padding = padding
@staticmethod
def get_indices(f, c, s, output_height, output_width):
io = np.repeat(s * np.arange(output_height, dtype=np.int32), output_height * f * f * c)
ko = np.tile(np.repeat(s * np.arange(output_width, dtype=np.int32), f * f * c), output_width)
i = np.tile(np.tile(np.repeat(np.arange(f, dtype=np.int32), f), c), output_height * output_width)
k = np.tile(np.tile(np.tile(np.arange(f, dtype=np.int32), f), output_height * output_width), c)
j = np.tile(np.repeat(np.arange(c, dtype=np.int32), f * f), output_height * output_width)
return slice(None), j, i + io, k + ko
@staticmethod
def get_output_dims(w, h, f, p, s):
assert (w - f + 2 * p) % s == 0
output_width = (w - f + 2 * p) / s + 1
output_height = (h - f + 2 * p) / s + 1
return output_width, output_height
@staticmethod
def pad(x, p):
return np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant')
def forward(self, data_bag):
super().forward(data_bag)
x = data_bag[self.in1].value
# input width
w = x.shape[2]
# input height
h = x.shape[3]
# number of samples
n = x.shape[0]
# depth
c = x.shape[1]
f = self.receptive_field_size
p = self.padding
s = self.stride
x_padded = self.pad(x, p)
output_width, output_height = self.get_output_dims(w, h, f, p, s)
idx = self.get_indices(f, c, s, output_height, output_width)
x_col = x_padded[idx].reshape(n, output_height * output_width, -1)
data_bag[self.out].value = x_col
def backward(self, data_bag):
x = data_bag[self.in1].value
# input width
w = x.shape[2]
# input height
h = x.shape[3]
# number of samples
n = x.shape[0]
# depth
c = x.shape[1]
f = self.receptive_field_size
p = self.padding
s = self.stride
grad_in = data_bag[self.out].gradient
output_width, output_height = self.get_output_dims(w, h, f, p, s)
idx = self.get_indices(f, c, s, output_height, output_width)
grad = self.pad(np.zeros(shape=x.shape), p)
np.add.at(grad, idx, grad_in.reshape(n, -1))
if p != 0:
grad = grad[:, :, p:-p, p:-p]
data_bag[self.in1].gradient = grad