This repository has been archived by the owner on Nov 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathmath_ops.py
208 lines (163 loc) · 5.45 KB
/
math_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
import os
import pdb
# MXNET_CPU_WORKER_NTHREADS must be greater than 1 for custom op to work on CPU
os.environ["MXNET_CPU_WORKER_NTHREADS"] = "2"
import mxnet as mx
import numpy as np
#=========== math reduce_mean ============#
class ChannelReduceMean(mx.operator.CustomOp):
"""
can only process array up to 4 dimontions
"""
def forward(self, is_train, req, in_data, out_data, aux):
x = in_data[0].asnumpy()
#pdb.set_trace()
y = x
n = x.ndim
#create mean for each channel
if n > 2:
y = np.mean(y, axis=2, keepdims=True)
if n == 4:
y = np.mean(y, axis=3, keepdims=True)
#do this if we want to get a global scalar mean across all channels
#if n > 1:
# y = np.mean(y, axis=1, keepdims=True)
y_nd = mx.nd.array(y)
y_o = y_nd.broadcast_to(x.shape)
#pdb.set_trace()
self.assign(out_data[0], req[0], y_o)
def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
self.assign(in_grad[0], req[0], out_grad[0])
@mx.operator.register("pro_channel_reduce_mean")
class ChannelReduceMeanProp(mx.operator.CustomOpProp):
def __init__(self):
super(ChannelReduceMeanProp, self).__init__(need_top_grad=True)
def list_arguments(self):
return ['data']
def list_outputs(self):
return ['output']
def infer_shape(self, in_shape):
data_shape = in_shape[0]
output_shape = in_shape[0]
return [data_shape], [output_shape], []
def create_operator(self, ctx, shapes, dtypes):
return ChannelReduceMean()
#========= end math mean ==========#
#=========== math tanh ============#
class Tanh(mx.operator.CustomOp):
def forward(self, is_train, req, in_data, out_data, aux):
x = in_data[0].asnumpy()
y = np.tanh(x)
#pdb.set_trace()
#print(y.asnumpy())
self.assign(out_data[0], req[0], mx.nd.array(y))
def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
self.assign(in_grad[0], req[0], out_grad[0])
@mx.operator.register("tanh")
class TanhProp(mx.operator.CustomOpProp):
def __init__(self):
super(TanhProp, self).__init__(need_top_grad=True)
def list_arguments(self):
return ['data']
def list_outputs(self):
return ['output']
def infer_shape(self, in_shape):
data_shape = in_shape[0]
output_shape = in_shape[0]
return [data_shape], [output_shape], []
def create_operator(self, ctx, shapes, dtypes):
return Tanh()
#=========== end tanh ============#
#=========== math max ============#
class Amax(mx.operator.CustomOp):
def forward(self, is_train, req, in_data, out_data, aux):
x = in_data[0].asnumpy()
y = np.amax(x)
self.assign(out_data[0], req[0], y)
def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
self.assign(in_grad[0], req[0], out_grad[0])
@mx.operator.register("amax")
class AmaxProp(mx.operator.CustomOpProp):
def __init__(self):
super(AmaxProp, self).__init__(need_top_grad=True)
def list_arguments(self):
return ['data']
def list_outputs(self):
return ['output']
def infer_shape(self, in_shape):
data_shape = in_shape[0]
output_shape = in_shape[0]
return [data_shape], [output_shape], []
def create_operator(self, ctx, shapes, dtypes):
return Amax()
#=========== end tanh ============#
#=========== math around ============#
class Around(mx.operator.CustomOp):
def forward(self, is_train, req, in_data, out_data, aux):
x = in_data[0]
y = mx.nd.round(x)
#pdb.set_trace()
#print(y.asnumpy())
self.assign(out_data[0], req[0], y)
def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
self.assign(in_grad[0], req[0], out_grad[0])
@mx.operator.register("around")
class AroundProp(mx.operator.CustomOpProp):
def __init__(self):
super(AroundProp, self).__init__(need_top_grad=True)
def list_arguments(self):
return ['data']
def list_outputs(self):
return ['output']
def infer_shape(self, in_shape):
data_shape = in_shape[0]
output_shape = in_shape[0]
return [data_shape], [output_shape], []
def create_operator(self, ctx, shapes, dtypes):
return Around()
#========= end around ==========#
#=========== math clip ============#
class Clip(mx.operator.CustomOp):
def forward(self, is_train, req, in_data, out_data, aux):
x = in_data[0]
y = mx.nd.clip(x, 0, 1)
#pdb.set_trace()
self.assign(out_data[0], req[0], y)
def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
self.assign(in_grad[0], req[0], out_grad[0])
@mx.operator.register("clip_by_0_1")
class ClipProp(mx.operator.CustomOpProp):
def __init__(self):
super(ClipProp, self).__init__(need_top_grad=True)
def list_arguments(self):
return ['data']
def list_outputs(self):
return ['output']
def infer_shape(self, in_shape):
data_shape = in_shape[0]
output_shape = in_shape[0]
return [data_shape], [output_shape], []
def create_operator(self, ctx, shapes, dtypes):
return Clip()
#========= end math clip ==========#
#=========== debug data ============#
class debug(mx.operator.CustomOp):
def forward(self, is_train, req, in_data, out_data, aux):
x = in_data[0].asnumpy()
#pdb.set_trace()
print(x)
#print(y.asnumpy())
self.assign(out_data[0], req[0], in_data[0])
def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
self.assign(in_grad[0], req[0], out_grad[0])
@mx.operator.register("debug")
class debugProp(mx.operator.CustomOpProp):
def __init__(self):
super(debugProp, self).__init__(need_top_grad=True)
def list_arguments(self):
return ['data']
def list_outputs(self):
return ['output']
def create_operator(self, ctx, shapes, dtypes):
return debug()
#=========== end debug data ============#