Theano 学习
来源:互联网 发布:水滴互助源码 编辑:程序博客网 时间:2024/06/10 01:32
1. 计算梯度
创建一个函数
以下就是计算该梯度的 Python 代码:
import numpyimport theanoimport theano.tensor as Tfrom theano import ppx = T.dscalar('x')y = x ** 2gy = T.grad(y, x)pp(gy)f = theano.function([x], gy)print(f(4)) #print 8print(numpy.allclose(f(94.2), 188.4)) #print Trueprint(pp(f.maker.fgraph.outputs[0])) #print (TensorConstant{2.0} * x)
我们能从
同时也能计算复杂表达式的梯度,例如
其导数:
import numpyimport theanoimport theano.tensor as Tx = T.dmatrix('x')s = T.sum(1 / (1 + T.exp(-x)))gs = T.grad(s, x)dlogistic = theano.function([x], gs)print(dlogistic([[0, 1], [-1, -2]])) #print [[ 0.25 0.19661193] [ 0.19661193 0.10499359]]
总的来说,对于标量表达式
补充:
2. 计算Jacobian
在 Theano 中,Jacobian 表示输出函数关于其输入的一阶偏导数(在数学中,就称为雅可比矩阵). Theano 中使用
我们使用
补充:
import theanoimport theano.tensor as Tx = T.dvector('x')y = x ** 2J, updates = theano.scan(lambda i, y, x: T.grad(y[i], x), sequences = T.arange(y.shape[0]), non_sequences=[y, x])f = theano.function([x], J, updates = updates)print(f([4, 4])) #print [[ 8. 0.] [ 0. 8.]]
在此代码中,使用
补充:关于
3. 计算 Hessian
在 Theano 中,术语 Hessian 和数学中的概念一样:是标量输出函数关于向量输入的二阶偏微分矩阵. Theano 中使用
手动计算 Hessian 和手动计算 Jacobian 类似,唯一的不同就是用
import theanoimport theano.tensor as Tx = T.dvector('x')y = x ** 2cost = y.sum()gy = T.grad(cost, x)H, updates = theano.scan(lambda i, gy, x: T.grad(gy[i], x), sequences = T.arange(gy.shape[0]), non_sequences=[gy, x])f = theano.function([x], H, updates = updates)print(f([4, 4]))#print [[ 2. 0.] [ 0. 2.]]
4. Jacobian 乘以向量
在解释算法的过程中,有时我们需要表示 Jacobian 乘以向量,或者是向量乘以 Jacobian . 与先评估 Jacobian 再做乘积相比,目前有许多方式能直接计算所需结果从而避免实际的 Jacobian 评估. 这能带来显著的性能提升,具体参考下文:
[Barak A. Pearlmutter, “Fast Exact Multiplication by the Hessian”, Neural Computation, 1994]
原则上我们希望 Theano 能为我们自动识别这些模式,实际上,以通用的方式进行优化是很困难的.
因此,提供了特殊的函数专用于这些任务.
R-operator
R-operator 用于评估 Jacobian 和向量之间的乘积,写作:
import theanoimport theano.tensor as TW = T.dmatrix('W')V = T.dmatrix('V')x = T.dvector('x')y = T.dot(x, W)JV = T.Rop(y, W, V)f = theano.function([W, V, x], JV)print(f([[1, 1], [1, 1]], [[2, 2], [2, 2]], [0, 1]))#print [ 2. 2.]
L-operator
与 R-operator 类似, L-operator是计算行向量与 Jacobian 的乘积. 数学形式为:
import theanoimport theano.tensor as TW = T.dmatrix('W')V = T.dmatrix('V')x = T.dvector('x')y = T.dot(x, W)VJ = T.Lop(y, W, V)f = theano.function([V, x], VJ)print(f([2, 2], [0, 1]))#print [[ 0. 0.] [ 2. 2.]]
补充:
5. Hessian 乘以向量
假如你需要计算 Hessian 乘以一个向量,你可以使用上面定义的算子直接计算,这比先计算精确的 Hessian ,再计算乘积更有效. 由于 Hessian 矩阵的对称性,你有两种方式得到同样的结果,尽管这两种方式可能展现出不同的性能. 下面给出这两种方式:
- 1
import theanoimport theano.tensor as Tx = T.dvector('x')v = T.dvector('v')y = T.sum(x ** 2)gy = T.grad(y, x)vH = T.grad(T.sum(gy * v), x)f = theano.function([x, v], vH)print(f([4, 4], [2, 2]))#print [ 4. 4.]
- 2使用 R-operator
import theanoimport theano.tensor as Tx = T.dvector('x')v = T.dvector('v')y = T.sum(x ** 2)gy = T.grad(y, x)Hv = T.Rop(gy, x, v)f = theano.function([x, v], Hv)print(f([4, 4], [2, 2]))#print [ 4. 4.]
6. 指示
1
grad 函数以符号方式工作:接收与返回都是 Theano 变量2
grad 可以比作宏,因为它可以重复使用3
标量函数只能被grad 直接处理,矩阵能够通过重复应用来处理4
内置函数能有效的计算向量乘以 Jacobian 和向量乘以 Hessian5
正在优化有效的计算完整的 Jacobian 和 Hessian 矩阵以及 Jacobian 乘以向量.
- theano 学习
- theano学习
- Theano 学习
- Theano学习三----Theano function
- Theano学习四----Theano shared
- Theano学习五----Theano RandomStreams
- 深度学习Theano教程Theano tutorial
- Theano 学习numpy.asarray(), theano.shared()
- theano学习--theano.function
- theano学习--theano.tensor
- Theano学习一----安装
- Theano学习二----numpy
- theano学习札记
- Theano开始学习2
- Theano学习1
- Theano深度学习入门
- theano学习1
- theano学习2MLP
- linux下could not find or load the qt platform plugin xcb问题
- 简易版之最短距离
- dubbo-admin-2.5.4在tomcat中部署失败的问题
- sql注入
- JAVA学习之旅
- Theano 学习
- 99% 的人都理解错了 HTTP 中 GET 与 POST 的区别
- Django笔记
- MySQL并发复制系列一:binlog组提交
- 在 docker 之间导出导入镜像
- javaWeb 20170710学习笔记 SVN , Working Set 及导入后import override文件报错处理
- webview上dialog不弹出的问题
- lua(nginx) 获取请求的请求头
- 数据挖掘:概念与技术(第三版)之第六章的学习记录