python - 寻找多维函数的Hessian矩阵
问题描述
我正在尝试创建 10 维凸函数。我知道它的 hessian 矩阵的特征值必须是正的,函数才能是凸的。我正在做下面的事情来找到 hessian 矩阵,但它的输入是一个数组,我不知道如何将函数表示为数组。
def hessian(x):
"""
Calculate the hessian matrix with finite differences
Parameters:
- x : ndarray
Returns:
an array of shape (x.dim, x.ndim) + x.shape
where the array[i, j, ...] corresponds to the second derivative x_ij
"""
x_grad = np.gradient(x)
hessian = np.empty((x.ndim, x.ndim) + x.shape, dtype=x.dtype)
for k, grad_k in enumerate(x_grad):
# iterate over dimensions
# apply gradient again to every component of the first derivative.
tmp_grad = np.gradient(grad_k)
for l, grad_kl in enumerate(tmp_grad):
hessian[k, l, :, :] = grad_kl
return hessian
x = np.random.randn(100,100)
t=hessian(x)
解决方案
推荐阅读
- c - 函数不能返回returnSize数组中的元素的问题
- python - 错误:“无法使用无作为查询值”尝试在 Django 中使用 ListView 分页时
- sql - 如何在 sql oracle 中插入子类型值?
- swift - youtube-ios-player-helper-swift 上的 UIWebView 到 WKWebView
- javascript - 添加新数据时自动刷新另一个页面 Laravel 5.8 Ajax
- mysql - 我已在数据库中插入数据但不知道如何在 laravel 中显示
- node.js - 使用nodejs一段时间后如何动态更新数据?
- html - 定位后引导列重叠
- wordpress - wordpress 中的 wp-includes 是什么以及它的用途?
- python - 坚持连接 Beautiful Soup 和 Google Sheets