赞
踩
动手练习1.3
执行上述算子的反向过程,并验证梯度是否正确。
- import math
-
-
- class Op(object):
- def __init__(self):
- pass
-
- def __call__(self, inputs):
- return self.forward(inputs)
-
- # 前向函数
- # 输入:张量inputs
- # 输出:张量outputs
- def forward(self, inputs):
- # return outputs
- raise NotImplementedError
-
- # 反向函数
- # 输入:最终输出对outputs的梯度outputs_grads
- # 输出:最终输出对inputs的梯度inputs_grads
- def backward(self, outputs_grads):
- # return inputs_grads
- raise NotImplementedError
-
-
- class add(Op):
- def __init__(self):
- super(add, self).__init__()
-
- def __call__(self, x, y):
- return self.forward(x, y)
-
- def forward(self, x, y):
- self.x = x
- self.y = y
- outputs = x + y
- return outputs
-
- def backward(self, grads):
- grads_x = grads * 1
- grads_y = grads * 1
- return grads_x, grads_y
-
-
- class multiply(Op):
- def __init__(self):
- super(multiply, self).__init__()
-
- def __call__(self, x, y):
- return self.forward(x, y)
-
- def forward(self, x, y):
- self.x = x
- self.y = y
- outputs = x * y
- return outputs
-
- def backward(self, grads):
- grads_x = grads * self.y
- grads_y = grads * self.x
- return grads_x, grads_y
-
-
- class exponential(Op):
- def __init__(self):
- super(exponential, self).__init__()
-
- def forward(self, x):
- self.x = x
- outputs = math.exp(x)
- return outputs
-
- def backward(self, grads):
- grads = grads * math.exp(self.x)
- return grads
-
-
- a, b, c, d = 2, 3, 2, 2
- multiply_op1 = multiply()
- f1=multiply_op1(a,b)
- multiply_op2 = multiply()
- f2=multiply_op2(c,d)
- add_op = add()
- f3=add_op(f1,f2)
- exp_op = exponential()
- f4=exp_op(f3)
-
- print(f4)
-
- val1=exp_op.backward(grads=1)
- val2=add_op.backward(val1)
- val3=multiply_op1.backward(val2[0])
- val4=multiply_op2.backward(val2[0])
- print(val3)
- print(val4)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。