NDArray自动求导

用代码实现如下：

``` 1 import mxnet.ndarray as nd
3 x = nd.array([[1,2],[3,4]])
4 print(x)
6 with ag.record():
7     y = 2*x**2
8 y.backward() #求导
10 print(z) #打印结果```
```[[ 1.  2.]
[ 3.  4.]]
<NDArray 2x2 @cpu(0)>

[[  4.   8.]
[ 12.  16.]]
<NDArray 2x2 @cpu(0)>```

NDArray还能对诸如if的控制分支进行求导，比如下面这段代码：

```1 def f(a):
2     if nd.sum(a).asscalar()<15: #如果矩阵a的元数和<15
3         b = a*2 #则所有元素*2
4     else:
5         b = a
6     return b```

```import mxnet.ndarray as nd

def f(a):
if nd.sum(a).asscalar()<15: #如果矩阵a的元数和<15
b = a*2 #则所有元素平方
else:
b = a
return b

#注:1+2+3+4<15，所以进入b=a*2的分支
x = nd.array([[1,2],[3,4]])
print("x1=")
print(x)
with ag.record():
y = f(x)
print("y1=")
print(y)
y.backward() #dy/dx = y/x 即:2

x = x*2
print("x2=")
print(x)
with ag.record():
y = f(x)
print("y2=")
print(y)
y.backward()
```x1=
[[ 1.  2.]
[ 3.  4.]]
<NDArray 2x2 @cpu(0)>
y1=
[[ 2.  4.]
[ 6.  8.]]
<NDArray 2x2 @cpu(0)>
[[ 2.  2.]
[ 2.  2.]]
<NDArray 2x2 @cpu(0)>
x2=
[[ 2.  4.]
[ 6.  8.]]
<NDArray 2x2 @cpu(0)>
y2=
[[ 2.  4.]
[ 6.  8.]]
<NDArray 2x2 @cpu(0)>
[[ 1.  1.]
[ 1.  1.]]
<NDArray 2x2 @cpu(0)>```

``` 1 import mxnet.ndarray as nd
3
4 x = nd.array([[1,2],[3,4]])
5 print("x=")
6 print(x)
7
9 with ag.record():
10     y = 2*x*x
11
12 head = nd.array([[10, 1.], [.1, .01]]) #所谓的"头梯度"
16
```x=
[[ 1.  2.]
[ 3.  4.]]
<NDArray 2x2 @cpu(0)>
[[ 10.     1.  ]
[  0.1    0.01]]
<NDArray 2x2 @cpu(0)>
[[ 40.           8.        ]
[  1.20000005   0.16      ]]
<NDArray 2x2 @cpu(0)>```

``` 1 import mxnet.ndarray as nd
3
4 x = nd.array([[1,2],[3,4]])
5 print("x=")
6 print(x)
7
9 with ag.record():
10     y = x**2
11     z = y**2 + y
12
13 z.backward()
14
17
18 print("w=")
19 w = 4*x**3 + 2*x
20 print(w) # 验证结果```
```x=
[[ 1.  2.]
[ 3.  4.]]
<NDArray 2x2 @cpu(0)>
[[   6.   36.]
[ 114.  264.]]
<NDArray 2x2 @cpu(0)>
w=
[[   6.   36.]
[ 114.  264.]]
<NDArray 2x2 @cpu(0)>```

0 条评论

73460

20350

79580

27820

40950

3.2K90

1.5K70

36370