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的实践机器学习

第 10 章 用 PyTorch 构建神经网络 用 PyTorch 构建神经网络

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

PyTorch 是一个功能强大的开源深度学习库,由 Facebook 的人工智能研究实验室(FAIR,现称为 Meta AI)开发。它是 Torch 库的 Python 后继库,最初是用 Lua 编程语言编写的。利用 PyTorch,你可以构建各种神经网络模型,并使用 GPU(或其他硬件加速器,我们将看到)对其进行大规模训练。PyTorch 在很多方面都与 NumPy 相似,只不过它也支持硬件加速和自动衍射(参见第 9 章),并包含优化器和现成的神经网络组件。

当 PyTorch 于 2016 年发布时,谷歌的 TensorFlow 库是迄今为止最受欢迎的:它速度快、扩展性好,而且可以在许多平台上部署。但它的编程模型复杂且静态,因此难以使用和调试。相比之下,PyTorch 从一开始就是为了提供一种更灵活的 Python 方法来构建神经网络而设计的。尤其是,正如你将看到的,它使用动态计算图(也称为 "按运行定义"),使其直观且易于调试。PyTorch 的编码和文档也非常精美,它专注于自己的核心任务:轻松构建和训练高性能神经网络。最后但并非最不重要的一点是,PyTorch 非常重视开源文化,并受益于热情、敬业的社区和丰富的生态系统。2022 年 9 月,PyTorch 的管理权甚至移交给了 Linux 基金会下属的 PyTorch 基金会。所有这些特质都引起了研究人员的共鸣:PyTorch 很快成为学术界使用最多的框架,而一旦大部分深度学习论文都基于 PyTorch,很大一部分业界人士也会逐渐转化。1

在本章中,你将学习如何使用 PyTorch 训练、评估、微调、优化和保存神经网络。我们将首先熟悉 PyTorch 的核心构建模块,即张量和 autograd,接下来我们将通过构建和训练一个简单的线性回归模型来试水,然后我们将把这个模型升级为多层神经网络,首先用于回归,然后用于分类。在此过程中,我们将了解如何构建具有多个输入或输出的自定义神经网络。最后,我们将讨论如何使用 Optuna 库自动微调超参数,以及如何优化和导出模型。加入我们吧,我们正在深入学习 Deep Learning!

注意事项

Colab 运行时预装了最新版本的 PyTorch。不过,如果你想在自己的机器上安装,请参阅https://homl.info/install-p 上的安装说明:这涉及到安装 Python、许多库和 GPU 驱动程序(如果你有的话)。

PyTorch 基本原理

PyTorch 的核心数据结构是张量。2它是一个有形状和数据类型的多维数组,用于数值计算。这不是和 NumPy 数组 完全一样吗?是的,没错!但张量还有两个额外的特点:它可以在 GPU(或其他硬件加速器,我们会看到)上运行,并且支持自动区分。从现在起,我们构建的每个神经网络都将输入和输出张量(就像 Scikit-Learn 对 NumPy 数组的输入和输出建模一样)。因此,我们先来看看如何创建和操作张量。

PyTorch 张量

首先,让我们导入 PyTorch 库:

>>> import torch

接下来,你可以像创建 NumPy 数组一样创建 PyTorch 张量。例如,让我们创建一个 2 × 3 数组:

>>> X = torch.tensor([[1.0, 4.0, 7.0], [2.0, 3.0 ...
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