簡體   English   中英

尋找多維函數的Hessian矩陣

[英]Finding Hessian matrix of multi dimensional function

我正在嘗試創建 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)

正如您從中獲得此代碼的問題所述, x是參數空間中均勻間隔網格節點處的函數值,而不是函數本身。

如果無法通過分析計算函數的 Hessian,則必須使用本示例代碼中的有限差分公式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM