Skip to Content
基于scikit-learn和PyTorch的实践机器学习
book

基于scikit-learn和PyTorch的实践机器学习

by Aurélien Géron
October 2025
Intermediate to advanced
878 pages
12h 53m
Chinese
O'Reilly Media, Inc.
Content preview from 基于scikit-learn和PyTorch的实践机器学习

第 11 章 深度神经网络的训练 训练深度神经网络

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在 第 10 章中,你使用 PyTorch 构建、训练和微调了几个人工神经网络。但它们都是只有几个隐藏层的浅层网络。如果你需要解决一个复杂的问题,比如检测高分辨率图像中的数百种物体,该怎么办?你可能需要训练一个更深层次的 ANN,也许有几十甚至几百个层,每个层包含几百个神经元,由几十万个连接相连。训练深度神经网络并非轻而易举。以下是你可能会遇到的一些问题:

  • 在训练过程中,您可能会遇到梯度在向后流经 DNN 时越来越小或越来越大的问题。这两个问题都会导致低层很难训练。

  • 对于如此庞大的网络,你可能没有足够的训练数据,或者标注成本太高。

  • 训练可能会非常缓慢。

  • 一个拥有数百万个参数的模型可能会严重过拟合训练集,尤其是在训练实例不足或噪声过大的情况下。

在本章中,我们将逐一讨论这些问题,并介绍解决这些问题的各种技术。首先,我们将探讨梯度消失和梯度爆炸问题,以及一些最常用的解决方案,包括智能权重初始化、更好的激活函数、批规范、层规范和梯度剪切。接下来,我们将介绍迁移学习和无监督预训练,它们可以帮助你在标注数据很少的情况下处理复杂的任务。然后,我们将讨论各种优化器,它们可以大大加快大型模型的训练速度。我们还将讨论如何在训练过程中调整学习率,以加快训练速度并生成更好的模型。最后,我们将介绍几种常用的大型神经网络正则化技术:ℓ1和ℓ2正则化、丢弃、蒙特卡罗丢弃和最大正则化。

有了这些工具,你就能训练各种深度神经网络。欢迎来到DeepLearning!

消失/爆炸梯度问题

正如 在第 9 章中所讨论的,反向传播 算法第二阶段的工作原理是从输出层到输入层,沿途传播误差梯度。一旦算法计算出成本函数 与网络中每个参数相关的梯度,它就会利用这些梯度,通过梯度下降步骤更新每个参数。

遗憾的是,随着算法向下层推进,梯度往往会越来越小。因此,梯度下降更新会使下层的连接权重几乎保持不变,训练永远不会收敛到一个好的解决方案。这就是所谓的梯度消失问题。在某些情况下,相反的情况也会发生:梯度会越来越大,直到各层的权重更新大得离谱,算法出现偏离。这就是梯度爆炸 问题,最常出现在递归神经网络中(参见第 13 章)。更普遍的情况是,Deep Learning 神经网络存在梯度不稳定的问题 ;不同层的学习速度可能大相径庭。

很早以前,人们就从经验上观察到了这种不幸的行为,这也是深度神经网络在 2000 年代初大多被放弃的原因之一。当时还不清楚是什么原因导致梯度在训练 DNN 时如此不稳定,但泽维尔-格罗洛特(Xavier Glorot)和尤苏亚-本吉奥(Yoshua Bengio)在2010 年的一篇论文中揭示了一些问题。1作者发现了一些疑点,包括流行的 sigmoid(对数)激活 函数与当时最流行的权重初始化技术(即均值为 0、标准差为 1 的正态分布)的结合。简而言之,他们的研究表明,使用这种激活函数和初始化方案,每一层输出的方差都远远大于输入的方差。在网络中继续前进,每一层之后方差都会不断增大,直到顶层的激活函数达到饱和。实际上,这种饱和会因 sigmoid 函数的均值为 0.5 而不是 0 而变得更糟(双曲正切函数的均值为 0,在深度网络中的表现比 sigmoid 函数稍好)。

观察 sigmoid 激活函数(见图 11-1),可以发现当输入变大(负或正)时,函数在 0 或 1 处达到饱和,导数极其接近 ...

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

编写整洁的Python代码(第2版)

编写整洁的Python代码(第2版)

Posts & Telecom Press, Mariano Anaya
生成式人工智能可视化

生成式人工智能可视化

Priyanka Vergadia, Valliappa Lakshmanan
AI工程

AI工程

Chip Huyen

Publisher Resources

ISBN: 0642572270117