theano 入门教程1.7

1.7.1计算导数
使用theano.tensor.grad() 函数计算梯度 

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

1.7.2计算Jacobian

theano中提供了theano.gradient.jacobian()宏计算Jacobian。

下面的代码阐明如何手动产生Jacobain。
>>> x = T.dvector('x')
>>> y = x ** 2
>>> J, updates = theano.scan(lambda i, y,x : T.grad(y[i], x), sequences=T.arange(y.shape[0]), non_sequences=[y,x])
>>> f = function([x], J, updates=updates)
>>> f([4, 4])
array([[ 8.,  0.],       [ 0.,  8.]])

scan()函数是用来产生循环类型的变量。
T.arange用来产生一个0到y.shape[0]的序列。

1.7.3 计算Hessian

theano中提供了theano.gradient.hessian()宏来计算Hessian

下面的代码阐明如何自己手动产生Hessian
>>> x = T.dvector('x')
>>> y = x ** 2
>>> cost = 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 = function([x], H, updates=updates)
>>> f([4, 4])
array([[ 2.,  0.],       [ 0.,  2.]])

1.7.4
Jacobian乘以向量
1.7.4.1 右乘
类似于这种形式, 
>>> 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)
>>> f([[1, 1], [1, 1]], [[2, 2], [2, 2]], [0,1])
array([ 2.,  2.])

1.7.4.2 左乘
类似于这种形式, 

>>> W = T.dmatrix('W')
>>> 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)
>>> f([2, 2], [0, 1])
array([[ 0.,  0.],       [ 2.,  2.]])

1.7.5 Hessian乘以向量








posted @ 2014-06-06 15:00  fireae  阅读(1109)  评论(0编辑  收藏  举报