机器学习-微积分
最大值、最小值的点的导数=0,即函数的瞬时变化率=0
""" learning store for machine learning """ import time import numpy as np def time_cost(function): """ 用于统计执行耗时的装饰器 """ def wrapper(*args, **kwargs): start = time.time() res = function(*args, **kwargs) end = time.time() print(f"{function.__name__} cost {end - start:.8f} s") return res return wrapper @time_cost def cal_delta(hessian_matrix, gradient_vector): """ cal next step """ hessian_inv = np.linalg.inv(hessian_matrix) vector = gradient_vector.reshape(-1, 1) result = np.dot(hessian_inv, vector) return result def cal_next_step(cur_step_vector, hessian_matrix, gradient_vector): """ 使用海森矩阵牛顿法推理下一步的点位 """ delta_vector = cal_delta(hessian_matrix, gradient_vector) return (cur_step_vector - delta_vector).reshape(1,-1).tolist()[0] if __name__ == "__main__": hessian = np.array([[198.4, -2.6], [-2.6, 157.6]]) gradient = np.array([277.6, 213.6]) cur_point = np.array([[4],[4]]) next_point = cal_next_step(cur_point, hessian, gradient) print(next_point)