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

第 13 章 使用 TensorFlow 加载和预处理数据 使用 TensorFlow加载和预处理数据

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

在第 2 章中,我们了解到加载和预处理数据是任何机器学习项目的重要组成部分。你使用 Pandas 加载并探索了(修改后的)加州住房数据集(该数据集存储在 CSV 文件中),并使用 Scikit-Learn 的转换器进行了预处理。这些工具非常方便,你可能会经常使用,尤其是在探索和实验数据时。

不过,在大型数据集上训练 TensorFlow 模型时,您可能更愿意使用 TensorFlow 自带的数据加载和预处理 API(称为tf.data)。它能够极其高效地加载和预处理数据,使用多线程和队列从多个文件并行读取数据,对样本进行洗牌和批处理,等等。此外,它还能快速完成所有这些工作--当 GPU 或 TPU 忙于训练当前批次的数据时,它能通过多个 CPU 内核加载和预处理下一批数据。

通过 tf.data API,您可以处理内存无法容纳的数据集,并充分利用硬件资源,从而加快训练速度。tf.data API 可以读取文本文件(如 CSV 文件)、具有固定大小记录的二进制文件,以及使用 TensorFlow 的 TFRecord 格式(支持不同大小的记录)的二进制文件。

TFRecord 是一种灵活高效的二进制格式,通常包含协议缓冲区(一种开源二进制格式)。tf.data API 还支持从 SQL 数据库读取数据。此外,还有许多开源扩展可用于从各种数据源读取数据,例如 Google 的 BigQuery 服务(参见https://tensorflow.org/io)。

Keras 还提供功能强大且易于使用的预处理层,可嵌入模型中:这样,当您将模型部署到生产中时,它就能直接摄取原始数据,而无需添加任何额外的预处理代码。这就消除了训练过程中使用的预处理代码与生产过程中使用的预处理代码不匹配的风险,而这种不匹配很可能会导致训练/服务偏差。而且,如果您在多个使用不同编程语言编码的应用程序中部署模型,就不必多次重新实施相同的预处理代码,这也降低了不匹配的风险。

正如您将看到的,这两个 API 可以联合使用--例如,可以受益于 tf.data 提供的高效数据加载和 Keras 预处理层带来的便利。

在本章中,我们将首先介绍 tf.data API 和 TFRecord 格式。然后,我们将探讨 Keras 预处理层以及如何将它们与 tf.data API 配合使用。最后,我们将快速浏览一些相关库,您可能会发现它们对加载和预处理数据非常有用,例如 TensorFlow Datasets 和 TensorFlow Hub。那么,让我们开始吧!

tf.data API

整个 tf.data 应用程序接口都围绕着tf.data.Dataset 这一概念:它代表了一连串的数据项。通常,您会使用从磁盘逐步读取数据的数据集,但为了简单起见,让我们使用tf.data.Dataset.from_tensor_slices() 从一个简单的数据张量创建一个数据集:

>>> import tensorflow as tf
>>> X = tf.range(10)  # any data tensor
>>> dataset = tf.data.Dataset.from_tensor_slices(X)
>>> dataset
<TensorSliceDataset shapes: ...
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