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 进行实践机器学习》第三版

第 12 章 使用 TensorFlow自定义模型和训练

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

到目前为止,我们只使用了 TensorFlow 的高级 API Keras,但它已经让我们走得相当远:我们构建了各种神经网络架构,包括回归和分类网络、Wide & Deep 网络以及自归一化网络,并使用了各种技术,例如批量归一化、丢弃和学习率调度。事实上,你将遇到的 95% 的用例都不需要 Keras(和 tf.data;参见第 13 章)以外的其他工具。不过,现在是时候深入了解 TensorFlow,看看它的底层Python API 了。当你需要额外的控制来编写自定义损失函数、自定义度量、层、模型、初始化器、正则化器、权重约束等时,这将非常有用。您甚至可能需要完全控制训练循环本身;例如,对梯度应用特殊的变换或约束(不仅仅是剪切梯度),或对网络的不同部分使用多个优化器。我们将在本章中介绍所有这些情况,我们还将了解如何使用 TensorFlow 的自动图生成功能提升自定义模型和训练算法。不过,首先让我们快速浏览一下 TensorFlow。

快速浏览 TensorFlow

众所周知,TensorFlow 是一个功能强大的数值计算库,尤其适合大规模机器学习并对其进行了微调(但你也可以用它来做其他任何需要繁重计算的事情)。它是由 Google Brain 团队开发的,它为 Google 的许多大规模服务提供动力,如 Google Cloud Speech、Google Photos 和 Google Search。它于 2015 年 11 月开源,目前是业界使用最广泛的深度学习库:1无数项目使用 TensorFlow 完成各种机器学习任务,如图像分类、自然语言处理、推荐系统和时间序列预测。

那么,TensorFlow 能提供什么呢?总结如下

  • 其核心与 NumPy 非常相似,但支持 GPU。

  • 它支持分布式计算(跨多个设备和服务器)。

  • 它包含一种即时(JIT)编译器,可以优化计算速度和内存使用。它的工作原理是从 Python 函数中提取计算图,对其进行优化(例如,修剪未使用的节点),并高效运行(例如,自动并行运行独立的操作)。

  • 计算图可以导出为可移植格式,因此您可以在一个环境中(如在 Linux 上使用 Python)训练 TensorFlow 模型,然后在另一个环境中(如在 Android 设备上使用 Java)运行该模型。

  • 它实现了反向模式自动衍射(参见第 10 章和附录 B),并提供了一些出色的优化器,如 RMSProp 和 Nadam(参见第 11 章),因此您可以轻松最小化各种损失函数。

TensorFlow 在这些核心功能的基础上提供了更多的功能:最重要的当然是 Keras。2但它还有数据加载和预处理操作(tf.data、tf.io 等)、图像处理操作(tf.image)、信号处理操作(tf.signal)等(TensorFlow 的 Python API 概述见图 12-1)。

提示

我们将介绍 TensorFlow API 的许多软件包和功能,但不可能面面俱到,所以您确实应该花些时间浏览一下 API;您会发现它的内容相当丰富,文档也很完善。

在最底层,每个 TensorFlow 操作(简称操作)都是通过高效的 C++ 代码实现的。3许多操作有多种实现方式,称为内核:每个内核专用于特定的设备类型,如 CPU、GPU 甚至 TPU(张量处理单元

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