美文网首页
理查德外推法计算偏导数近似值-python实现

理查德外推法计算偏导数近似值-python实现

作者: 斐硕人 | 来源:发表于2020-12-23 16:23 被阅读0次

一阶偏导数

一阶偏导数
思路

自变量: x0,...,xi+h,...,xn
带入理查德外推法公式,求对xi的偏导数
计算gradi: grad[grad0,...,gradi,...,null]

实现
import numpy as np

def computeGrad(pointFun, x, h):
    temp = np.array(x)
    grad = np.array(x)
    for i in range(len(x)):
        temp[i] += 0.5 * h
        grad[i] = 4 * pointFun(temp) / (3 * h)
        temp[i] -= h
        grad[i] -= 4 * pointFun(temp) / (3 * h)
        temp[i] += 3 * h / 2
        grad[i] -= pointFun(temp) / (6 * h)
        temp[i] -= 2 * h
        grad[i] += pointFun(temp) / (6 * h)
        temp[i] = x[i]
    return grad

# def fun(x):
#     return 100 * (x[1] - x[0] ** 2) ** 2 + (1 - x[0]) ** 2
#
#
# grad = computeGrad(fun, np.array([-1.2, 1.0]), 1e-3)
#
# print "grad:", grad

调用
def fun(x):
    return 100 * (x[1] - x[0] ** 2) ** 2 + (1 - x[0]) ** 2

# 手动计算偏导数:
def gradient(x):
    return np.array([-400 * x[0] * (x[1] - x[0] ** 2) - 2 * (1 - x[0]), 200 * (x[1] - x[0] ** 2)])

# 理查德外推法计算偏导数
def gradient(x):
    grad = computeGrad(fun, x, 1e-3)
    return grad

二阶偏导数

二阶偏导
思路

计算主对角线hessian矩阵元素fxixi''
计算下三角阵hessian矩阵元素fxixj''

实现
import numpy as np


def computeHes(pointFun, x, epsilon):
    temp = np.array(x)
    Hess = np.array(x)

    for index in range(len(x) - 1):
        Hess = np.vstack((Hess, x))

    for i in range(len(x)):
        for j in range(len(x)):
            Hess[i, j] = 0

    # compute fxx''
    for i in range(len(x)):
        temp[i] += epsilon / 2
        Hess[i, i] = 16 * pointFun(temp)
        temp[i] -= epsilon
        Hess[i, i] += 16 * pointFun(temp)
        temp[i] += 3 * epsilon / 2
        Hess[i, i] -= pointFun(temp)
        temp[i] -= 2 * epsilon
        Hess[i, i] -= pointFun(temp)
        Hess[i, i] -= 30 * pointFun(x)
        Hess[i, i] /= 3 * epsilon * epsilon

        temp[i] = x[i]

    # compute Zij
    for i in range(len(x)):
        for j in range(len(x)):
            if j > i:
                temp[i] += epsilon / 2
                temp[j] += epsilon / 2
                Hess[i, j] = 16 * pointFun(temp)
                temp[i] -= epsilon
                temp[j] -= epsilon
                Hess[i, j] += 16 * pointFun(temp)
                temp[i] += 3 * epsilon / 2
                temp[j] += 3 * epsilon / 2
                Hess[i, j] -= pointFun(temp)
                temp[i] -= 2 * epsilon
                temp[j] -= 2 * epsilon
                Hess[i, j] -= pointFun(temp)
                Hess[i, j] -= 30 * pointFun(x)
                Hess[i, j] /= (3 * epsilon * epsilon)
                Hess[i, j] -= Hess[i, i]
                Hess[i, j] -= Hess[j, j]
                Hess[i, j] /= 2

                temp[i] = x[i]
                temp[j] = x[j]

    # compute fij''  use fii'' & Zij
    for i in range(len(x)):
        for j in range(len(x)):
            if j > i:
                Hess[j, i] -= Hess[i, j]

    return Hess
测试
def fun(x):
    return x[0] * np.exp(x[0] - x[1] * x[1])


point = np.array([1.0, 1.0])

Hessian = computeHes(fun, point, 1e-3)

print "Hessian:", Hessian
测试结果
调用
def hessianMatrix(x):
    # Hes = np.array([[-400 * (x[1] - 3 * x[0] ** 2) + 2, -400 * x[0]], [-400 * x[0], 200]])
    Hes = computeHes(fun, x, 1e-3)
    return Hes

相关文章

  • 理查德外推法计算偏导数近似值-python实现

    一阶偏导数 思路 自变量: x0,...,xi+h,...,xn带入理查德外推法公式,求对xi的偏导数计算grad...

  • 梯度下降和上升

    在介绍梯度概念之前,首先需要引入偏导数和方向偏导数的概念, 偏导数: 所谓偏导数,简单来说是对于一个多元函数,选定...

  • Weighted Caffe Sigmoid Cross Ent

    Caffe Loss 层 - SigmoidCrossEntropyLoss 推导与Python实现 一、导数推导...

  • 多元函数偏导数及计算

    二元函数是空间中的曲面一元函数是平面内的平面 一,概念及二元函数 二,二元函数偏导数 二元函数偏导数时,自变量每次...

  • 偏导数

    在一些实际问题中,我们需要确定一个多元函数,关于某一个自变量的变化率,这种变化率就是函数对该自变量的偏导数 一元函...

  • 导数和偏导数

    导数和偏导数的定义 导数是只含一个自变量的方程中,当自变量有了一个很小的变化时函数的变化率. 偏导数是含有2个或者...

  • 导数、偏导数、梯度

    1. 导数 数学定义image.png 几何意义如果函数是曲线,那么导数就是切线的斜率。如果函数是曲面,那么导数就...

  • 牛顿法python3实现

    本文参考链接:牛顿法实现偏导数求解 输出结果 从输出结果可以看出最终收敛至(0,0),即f在(0,0)处取得极小值。

  • 图片梯度直方图的生成

    参考博客 梯度的计算 梯度模: 梯度方向: 以上都是按照定义计算的 一阶偏导数计算(h = 1):我们发现,L的一...

  • 导数,偏导数,导数方向,梯度

    导数 导数反应的变化率:一个函数在某一点的导数描述了这个函数在这一点附近的变化率。导数的本质是通过极限的概念对函数...

网友评论

      本文标题:理查德外推法计算偏导数近似值-python实现

      本文链接:https://www.haomeiwen.com/subject/kvfunktx.html