-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
core.py
348 lines (260 loc) · 8.62 KB
/
core.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
import weakref
import numpy as np
import contextlib
import dezero
# =============================================================================
# Config
# =============================================================================
class Config:
enable_backprop = True
train = True
@contextlib.contextmanager
def using_config(name, value):
old_value = getattr(Config, name)
setattr(Config, name, value)
try:
yield
finally:
setattr(Config, name, old_value)
def no_grad():
return using_config('enable_backprop', False)
def test_mode():
return using_config('train', False)
# =============================================================================
# Variable / Function
# =============================================================================
try:
import cupy
array_types = (np.ndarray, cupy.ndarray)
except ImportError:
array_types = (np.ndarray)
class Variable:
__array_priority__ = 200
def __init__(self, data, name=None):
if data is not None:
if not isinstance(data, array_types):
raise TypeError('{} is not supported'.format(type(data)))
self.data = data
self.name = name
self.grad = None
self.creator = None
self.generation = 0
@property
def shape(self):
return self.data.shape
@property
def ndim(self):
return self.data.ndim
@property
def size(self):
return self.data.size
@property
def dtype(self):
return self.data.dtype
def __len__(self):
return len(self.data)
def __repr__(self):
if self.data is None:
return 'variable(None)'
p = str(self.data).replace('\n', '\n' + ' ' * 9)
return 'variable(' + p + ')'
def set_creator(self, func):
self.creator = func
self.generation = func.generation + 1
def unchain(self):
self.creator = None
def cleargrad(self):
self.grad = None
def backward(self, retain_grad=False, create_graph=False):
if self.grad is None:
xp = dezero.cuda.get_array_module(self.data)
self.grad = Variable(xp.ones_like(self.data))
funcs = []
seen_set = set()
def add_func(f):
if f not in seen_set:
funcs.append(f)
seen_set.add(f)
funcs.sort(key=lambda x: x.generation)
add_func(self.creator)
while funcs:
f = funcs.pop()
gys = [output().grad for output in f.outputs] # output is weakref
with using_config('enable_backprop', create_graph):
gxs = f.backward(*gys)
if not isinstance(gxs, tuple):
gxs = (gxs,)
for x, gx in zip(f.inputs, gxs):
if x.grad is None:
x.grad = gx
else:
x.grad = x.grad + gx
if x.creator is not None:
add_func(x.creator)
if not retain_grad:
for y in f.outputs:
y().grad = None # y is weakref
def unchain_backward(self):
if self.creator is not None:
funcs = [self.creator]
while funcs:
f = funcs.pop()
for x in f.inputs:
if x.creator is not None:
funcs.append(x.creator)
x.unchain()
def reshape(self, *shape):
if len(shape) == 1 and isinstance(shape[0], (tuple, list)):
shape = shape[0]
return dezero.functions.reshape(self, shape)
def transpose(self, *axes):
if len(axes) == 0:
axes = None
elif len(axes) == 1:
if isinstance(axes[0], (tuple, list)) or axes[0] is None:
axes = axes[0]
return dezero.functions.transpose(self, axes)
@property
def T(self):
return dezero.functions.transpose(self)
def sum(self, axis=None, keepdims=False):
return dezero.functions.sum(self, axis, keepdims)
def to_cpu(self):
if self.data is not None:
self.data = dezero.cuda.as_numpy(self.data)
def to_gpu(self):
if self.data is not None:
self.data = dezero.cuda.as_cupy(self.data)
class Parameter(Variable):
pass
def as_variable(obj):
if isinstance(obj, Variable):
return obj
return Variable(obj)
def as_array(x, array_module=np):
if np.isscalar(x):
return array_module.array(x)
return x
class Function:
def __call__(self, *inputs):
inputs = [as_variable(x) for x in inputs]
xs = [x.data for x in inputs]
ys = self.forward(*xs)
if not isinstance(ys, tuple):
ys = (ys,)
outputs = [Variable(as_array(y)) for y in ys]
if Config.enable_backprop:
self.generation = max([x.generation for x in inputs])
for output in outputs:
output.set_creator(self)
self.inputs = inputs
self.outputs = [weakref.ref(output) for output in outputs]
return outputs if len(outputs) > 1 else outputs[0]
def forward(self, xs):
raise NotImplementedError()
def backward(self, gys):
raise NotImplementedError()
# =============================================================================
# 四則演算 / 演算子のオーバーロード
# =============================================================================
class Add(Function):
def forward(self, x0, x1):
self.x0_shape, self.x1_shape = x0.shape, x1.shape
y = x0 + x1
return y
def backward(self, gy):
gx0, gx1 = gy, gy
if self.x0_shape != self.x1_shape: # for broadcaset
gx0 = dezero.functions.sum_to(gx0, self.x0_shape)
gx1 = dezero.functions.sum_to(gx1, self.x1_shape)
return gx0, gx1
def add(x0, x1):
x1 = as_array(x1, dezero.cuda.get_array_module(x0.data))
return Add()(x0, x1)
class Mul(Function):
def forward(self, x0, x1):
y = x0 * x1
return y
def backward(self, gy):
x0, x1 = self.inputs
gx0 = gy * x1
gx1 = gy * x0
if x0.shape != x1.shape: # for broadcast
gx0 = dezero.functions.sum_to(gx0, x0.shape)
gx1 = dezero.functions.sum_to(gx1, x1.shape)
return gx0, gx1
def mul(x0, x1):
x1 = as_array(x1, dezero.cuda.get_array_module(x0.data))
return Mul()(x0, x1)
class Neg(Function):
def forward(self, x):
return -x
def backward(self, gy):
return -gy
def neg(x):
return Neg()(x)
class Sub(Function):
def forward(self, x0, x1):
self.x0_shape, self.x1_shape = x0.shape, x1.shape
y = x0 - x1
return y
def backward(self, gy):
gx0 = gy
gx1 = -gy
if self.x0_shape != self.x1_shape: # for broadcast
gx0 = dezero.functions.sum_to(gx0, self.x0_shape)
gx1 = dezero.functions.sum_to(gx1, self.x1_shape)
return gx0, gx1
def sub(x0, x1):
x1 = as_array(x1, dezero.cuda.get_array_module(x0.data))
return Sub()(x0, x1)
def rsub(x0, x1):
x1 = as_array(x1, dezero.cuda.get_array_module(x0.data))
return Sub()(x1, x0)
class Div(Function):
def forward(self, x0, x1):
y = x0 / x1
return y
def backward(self, gy):
x0, x1 = self.inputs
gx0 = gy / x1
gx1 = gy * (-x0 / x1 ** 2)
if x0.shape != x1.shape: # for broadcast
gx0 = dezero.functions.sum_to(gx0, x0.shape)
gx1 = dezero.functions.sum_to(gx1, x1.shape)
return gx0, gx1
def div(x0, x1):
x1 = as_array(x1, dezero.cuda.get_array_module(x0.data))
return Div()(x0, x1)
def rdiv(x0, x1):
x1 = as_array(x1, dezero.cuda.get_array_module(x0.data))
return Div()(x1, x0)
class Pow(Function):
def __init__(self, c):
self.c = c
def forward(self, x):
y = x ** self.c
return y
def backward(self, gy):
x, = self.inputs
c = self.c
gx = c * x ** (c - 1) * gy
return gx
def pow(x, c):
return Pow(c)(x)
def setup_variable():
Variable.__add__ = add
Variable.__radd__ = add
Variable.__mul__ = mul
Variable.__rmul__ = mul
Variable.__neg__ = neg
Variable.__sub__ = sub
Variable.__rsub__ = rsub
Variable.__truediv__ = div
Variable.__rtruediv__ = rdiv
Variable.__pow__ = pow
Variable.__getitem__ = dezero.functions.get_item
Variable.matmul = dezero.functions.matmul
Variable.dot = dezero.functions.matmul
Variable.max = dezero.functions.max
Variable.min = dezero.functions.min