-
Notifications
You must be signed in to change notification settings - Fork 0
/
chained_operations.py
307 lines (230 loc) · 8.92 KB
/
chained_operations.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
import numpy as np
def run(return_values, feed_dict=None, backwards=True):
feed_dict = feed_dict or {}
for operation, value in feed_dict.items():
operation.run(value)
if isinstance(return_values, (list, tuple)):
ret = []
for operation in return_values:
if isinstance(operation, ChainedOperation):
output = operation.get_output()
else:
output = operation
ret.append(output)
if backwards:
operation.backwards()
return tuple(ret)
elif isinstance(return_values, dict):
ret = {}
for key, operation in return_values.items():
if isinstance(operation, ChainedOperation):
output = operation.get_output()
else:
output = operation
ret[key] = output
if backwards:
operation.backwards()
return ret
operation = return_values
if isinstance(operation, ChainedOperation):
output = operation.get_output()
else:
output = operation
if backwards:
operation.backwards()
return output
class ChainedOperation(object):
def __init__(self, inputs=None):
self.input_objects = inputs or []
self.output_objects = []
self.inputs_ready = {}
self.outputs_ready = {}
self.inputs = []
self.output = None
self.grad = 0
for i in self.input_objects:
if isinstance(i, ChainedOperation):
if not isinstance(i, Variable):
self.inputs_ready[i] = False
i.add_output(self)
def add_output(self, output_object):
self.output_objects.append(output_object)
self.outputs_ready[output_object] = False
def forwards(self, input_object=None):
self_forward = input_object is None
if not self_forward:
self.inputs_ready[input_object] = True
inputs_ready = self.all_inputs_ready()
if inputs_ready and not self_forward:
self.inputs.clear()
for i in self.input_objects:
if isinstance(i, ChainedOperation):
self.inputs.append(i.get_output())
else:
self.inputs.append(i)
self.output = self.calc_forwards(self.inputs)
if inputs_ready or self_forward:
for o in self.output_objects:
o.forwards(self)
# Clear variables
self.grad = 0
for key in self.outputs_ready.keys():
self.outputs_ready[key] = False
for key in self.inputs_ready.keys():
self.inputs_ready[key] = False
def backwards(self, output_object=None):
if len(self.inputs) != len(self.input_objects):
raise ValueError('cannot preform backwards pass before full forwards pass,'
'have you missed placeholder values in the feed_dict?')
if output_object is None:
if not np.any(self.grad):
self.grad = np.ones_like(self.output)
else:
self.outputs_ready[output_object] = True
self.grad += output_object.get_grad(self)
if self.all_outputs_ready():
for input_object in self.input_objects:
if isinstance(input_object, ChainedOperation):
input_object.backwards(self)
def get_output(self):
return self.output
def get_grad(self, input_object=None):
if input_object is None:
return self.grad
return np.multiply(self.grad, self.calc_backwards(input_object))
def all_inputs_ready(self):
return self.all_ready(self.inputs_ready)
def all_outputs_ready(self):
return self.all_ready(self.outputs_ready)
@staticmethod
def all_ready(l):
for state in l.values():
if state is False:
return False
return True
def calc_forwards(self, inputs):
raise NotImplementedError('Abstract class')
def calc_backwards(self, input_object):
raise NotImplementedError('Abstract class')
class UnaryChainedOperation(ChainedOperation):
def __init__(self, x):
super(UnaryChainedOperation, self).__init__([x])
def calc_forwards(self, inputs):
return self.calc_forwards_single(inputs[0])
def calc_backwards(self, input_object):
return self.calc_backwards_single(self.inputs[0])
def calc_forwards_single(self, x):
raise NotImplementedError('Abstract class')
def calc_backwards_single(self, x):
raise NotImplementedError('Abstract class')
class BinaryChainedOperation(ChainedOperation):
def __init__(self, a, b):
super(BinaryChainedOperation, self).__init__([a, b])
def calc_forwards(self, inputs):
return self.calc_forwards_binary(inputs[0], inputs[1])
def calc_backwards(self, input_object):
return self.calc_backwards_binary(input_object, self.inputs[0], self.inputs[1])
def calc_forwards_binary(self, a, b):
raise NotImplementedError('Abstract class')
def calc_backwards_binary(self, input_object, a, b):
raise NotImplementedError('Abstract class')
class Placeholder(ChainedOperation):
def __init__(self):
super(Placeholder, self).__init__()
def calc_forwards(self, _):
return 1
def calc_backwards(self, _):
return 1
def run(self, value):
self.output = np.asmatrix(value)
self.forwards()
def random_weight(*args):
return (np.random.rand(*args) - 0.5) * 2
class Variable(Placeholder):
def __init__(self, shape, random_func=random_weight):
super(Variable, self).__init__()
self.shape = shape
self.random_func = random_func
self.value = None
self.initialize()
def get_output(self):
return self.value
def update(self, delta, direction=1):
self.value += direction * delta
def initialize(self):
self.value = self.random_func(*self.shape)
def reset_grad(self):
self.grad = 0
class Log(UnaryChainedOperation):
def calc_forwards_single(self, x):
return np.log(x)
def calc_backwards_single(self, x):
return 1 / x
class Sum(UnaryChainedOperation):
def __init__(self, x, axis=None):
super(Sum, self).__init__(x)
self.axis = axis
def calc_forwards_single(self, x):
return np.sum(x, self.axis)
def get_grad(self, input_object):
if self.axis is not None:
reps = np.ones_like(np.shape(self.inputs[0]))
reps[self.axis] = np.shape(self.inputs[0])[self.axis]
return np.tile(self.grad, reps)
return super(Sum, self).get_grad(input_object)
def calc_backwards_single(self, x):
return np.ones_like(x)
class Reciprocal(UnaryChainedOperation):
def calc_forwards_single(self, x):
return 1 / x
def calc_backwards_single(self, x):
return - 1 / np.square(x)
class Exp(UnaryChainedOperation):
def __init__(self, x, axis=None):
super(Exp, self).__init__(x)
self.axis = axis
def calc_forwards_single(self, x):
if self.axis is not None:
return np.exp(x - np.max(x, axis=self.axis))
return np.exp(x)
def calc_backwards_single(self, x):
if self.axis is not None:
return np.exp(x - np.max(x, axis=self.axis))
return np.exp(x)
class Add(BinaryChainedOperation):
def __init__(self, a, b, axis=0):
super(Add, self).__init__(a, b)
self.axis = axis
def calc_forwards_binary(self, a, b):
return np.add(a, b)
def get_grad(self, input_object=None):
if input_object is None:
return self.grad
grad = np.multiply(self.grad, self.calc_backwards(input_object))
if np.shape(grad) != np.shape(self.inputs[self.input_objects.index(input_object)]):
grad = np.sum(grad, self.axis)
return grad
def calc_backwards_binary(self, input_object, a, b):
if self.input_objects.index(input_object) == 0:
return np.ones_like(a)
return np.ones_like(b)
class Mul(BinaryChainedOperation):
def calc_forwards_binary(self, a, b):
return np.multiply(a, b)
def calc_backwards_binary(self, input_object, a, b):
if self.input_objects.index(input_object) == 0:
return b
return a
class Dot(BinaryChainedOperation):
def get_grad(self, input_object=None):
if input_object is None:
return self.grad
if self.input_objects.index(input_object) == 0:
return np.dot(self.grad, self.calc_backwards(input_object))
return np.dot(self.calc_backwards(input_object), self.grad)
def calc_forwards_binary(self, a, b):
return np.dot(a, b)
def calc_backwards_binary(self, input_object, a, b):
if self.input_objects.index(input_object) == 0:
return np.transpose(b)
return np.transpose(a)