利用Theano求梯度与和hessian矩阵

利用Theano求多维函数的梯度和hessian矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import theano
import theano.tensor as T
from theano import function

# 求梯度
x, y, a, b = T.dscalars('x','y','a','b')
s = T.sum((x + 10 * y) ** 2 + 5 * (a - b) ** 2 + (y - 2 * a) ** 4 + 10 * (x - b) ** 4)
gs = T.grad(s, [x,y,a,b])
dlogistic = function([x,y,a,b], gs)
print dlogistic(0.22, 0.54, 0.67, 0.78)

# 求hessian矩阵
x = T.dvector('x')
y = (x[0] + 10 * x[1]) ** 2 + 5 * (x[2] - x[3]) ** 2 + (x[1] - 2 * x[2]) ** 4 + 10 * (x[0] - x[3]) ** 4
cost = y.sum()
f = theano.gradient.hessian(y, wrt=x)
dlogistic = theano.function([x], f)
print dlogistic([0.22, 0.54, 0.67, 0.78])

# 求梯度
f = T.grad(cost, wrt=x)
dlogistic = function([x], f)
print dlogistic([0.22, 0.54, 0.67, 0.78])
------ 本文结束------
坚持原创技术分享,您的支持将鼓励我继续创作!

欢迎关注我的其它发布渠道