Skip to Content
《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版
book

《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

by Aurélien Géron
May 2025
Intermediate to advanced
864 pages
12h 32m
Chinese
O'Reilly Media, Inc.
Content preview from 《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

附录 B. 自动扩散

本附录解释了 TensorFlow 的自动区分(autodiff)功能如何工作,以及与其他解决方案的比较。

假设你定义了一个函数f(x,y) =x2y+y+ 2,你需要它的偏导数∂f/∂x和∂f/∂y,通常是为了执行梯度下降(或其他优化算法)。主要选项有手动微分、有限差分逼近、正向模式自动衍射和反向模式自动衍射。TensorFlow 实现了反向模式自动微分,但要理解它,最好先看看其他选项。因此,让我们从手动微分开始,逐一了解它们。

手动区分

计算导数的第一种方法是拿起笔和纸,利用微积分知识推导出相应的方程。对于刚刚定义的函数f(x,y) 来说,这并不难;你只需使用五条规则:

  • 常数的导数为 0。

  • λx的导数是λ(其中λ是常数)。

  • xλ的导数是λxλ - 1,因此x2的导数是2x。

  • 函数之和的导数就是这些函数的导数之和。

  • 函数λ次的导数是函数λ次的导数。

根据这些规则,可以推导出等式 B-1。

方程 B-1. f(x,y) 的偏导数
∂f ∂x = ∂(x 2 y) ∂x + ∂y ∂x + ∂2 ∂x = y ∂(x 2 ) ∂x + 0 + 0 = 2 x y ∂f ∂y = ∂(x 2 y) ∂y + ∂y ∂y + ∂2 ∂y = x 2 + 1 + 0 = x 2 + 1

对于更复杂的功能,这种方法可能会变得非常乏味,而且还有出错的风险。幸运的是,我们还有其他选择。现在让我们来看看有限差分近似法。

有限差分法

回想一下,函数h(x) 在某点x0的导数h′(x0) 就是函数在该点的斜率。更确切地说,导数的定义是,当x无限接近x0时,经过该点x0和函数上另一点x的直线斜率的极限(见公式 B-2)。

公式 B-2. 函数h(x) 在x0点的导数定义
h ' ( x 0 ) = 限制 x→x 0 h(x)-h(x 0 ) x-x 0 = 限制 ε→0 h(x 0 +ε)-h(x 0 ) ε

因此,如果我们想计算f(x,y) 在x= 3 和y= 4 时对x的偏导数,我们可以计算f(3 +ε, 4) -f(3, 4) 并将结果除以ε,使用一个很小的ε 值。下面的代码正是如此:

def f(x, y):
    return x**2*y + y + 2

def derivative(f, x, y, x_eps, y_eps):
    return (f(x + x_eps, y + y_eps) - f(x, y)) / (x_eps + y_eps)

df_dx = derivative(f, 3, 4, 0.00001, 0)
df_dy = derivative(f, 3, 4, 0, 0.00001)

遗憾的是,计算结果并不精确(对于更复杂的函数,情况会更糟)。正确的结果分别是 24 和 10,但我们得到的却是

>>> df_dx
24.000039999805264
>>> df_dy
10.000000000331966

请注意,要计算两个偏导数,我们至少要调用f() 三次(前面的代码中调用了四次,但可以优化)。如果有 1,000 个参数,我们至少需要调用 1,001 次f() 。在处理大型神经网络时,这使得有限差分近似的效率太低。

不过,这种方法的实现非常简单,是检查其他方法是否正确实现的好工具。例如,如果它与你手动导出的函数不一致,那么你的函数很可能存在错误。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
学习 API 风格

学习 API 风格

Lukasz Dynowski, Marcin Dulak
《高性能 Python》第二版

《高性能 Python》第二版

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341656598