手写高斯牛顿python


import numpy as np
# 生成数据
def generate_noise_data(num=1000, noise_sigma=1.0):
    x = np.linspace(0, 1, num)
    a, b, c = 1, 2, 3
    func = lambda x: np.exp(a * x ** 2 + b * x + c)
    y = func(x) + np.random.normal(0, noise_sigma, num)
    return x, y

# 高斯牛顿法
def gauss_newton():
    x, y = generate_noise_data()
    a, b, c = 1, 1, 1
    detla_a = lambda a, b, c: - x ** 2 * np.exp(a * x ** 2 + b * x + c)
    detla_b = lambda a, b, c: - x * np.exp(a * x ** 2 + b * x + c)
    detla_c = lambda a, b, c: - np.exp(a * x ** 2 + b * x + c)

    # 目标函数
    func = lambda a, b, c: np.exp(a * x ** 2 + b * x + c)
    for epoch in range(101):
        r = y - func(a, b, c)
        Fun = np.sum(r ** 2)#目标函数

        J = np.vstack([detla_a(a, b, c), detla_b(a, b, c), detla_c(a, b, c)])
        H = J @ J.T#海瑟矩阵
        bb = np.sum(-J * r, axis=1).reshape(-1, 1)

        delta = np.linalg.inv(H) @ bb#迭代公式

        a += delta[0, 0]
        b += delta[1, 0]
        c += delta[2, 0]

        if epoch % 10 == 0:
            print(f"epoch: {epoch}, Fun: {Fun}, a: {a}, b: {b}, c: {c}")


if __name__ == "__main__":
    gauss_newton()

你可能感兴趣的:(人工智能,python,开发语言,计算机视觉)