Skip to Content
构建机器学习管道
book

构建机器学习管道

by Hannes Hapke, Catherine Nelson
May 2025
Intermediate to advanced
366 pages
4h 36m
Chinese
O'Reilly Media, Inc.
Content preview from 构建机器学习管道

第 5 章 数据预处理 数据预处理

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

我们用来训练机器学习模型的数据通常是以机器学习模型无法使用的格式提供的。例如,在我们的示例项目中,我们想用来训练模型的一个特征只有 "是 "和 ""两个标签。任何机器学习模型都需要这些值的数字表示(如10)。在本章中,我们将介绍如何将特征转换为一致的数值表示,以便使用特征的数值表示来训练机器学习模型。

本章讨论的一个主要方面是关注一致的预处理。如图 5-1 所示,预处理是在数据验证之后进行的,我们将在第 4 章讨论这一点。用于数据预处理的 TFX 组件 TensorFlow Transform(TFT)允许我们将预处理步骤构建为 TensorFlow 图。在接下来的章节中,我们将讨论为什么以及何时这样做才是好的工作流程,以及如何导出预处理步骤。在第 6 章中,我们将使用预处理数据集和保留的转换图分别训练和导出我们的机器学习模型。

Data Preprocessing as part of ML Pipelines
图 5-1. 作为 ML 管道一部分的数据预处理

数据科学家可能会认为用 TensorFlow 运算(操作)表示的预处理步骤开销太大。毕竟,它需要的实现方式与您用 Python 的pandasnumpy 编写预处理步骤时可能习惯的实现方式不同。我们并不提倡在实验阶段使用 TFT。不过,正如我们在以下章节中演示的那样,当您将机器学习模型带入生产环境时,将您的预处理步骤转换为 TensorFlow 操作将有助于避免我们在第 4 章中讨论的为训练服务的偏差。

为什么要进行数据预处理?

根据我们的经验,在所有 TFX 库中,TFT 的学习曲线最为陡峭,因为需要通过 TensorFlow 操作来表达预处理步骤。 不过,在使用 TFT 的机器学习流水线中,数据预处理标准化有很多很好的理由,其中包括: 1:

  • 根据整个数据集有效预处理数据

  • 有效扩展预处理步骤

  • 避免潜在的培训服务偏差

在整个数据集的背景下预处理数据

当我们想要将数据转换为数值表示时,通常必须在整个数据集的背景下进行。 例如,如果我们要对数值特征进行归一化处理,首先必须确定训练集中特征的最小值和最大值。有了确定的边界,我们就可以将数据归一化为介于 0 和 1 之间的值。这一归一化步骤通常需要对数据进行两次处理:一次用于确定边界,一次用于转换每个特征值。TFT 提供了在幕后管理数据的函数。

扩展预处理步骤

TFT 在引擎盖下使用 Apache Beam 执行预处理指令。 这样,如果需要,我们就可以在自己选择的 Apache Beam 后端分发预处理。如果您无法访问 Google Cloud 的 Dataflow 产品或 Apache Spark 或 Apache Flink 集群,Apache Beam 将默认返回其 Direct Runner 模式。

避免培训服务偏差

TFT 会创建并保存预处理步骤的 TensorFlow 图形。 首先,它将创建一个图形来处理数据(例如,确定最小值/最大值)。然后,它将保存已确定边界的图形。然后,在模型生命周期的推理阶段可以使用该图形。这一过程保证了推理生命周期步骤中的模型与训练过程中使用的模型具有相同的预处理步骤。

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

The Three Traps That Stymie Reinvention

The Three Traps That Stymie Reinvention

Ryan Raffaelli
The Human Factor in AI-Based Decision-Making

The Human Factor in AI-Based Decision-Making

Philip Meissner, Christoph Keding
What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
Coaching for High Performance

Coaching for High Performance

MIT Sloan Management Review

Publisher Resources

ISBN: 9798341659292