A.1_问题确认

A.1 问题确认

首先对这个问题进行梳理。在步骤14中,为了能够重复使用同一个变量,我们对Variable类的backward方法做了如下修改。

class Variable: def backward(self): if self.grad is None: self.grad  $=$  np.ones_like(self.data) funcs  $=$  [self creator] while funcs: f  $=$  funcs.pop() gys  $=$  [output.grad for output in f.outputs] gxs  $=$  f.backup(gys) for x,gx in zip(f-inputs,gxs): if x.grad is None: x.grad  $\equiv$  gx else: x.grad  $=$  x.grad +gx if x creator is not None: funcs.append(x creator)

阴影部分是修改的地方。简单来说,第一次传播导数(梯度)时,进行的是赋值操作,即 x.grad=gxx.\mathrm{grad} = g x ,之后进行的是加法运算,即 x.grad=x.grad+gxx.\mathrm{grad} = x.\mathrm{grad} + g x 。其中的gx是ndarray实例。步骤14提到将加法运算的代码改写为 x.grad+=gxx.\mathrm{grad} += g x 在某些情况下会出现问题,这里说明其原因。

A.1_问题确认 - 深度学习自制框架 | OpenTech