# Theano学习笔记（四）——导数

fill((x** TensorConstant{2}), TensorConstant{1.0})指创建一个x**2大小的矩阵，并填充1。

importtheano.tensor as T
fromtheano import pp
fromtheano import function
x= T.dscalar('x')
y= x ** 2
printpp(gy)
f= function([x], gy)
printf(4)
printpp(f.maker.fgraph.outputs[0])
>>>
((fill((x** TensorConstant{2}), TensorConstant{1.0}) * TensorConstant{2}) * (x **(TensorConstant{2} - TensorConstant{1})))
8.0
(TensorConstant{2.0}* x)

$\frac{{ds\left( x \right)}}{{dx}} = s\left( x \right) \cdot \left( {1 - s\left( x \right)} \right)$

importtheano.tensor as T
fromtheano import function
x= T.dmatrix('x')
s= T.sum(1 / (1 + T.exp(-x)))
dlogistic= function([x], gs)
printdlogistic([[0, 1], [-1, -2]])
>>>
[[0.25        0.19661193]
[ 0.19661193 0.10499359]]

$J = \left[ {\begin{array}{*{20}{c}}{\frac{{\partial {u_1}}}{{\partial {x_1}}}}&{\frac{{\partial {u_1}}}{{\partial {x_2}}}}& \cdots &{\frac{{\partial {u_1}}}{{\partial {x_n}}}}\\{\frac{{\partial {u_2}}}{{\partial {x_1}}}}&{\frac{{\partial {u_2}}}{{\partial {x_2}}}}& \cdots &{\frac{{\partial {u_2}}}{{\partial {x_n}}}}\\ \vdots & \vdots & \ddots & \vdots \\{\frac{{\partial {u_n}}}{{\partial {x_1}}}}&{\frac{{\partial {u_n}}}{{\partial {x_2}}}}& \cdots &{\frac{{\partial {u_n}}}{{\partial {x_n}}}}\end{array}} \right]$

scan可以提高创建符号循环效率。

lambda~是python内建的magicfunction.

x= T.dvector('x')
y = x ** 2
f([4, 4])
>>>
[[ 8.  0.]
[ 0. 8.]]

$H\left( f \right) = \left[ {\begin{array}{*{20}{c}}{\frac{{{\partial ^2}f}}{{\partial x_1^2}}}&{\frac{{{\partial ^2}f}}{{\partial {x_1}\partial {x_2}}}}& \cdots &{\frac{{{\partial ^2}f}}{{\partial {x_1}\partial {x_n}}}}\\{\frac{{{\partial ^2}f}}{{\partial {x_2}\partial {x_1}}}}&{\frac{{{\partial ^2}f}}{{\partial x_2^2}}}& \cdots &{\frac{{{\partial ^2}f}}{{\partial {x_2}\partial {x_n}}}}\\ \vdots & \vdots & \ddots & \vdots \\{\frac{{{\partial ^2}f}}{{\partial {x_n}\partial {x_1}}}}&{\frac{{{\partial ^2}f}}{{\partial {x_n}\partial {x_2}}}}& \cdots &{\frac{{{\partial ^2}f}}{{\partial x_n^2}}}\end{array}} \right]$

x= T.dvector('x')
y = x** 2
cost= y.sum()
f([4,4])
>>>
[[2.  0.]
[ 0. 2.]]

x可以由向量扩展成矩阵。雅克比右乘使用Rop:

W = 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)
printf([[1, 1], [1, 1]], [[2, 2], [2, 2]], [0,1])
>>>
[2.  2.]

$\frac{{\partial f\left( x \right)}}{{\partial x}}v$

import theano
import theano.tensor as T
from theano import function
x = T.dvector('x')
v =T.dvector('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])
>>>
[[0.  0.]
[ 2. 2.]]

$v\frac{{\partial f\left( x \right)}}{{\partial x}}$

import theano
import theano.tensor as T
from theano import function
x= T.dvector('x')
v= T.dvector('v')
y= T.sum(x ** 2)
Hv= T.Rop(gy, x, v)
f= theano.function([x, v], Hv)
printf([4, 4], [2, 2])
>>>
[4.  4.]

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客