-
Notifications
You must be signed in to change notification settings - Fork 87
/
ops.py
381 lines (275 loc) · 10.7 KB
/
ops.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 5 15:23:01 2019
@author: zhangjuefei
"""
import numpy as np
from ..core import Node
def fill_diagonal(to_be_filled, filler):
"""
将 filler 矩阵填充在 to_be_filled 的对角线上
"""
assert to_be_filled.shape[0] / \
filler.shape[0] == to_be_filled.shape[1] / filler.shape[1]
n = int(to_be_filled.shape[0] / filler.shape[0])
r, c = filler.shape
for i in range(n):
to_be_filled[i * r:(i + 1) * r, i * c:(i + 1) * c] = filler
return to_be_filled
class Operator(Node):
'''
定义操作符抽象类
'''
pass
class Add(Operator):
"""
(多个)矩阵加法
"""
def compute(self):
# assert len(self.parents) == 2 and self.parents[0].shape() == self.parents[1].shape()
self.value = np.mat(np.zeros(self.parents[0].shape()))
for parent in self.parents:
self.value += parent.value
def get_jacobi(self, parent):
return np.mat(np.eye(self.dimension())) # 矩阵之和对其中任一个矩阵的雅可比矩阵是单位矩阵
class MatMul(Operator):
"""
矩阵乘法
"""
def compute(self):
assert len(self.parents) == 2 and self.parents[0].shape()[
1] == self.parents[1].shape()[0]
self.value = self.parents[0].value * self.parents[1].value
def get_jacobi(self, parent):
"""
将矩阵乘法视作映射,求映射对参与计算的矩阵的雅克比矩阵。
"""
# 很神秘,靠注释说不明白了
zeros = np.mat(np.zeros((self.dimension(), parent.dimension())))
if parent is self.parents[0]:
return fill_diagonal(zeros, self.parents[1].value.T)
else:
jacobi = fill_diagonal(zeros, self.parents[0].value)
row_sort = np.arange(self.dimension()).reshape(
self.shape()[::-1]).T.ravel()
col_sort = np.arange(parent.dimension()).reshape(
parent.shape()[::-1]).T.ravel()
return jacobi[row_sort, :][:, col_sort]
class Logistic(Operator):
"""
对向量的分量施加Logistic函数
"""
def compute(self):
x = self.parents[0].value
# 对父节点的每个分量施加Logistic
self.value = np.mat(
1.0 / (1.0 + np.power(np.e, np.where(-x > 1e2, 1e2, -x))))
def get_jacobi(self, parent):
return np.diag(np.mat(np.multiply(self.value, 1 - self.value)).A1)
class ReLU(Operator):
"""
对矩阵的元素施加ReLU函数
"""
nslope = 0.1 # 负半轴的斜率
def compute(self):
self.value = np.mat(np.where(
self.parents[0].value > 0.0,
self.parents[0].value,
self.nslope * self.parents[0].value)
)
def get_jacobi(self, parent):
return np.diag(np.where(self.parents[0].value.A1 > 0.0, 1.0, self.nslope))
class SoftMax(Operator):
"""
SoftMax函数
"""
@staticmethod
def softmax(a):
a[a > 1e2] = 1e2 # 防止指数过大
ep = np.power(np.e, a)
return ep / np.sum(ep)
def compute(self):
self.value = SoftMax.softmax(self.parents[0].value)
def get_jacobi(self, parent):
"""
我们不实现SoftMax节点的get_jacobi函数,
训练时使用CrossEntropyWithSoftMax节点
"""
raise NotImplementedError("Don't use SoftMax's get_jacobi")
class Reshape(Operator):
"""
改变父节点的值(矩阵)的形状
"""
def __init__(self, *parent, **kargs):
Operator.__init__(self, *parent, **kargs)
self.to_shape = kargs.get('shape')
assert isinstance(self.to_shape, tuple) and len(self.to_shape) == 2
def compute(self):
self.value = self.parents[0].value.reshape(self.to_shape)
def get_jacobi(self, parent):
assert parent is self.parents[0]
return np.mat(np.eye(self.dimension()))
class Multiply(Operator):
"""
两个父节点的值是相同形状的矩阵,将它们对应位置的值相乘
"""
def compute(self):
self.value = np.multiply(self.parents[0].value, self.parents[1].value)
def get_jacobi(self, parent):
if parent is self.parents[0]:
return np.diag(self.parents[1].value.A1)
else:
return np.diag(self.parents[0].value.A1)
class Convolve(Operator):
"""
以第二个父节点的值为滤波器,对第一个父节点的值做二维离散卷积
"""
def __init__(self, *parents, **kargs):
assert len(parents) == 2
Operator.__init__(self, *parents, **kargs)
self.padded = None
def compute(self):
data = self.parents[0].value # 图像
kernel = self.parents[1].value # 滤波器
w, h = data.shape # 图像的宽和高
kw, kh = kernel.shape # 滤波器尺寸
hkw, hkh = int(kw / 2), int(kh / 2) # 滤波器长宽的一半
# 补齐数据边缘
pw, ph = tuple(np.add(data.shape, np.multiply((hkw, hkh), 2)))
self.padded = np.mat(np.zeros((pw, ph)))
self.padded[hkw:hkw + w, hkh:hkh + h] = data
self.value = np.mat(np.zeros((w, h)))
# 二维离散卷积
for i in np.arange(hkw, hkw + w):
for j in np.arange(hkh, hkh + h):
self.value[i - hkw, j - hkh] = np.sum(
np.multiply(self.padded[i - hkw:i - hkw + kw, j - hkh:j - hkh + kh], kernel))
def get_jacobi(self, parent):
data = self.parents[0].value # 图像
kernel = self.parents[1].value # 滤波器
w, h = data.shape # 图像的宽和高
kw, kh = kernel.shape # 滤波器尺寸
hkw, hkh = int(kw / 2), int(kh / 2) # 滤波器长宽的一半
# 补齐数据边缘
pw, ph = tuple(np.add(data.shape, np.multiply((hkw, hkh), 2)))
jacobi = []
if parent is self.parents[0]:
for i in np.arange(hkw, hkw + w):
for j in np.arange(hkh, hkh + h):
mask = np.mat(np.zeros((pw, ph)))
mask[i - hkw:i - hkw + kw, j - hkh:j - hkh + kh] = kernel
jacobi.append(mask[hkw:hkw + w, hkh:hkh + h].A1)
elif parent is self.parents[1]:
for i in np.arange(hkw, hkw + w):
for j in np.arange(hkh, hkh + h):
jacobi.append(
self.padded[i - hkw:i - hkw + kw, j - hkh:j - hkh + kh].A1)
else:
raise Exception("You're not my father")
return np.mat(jacobi)
class MaxPooling(Operator):
"""
最大值池化
"""
def __init__(self, *parent, **kargs):
Operator.__init__(self, *parent, **kargs)
self.stride = kargs.get('stride')
assert self.stride is not None
self.stride = tuple(self.stride)
assert isinstance(self.stride, tuple) and len(self.stride) == 2
self.size = kargs.get('size')
assert self.size is not None
self.size = tuple(self.size)
assert isinstance(self.size, tuple) and len(self.size) == 2
self.flag = None
def compute(self):
data = self.parents[0].value # 输入特征图
w, h = data.shape # 输入特征图的宽和高
dim = w * h
sw, sh = self.stride
kw, kh = self.size # 池化核尺寸
hkw, hkh = int(kw / 2), int(kh / 2) # 池化核长宽的一半
result = []
flag = []
for i in np.arange(0, w, sw):
row = []
for j in np.arange(0, h, sh):
# 取池化窗口中的最大值
top, bottom = max(0, i - hkw), min(w, i + hkw + 1)
left, right = max(0, j - hkh), min(h, j + hkh + 1)
window = data[top:bottom, left:right]
row.append(
np.max(window)
)
# 记录最大值在原特征图中的位置
pos = np.argmax(window)
w_width = right - left
offset_w, offset_h = top + pos // w_width, left + pos % w_width
offset = offset_w * w + offset_h
tmp = np.zeros(dim)
tmp[offset] = 1
flag.append(tmp)
result.append(row)
self.flag = np.mat(flag)
self.value = np.mat(result)
def get_jacobi(self, parent):
assert parent is self.parents[0] and self.jacobi is not None
return self.flag
class Concat(Operator):
"""
将多个父节点的值连接成向量
"""
def compute(self):
assert len(self.parents) > 0
# 将所有父节点矩阵展平并连接成一个向量
self.value = np.concatenate(
[p.value.flatten() for p in self.parents],
axis=1
).T
def get_jacobi(self, parent):
assert parent in self.parents
dimensions = [p.dimension() for p in self.parents] # 各个父节点的元素数量
pos = self.parents.index(parent) # 当前是第几个父节点
dimension = parent.dimension() # 当前父节点的元素数量
assert dimension == dimensions[pos]
jacobi = np.mat(np.zeros((self.dimension(), dimension)))
start_row = int(np.sum(dimensions[:pos]))
jacobi[start_row:start_row + dimension,
0:dimension] = np.eye(dimension)
return jacobi
class ScalarMultiply(Operator):
"""
用标量(1x1矩阵)数乘一个矩阵
"""
def compute(self):
assert self.parents[0].shape() == (1, 1) # 第一个父节点是标量
self.value = np.multiply(self.parents[0].value, self.parents[1].value)
def get_jacobi(self, parent):
assert parent in self.parents
if parent is self.parents[0]:
return self.parents[1].value.flatten().T
else:
return np.mat(np.eye(self.parents[1].dimension())) * self.parents[0].value[0, 0]
class Step(Operator):
def compute(self):
self.value = np.mat(np.where(self.parents[0].value >= 0.0, 1.0, 0.0))
def get_jacobi(self, parent):
return np.mat(np.zeros(self.dimension()))
class Welding(Operator):
def compute(self):
assert len(self.parents) == 1 and self.parents[0] is not None
self.value = self.parents[0].value
def get_jacobi(self, parent):
assert parent is self.parents[0]
return np.mat(np.eye(self.dimension()))
def weld(self, node):
"""
将本节点焊接到输入节点上
"""
# 首先与之前的父节点断开
if len(self.parents) == 1 and self.parents[0] is not None:
self.parents[0].children.remove(self)
self.parents.clear()
# 与输入节点焊接
self.parents.append(node)
node.children.append(self)