首先先回顾一下高中的通过定义求导 # 1.单变量求导 def f(x): return 3. * x ** 2 + 2. * x - 1 def approximate_derivative(f, x, eps=1e-3): return (f(x + eps) - f(x - eps)) / (2. * eps) print(approximate_derivative(f, 1.)) # 7.999999 # 2.求偏导 def g(x1, x2): return (x1 + 5) * (x2 ** 2) def approximate_gradient(g, x