蓝色天空网mbsky.com · 知识 · 实践 · 创造AI 数学基础 · 本地阅读预览
教程 / AI / 智能开发 / AI 数学基础

泰勒展开

用局部多项式、误差和曲率理解函数近似与优化步骤。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

指定中心点和最高次数

取展开点 a,令位移 h=x−a。若 f 在 a 可导,一阶近似是 f(a)+f′(a)h,误差相对 |h| 趋零。它用中心点的切线估算附近的函数值。

若二阶导数在 a 附近连续,二阶式再加 f″(a)h²/2,误差相对 h² 趋零。以 f(x)=x²、a=3、h=0.25 为例,一阶近似为 10.5,二阶得到 10.5625,等于该点的函数值。

有限阶展开的精度依赖导数与余项。若要让无穷泰勒级数在邻域中表示原函数,还需检查解析性。一个光滑反例是 x≠0 时的 exp(−1/x²) 与零点处取值 0;它在零点的各阶导数都是零,离开零点后函数取正值。

读懂正弦与指数的误差

角度用弧度。sin x 在零点的一阶式为 x,所以 sin(0.1) 约为 0.1。实际值约为 0.099833,绝对误差约为 0.000167。

对 x=0.5,保留最高次数 1、3、5,结果分别为 0.50000000、0.47916667、0.47942708,对应误差约 2.06×10⁻²、2.59×10⁻⁴、1.54×10⁻⁶。最高次数与非零项数不同。这三个多项式实际分别累加了 1、2、3 个非零项。此处交错项递减,首个舍弃项给出误差上界。

一阶步进与二阶局部模型

对可微损失,负梯度在欧氏单位方向中给出局部线性项最快的下降方向。把方向变成实际一步还要选步长。学习率过大时,局部近似误差可能使目标值升高。

二阶模型用 Hessian H 描述局部曲率。H 正定时,解 Hs=−g 得到该二次模型的唯一极小步。在一般目标中,可配合阻尼、线搜索或信赖域,避免奇异或不定的曲率使步子失效。密集 Hessian 的储存量随参数数目的平方增长。结构化或近似求解可改变成本。

XGBoost 在当前预测附近展开损失,按样本使用一阶导数 gᵢ 和二阶导数 hᵢ。用同一个叶子分数 w 修正这组样本时,二阶项与 L2 正则相加,成为 (H+λ)w²/2,线性项是 Gw。H+λ>0 时最小值在 w=−G/(H+λ)。效果仍取决于目标、数据和设置。

零点附近的展开

函数多项式或级数形式适用说明
eˣ1+x+x²/2!+x³/3!+…各阶导数在零点为 1。保留三次项后仍有余项。
sin xx−x³/3!+x⁵/5!−…零点偶次项为零,奇次项正负交替。

核对近似误差、牛顿步与叶子权重

python · 阅读源码
"""Compare Taylor polynomials and calculate a local quadratic step."""
from fractions import Fraction as F
import json
from math import exp,factorial,sin
import platform

import numpy as np


def sin_polynomial(x, degree):
    return sum((-1)**((k-1)//2)*x**k/factorial(k) for k in range(1,degree+1,2))


def exp_polynomial(x, degree):
    return sum(x**k/factorial(k) for k in range(degree+1))


def main():
    cases = []

    def check(name,inputs,expected,actual,operation):
        assert expected == actual,(name,expected,actual)
        cases.append(dict(name=name,input=inputs,operation=operation,expected=expected,actual=actual,passed=True))

    degrees = [1,3,5]
    x = F(1,2)
    estimates = [sin_polynomial(x,d) for d in degrees]
    check("sin_degrees_one_three_five", {"x":"1/2","degrees":degrees}, ["1/2","23/48","1841/3840"],
          list(map(str, estimates)), "sum the signed exact rational terms")
    actual = sin(float(x))
    errors = [abs(float(v)-actual) for v in estimates]
    check("sin_alternating_error_bound", {"x":"1/2","degrees":degrees}, [True]*3,
          [error<=float(x**(d+2)/factorial(d+2)) for error,d in zip(errors,degrees,strict=True)],
          "compare each absolute error with the first omitted term")
    check("sin_point_one_linear", {"x":.1,"digits":6}, [0.1,0.099833,0.000167],
          [float(sin_polynomial(F(1,10),1)),round(sin(.1),6),round(.1-sin(.1),6)], "compare to the tangent")
    value = exp_polynomial(F(1,2),3)
    check("exponential_coefficients", {"x":"1/2","degree":3}, ["79/48",True],
          [str(value),0<exp(.5)-float(value)<.005], "evaluate the first four exponential terms")
    center,offset = 3.,.25
    exact = (center+offset)**2
    first = center**2+2*center*offset
    second = first+offset**2
    check("linear_and_quadratic_at_center", [center,offset], [10.5,10.5625,10.5625],
          [first,second,exact], "compare degree one, degree two and the true quadratic")

    hessian = np.array([[4.,0.],[0.,2.]])
    point = np.array([2.,-1.])
    minimum = np.array([1.,1.])
    gradient = hessian@(point-minimum)
    newton = point + np.linalg.solve(hessian,-gradient)
    descent = point - .125*gradient
    check("positive_definite_hessian_step", {"point":point.tolist(),"H":hessian.tolist(),"eta":.125},
          [[1.,1.],[1.5,-.5]], [newton.tolist(),descent.tolist()], "solve for the Newton step and compare a gradient step")
    flat = lambda u: exp(-1/u**2)
    check("smooth_flat_function_nonzero_away", .5, True, flat(.5)>0, "evaluate the flat-function example away from the center")

    gradient_sum, curvature_sum, penalty = -3.,5.,1.
    weight = -gradient_sum/(curvature_sum+penalty)
    objective = lambda w: gradient_sum*w+(curvature_sum+penalty)*w*w/2
    check("regularized_tree_leaf_weight", {"G":gradient_sum,"H":curvature_sum,"lambda":penalty},
          [.5,-.75,True], [weight,objective(weight),objective(weight)<min(objective(weight-.25),objective(weight+.25))],
          "minimize a scalar second-order tree-leaf objective")
    print(json.dumps(dict(version=platform.python_version(),library_versions={"numpy":np.__version__},cases=cases)))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • sin(0.5) 的 1、3、5 次式误差依次减小。正定二次目标的牛顿步准确到达极小点。
核对近似误差、牛顿步与叶子权重

所需环境:Windows、Python 3.12.14。numpy 2.5.4。

当前源码 SHA256:55dc7e3d55c8ff6687bb5d9d28934c903774c600496d3a7d0de62be19bda87e2

下载本站示例源码

复跑实验

解压后按 README 运行自编程序。

下载泰勒展开实验

所需环境:Windows、Python 3.12.14。numpy 2.5.4。

ZIP SHA256:1f11e4cffae88df1738ef7eb87fa0e4457a22ca9fdfe2b64ba7a9048abb3203d

常见错误与边界

  • 固定展开中心和角度单位。
  • 先检查二阶模型的曲率再求步长。

练习与解答

为何 sin 的五次式只需要三个非零项?

展开参考解答

零点处偶次项系数为零,保留次数 1、3、5。

规范与官方依据