我如何在数值上近似一个函数的雅可比行列式和黑森州式? [英] How do I approximate the Jacobian and Hessian of a function numerically?

查看:127
本文介绍了我如何在数值上近似一个函数的雅可比行列式和黑森州式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有一个函数:

I have a function in Python:

def f(x):
    return x[0]**3 + x[1]**2 + 7 
    # Actually more than this.
    # No analytical expression

它是向量的标量值函数.

It's a scalar valued function of a vector.

我该如何以数值方式以numpy或scipy近似表示该函数的Jacobian和Hessian?

How can I approximate the Jacobian and Hessian of this function in numpy or scipy numerically?

推荐答案

(于2017年底更新,因为此空间中有很多更新.)

(Updated in late 2017 because there's been a lot of updates in this space.)

您最好的选择可能是自动区分.现在有很多软件包可以使用,因为它是深度学习的标准方法:

Your best bet is probably automatic differentiation. There are now many packages for this, because it's the standard approach in deep learning:

  • Autograd 可与大多数numpy代码透明地工作.它是纯Python,对于典型功能几乎不需要任何代码更改,而且速度相当快.
  • 有许多面向深度学习的库可以做到这一点. 最受欢迎的是 TensorFlow PyTorch Theano MXNet .每种方法都需要您使用类似numpy但不必要地不同的API重写函数,作为回报,它会为您提供GPU支持以及您可能会或可能不会在意的一系列面向深度学习的功能
  • FuncDesigner 是我尚未使用的较旧的软件包,其网站目前已关闭. /li>
  • Autograd works transparently with most numpy code. It's pure-Python, requires almost no code changes for typical functions, and is reasonably fast.
  • There are many deep-learning-oriented libraries that can do this. Some of the most popular are TensorFlow, PyTorch, Theano, Chainer, and MXNet. Each will require you to rewrite your function in their kind-of-like-numpy-but-needlessly-different API, and in return will give you GPU support and a bunch of deep learning-oriented features that you may or may not care about.
  • FuncDesigner is an older package I haven't used whose website is currently down.

另一种选择是使用有限差异进行近似估算,基本上只是评估(f(x + eps) - f(x - eps)) / (2 * eps)(但显然需要付出更多的努力).与其他方法相比,这可能会更慢且准确性更低,尤其是在中等高度的尺寸中,但这是完全通用的并且不需要更改代码. numdifftools 似乎是为此的标准Python软件包.

Another option is to approximate it with finite differences, basically just evaluating (f(x + eps) - f(x - eps)) / (2 * eps) (but obviously with more effort put into it than that). This will probably be slower and less accurate than the other approaches, especially in moderately high dimensions, but is fully general and requires no code changes. numdifftools seems to be the standard Python package for this.

您还可以尝试使用 SymPy 查找完全符号导数,但这将是一个相对手动的过程.

You could also attempt to find fully symbolic derivatives with SymPy, but this will be a relatively manual process.

这篇关于我如何在数值上近似一个函数的雅可比行列式和黑森州式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆