首页 > 编程知识 正文

hessian矩阵计算公式,hessian矩阵理解

时间:2023-05-05 05:55:01 阅读:176699 作者:1118

机器学习课上提到了这个矩阵,这个矩阵从哪里来,有什么用呢? 让我们先来看看定义:

htdls矩阵(Hessian Matrix )也被翻译为海森矩阵、海森矩阵、海森矩阵等,是基于多元函数的二阶偏导数的方阵,用于描述函数的局部曲率。 htdls矩阵是19世纪由德国数学家Ludwig Otto Hesse提出并命名的。 htdls矩阵常用于sydxh法求解优化问题。

一般来说,sydxh方法主要应用于两个方面: 1、求方程根; 2、优化。

在机器学习中,可以考虑用它来计算n值少的数据,在图像处理中可以提取图像的特征,在金融中用于量化分析。

图像处理可以看到这个连接:

3358 blog.csdn.net/Jia 20003/article/details/16874237

量化分析可以看到这一点:

33558 oo kiddy.iteye.com/blog/2204127

尝试使用TensorFlow和htdls矩阵求解以下方程:

代码如下所示。

33558 www.Sina.com/viewplaincopy # python3.5.3gg dsw # http://edu.csdn.net/course/detail/2592 # importtensorflon dtype=tf.float32 ) defcompute_hessian(fn,vars ) 3360mat=[]forV1invars3360temp

1               temp.append(tf.gradients(tf.gradients(f, v2)[0], v1)[0])           temp = [cons(0) if t == None else t for t in temp] # tensorflow returns None when there is no gradient, so we replace None with 0           temp = tf.stack(temp)           mat.append(temp)       mat = tf.stack(mat)       return mat         x = tf.Variable(np.random.random_sample(), dtype=tf.float32)   y = tf.Variable(np.random.random_sample(), dtype=tf.float32)         f = tf.pow(x, cons(2)) + cons(2) * x * y + cons(3) * tf.pow(y, cons(2)) + cons(4)* x + cons(5) * y + cons(6)   # arg1: our defined function, arg2: list of tf variables associated with the function   hessian = compute_hessian(f, [x, y])      sess = tf.Session()   sess.run(tf.global_variables_initializer())   print(sess.run(hessian))   输出结果如下:


再来举多一个例子的源码,它就是用来计算量化分析,这个代码很值钱啊,如下:

[python]  view plain  copy #python 3.5.3  ggdsw       #http://edu.csdn.net/course/detail/2592       #    import numpy as np   import scipy.stats as stats   import scipy.optimize as opt      #构造Hessian矩阵   def rosen_hess(x):       x = np.asarray(x)       H = np.diag(-400*x[:-1],1) - np.diag(400*x[:-1],-1)       diagonal = np.zeros_like(x)       diagonal[0] = 1200*x[0]**2-400*x[1]+2       diagonal[-1] = 200       diagonal[1:-1] = 202 + 1200*x[1:-1]**2 - 400*x[2:]       H = H + np.diag(diagonal)       return H   def rosen(x):       """The Rosenbrock function"""       return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)   def rosen_der(x):       xm = x[1:-1]       xm_m1 = x[:-2]       xm_p1 = x[2:]       der = np.zeros_like(x)       der[1:-1] = 200*(xm-xm_m1**2) - 400*(xm_p1 - xm**2)*xm - 2*(1-xm)       der[0] = -400*x[0]*(x[1]-x[0]**2) - 2*(1-x[0])       der[-1] = 200*(x[-1]-x[-2]**2)       return der      x_0 = np.array([0.5, 1.6, 1.1, 0.8, 1.2])      res = opt.minimize(rosen, x_0, method='Newton-CG', jac=rosen_der, hess=rosen_hess,                      options={'xtol': 1e-8, 'disp': True})   print("Result of minimizing Rosenbrock function via Newton-Conjugate-Gradient algorithm (Hessian):")   print(res)  
输出结果如下:

====================== RESTART: D:/AI/sample/tf_1.43.py ======================
Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 20
         Function evaluations: 22
         Gradient evaluations: 41
         Hessian evaluations: 20
Result of minimizing Rosenbrock function via Newton-Conjugate-Gradient algorithm (Hessian):
     fun: 1.47606641102778e-19
     jac: array([ -3.62847530e-11,   2.68148992e-09,   1.16637362e-08,
         4.81693414e-08,  -2.76999090e-08])
 message: 'Optimization terminated successfully.'
    nfev: 22
    nhev: 20
     nit: 20
    njev: 41
  status: 0
 success: True
       x: array([ 1.,  1.,  1.,  1.,  1.])
>>> 


可见hessian矩阵可以使用在很多地方了吧。

1. C++标准模板库从入门到精通 
http://edu.csdn.net/course/detail/3324 2.跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901 3. 跟老菜鸟学python http://edu.csdn.net/course/detail/2592 4. 在VC2015里学会使用tinyxml库 http://edu.csdn.net/course/detail/2590 5. 在Windows下SVN的版本管理与实战   http://edu.csdn.net/course/detail/2579 6.Visual Studio 2015开发C++程序的基本使用  http://edu.csdn.net/course/detail/2570 7.在VC2015里使用protobuf协议 http://edu.csdn.net/course/detail/2582 8.在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672


可以看更多的网站:

http://blog.csdn.net/ubunfans/article/details/41520047

http://blog.csdn.net/baimafujinji/article/details/51167852

http://jacoxu.com/jacobian%E7%9F%A9%E9%98%B5%E5%92%8Chessian%E7%9F%A9%E9%98%B5/

http://www.cnblogs.com/logosxxw/p/4651413.html


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。