码迷,mamicode.com
首页 > 其他好文 > 详细

autograd手动仿真手记

时间:2018-02-17 13:09:55      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:put   net   time   body   表格   程序   应用   ogr   attach   

Preface

查看损失函数的时候,突然想手工模拟梯度传递过程。

Script

损失函数是这个: \(l(x)=-||x| - |x-1||\)。程序脚本:

import mxnet as mx;import numpy as np;import mxnet.autograd as ag
x=mx.nd.zeros((1,))+2
x.attach_grad()
with ag.record():
    l=-mx.nd.abs( mx.nd.abs(x) - mx.nd.abs(x-1.)   )
    l.backward(mx.nd.ones_like(l))
print x,l,‘x.grad:‘,x.grad

先回顾一下梯度传递的理论基础:如果\(y=f(z(x))\),其中\(f,z\)都可看作不同layer代表的映射,那么需要求得对\(x\)的梯度,易于推广的方法是链式法则:
\[ y_x = f_z(z)\times z_x(x) \]
得到普遍应用的原因是易于模块化,每个layer只需要把自己那部分设计好,就可以支持构建自定义系统。
明白这点后,模拟过程最好就是列表:
比如现在\(x=2\)

Symbol Op Input Output Grad
x | | 2 2 -1x1=-1
x-1 + 2 1 1x1=1
|x-1| | | 1 1 1x1/1=1
|x|-|x-1| +,- 2, 1 1 -1x1=-1, -1x-1=1
| |x| - |x-1| | | | 1 1 -1 x 1/1=-1
-| |x| - |x-1| | - 1 -1 1 x -1 =-1
l 1

绝对值的导数:\(f(x)=|x| \Rightarrow f^,(x)=\frac{x}{f(x)}\)
填写Input/Output的过程类似于Forward过程;计算Grad类比于Backward,需要从表格底部算起。
x的节点被复用一次:\(grad_x=1+(-1)=0\)

Note

\(f(0)=|0|\)处,MXNet使用的应该是置零操作。参考\(x=0\Rightarrow 1, x=0.5\Rightarrow 0, x=1\Rightarrow -1\)

autograd手动仿真手记

标签:put   net   time   body   表格   程序   应用   ogr   attach   

原文地址:https://www.cnblogs.com/chenyliang/p/8451547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!