forked from nouiz/ccw_tutorial_theano
-
Notifications
You must be signed in to change notification settings - Fork 14
/
scalmulop.py
57 lines (43 loc) · 1.53 KB
/
scalmulop.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
from theano import Op, Apply
from theano.tensor import as_tensor_variable
from theano.scalar import as_scalar
class ScalMulV1(Op):
__props__ = ('scal',)
def __init__(self, scal):
if not isinstance(scal, int):
raise TypeError('expected an int')
self.scal = scal
def make_node(self, x):
x = as_tensor_variable(x)
return Apply(self, [x], [x.type()])
def perform(self, node, inputs, output_storage):
x = inputs[0]
z = output_storage[0]
z[0] = x * self.scal
def infer_shape(self, node, input_shapes):
return input_shapes
def grad(self, inputs, output_grads):
return [output_grads[0] * self.scal]
def R_op(self, inputs, eval_points):
if eval_points[0] is None:
return eval_points
return self.grad(inputs, eval_points)
class ScalMulV2(Op):
__props__ = ()
def make_node(self, x, scal):
x = as_tensor_variable(x)
scal = as_scalar(scal)
return Apply(self, [x, scal], [x.type()])
def perform(self, node, inputs, output_storage):
x = inputs[0]
scal = inputs[1]
z = output_storage[0]
z[0] = x * scal
def infer_shape(self, node, input_shapes):
return [input_shapes[0]]
def grad(self, inputs, output_grads):
return [output_grads[0] * inputs[1], (inputs[0] * outputs_grads[1]).sum()]
# def R_op(self, inputs, eval_points):
# if eval_points[0] is None:
# return eval_points
# return self.grad(inputs, eval_points)