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 构建机器学习管道

第 6 章 模型培训 模型训练

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

现在,数据预处理步骤已经完成, ,数据已被转换成模型所需的格式,我们下一步的工作就是用新转换的数据训练模型。

正如我们在第 1 章中所讨论的,我们将不介绍选择模型架构的过程。我们假定,在您拿起本书之前,您已经有了一个单独的实验过程,并且您已经知道了您希望训练的模型类型。我们将在第 15 章讨论如何跟踪这一实验过程,因为它有助于为模型创建完整的审计跟踪。但是,我们并没有介绍理解模型训练过程所需的任何理论背景。如果你想了解这方面的更多信息,我们强烈推荐 O'Reilly 出版的《Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow》(第 2 版)。

在本章中,我们将介绍作为机器学习管道一部分的模型训练过程,包括如何在 TFX 管道中实现自动化。我们还包括 TensorFlow 中可用的分布策略以及如何在管道中调整超参数的一些细节。与大多数其他章节相比,本章更侧重于 TFX 管道,因为我们没有将训练作为一个独立的过程进行介绍。

如图 6-1 所示,此时数据已被摄取、验证和预处理。这确保了模型所需的所有数据都已存在,并且已被可重复地转换成模型所需的特征。所有这些都是必要的,因为我们不希望管道在下一步失败。我们希望确保训练顺利进行,因为这通常是整个管道中最耗时的部分。

Model Training as part of ML Pipelines
图 6-1. 作为 ML 管道一部分的模型训练

在 TFX 管道中训练模型的一个非常重要的特点是,我们在第 5 章中讨论的数据预处理步骤将与训练后的模型权重一起保存。一旦我们的模型部署到生产环境中,这一点将非常有用,因为这意味着预处理步骤将始终产生模型所期望的特征。如果没有这项功能,就有可能在不更新模型的情况下更新数据预处理步骤,这样模型就会在生产中失败,或者预测结果将基于错误的数据。由于我们将预处理步骤和模型导出为一张图,因此消除了这一潜在的错误来源。

在接下来的两节中,我们将详细介绍作为 TFX 管道的一部分训练tf.Keras 模型所需的步骤。1

确定示例项目的模式

尽管模型架构已经确定,但这里仍需要一些额外的代码。 我们需要实现管道中模型训练部分的自动化。在本节中,我们将简要介绍本章中使用的模型。

我们示例项目的模型只是一种假设实现,我们或许可以优化模型架构。不过,它展示了许多 Deep Learning 模型的一些常见成分:

  • 预训练模型的迁移学习

  • 致密层

  • 连接层

正如我们在第 1 章中所讨论的,我们示例项目中的模型使用美国消费者金融保护局的数据来预测消费者是否对金融产品的投诉提出异议。我们模型中的特征包括金融产品、公司回应、美国州以及消费者投诉叙述。我们的模型受Wide and Deep 模型架构的启发,增加了TensorFlow Hub通用句子编码器来编码自由文本特征(消费者投诉叙述)。

您可以在图 6-2 中看到我们的模型架构的可视化表示,文本特征 (narrative_xf) 采用 "深 "路径,其他特征采用 "广 "路径。

图 6-2. 示例项目的模型架构

例 6-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

What Successful Brick-and-Mortar Retailers Get Right

What Successful Brick-and-Mortar Retailers Get Right

Rob Angell
Search Marketing

Search Marketing

Kelly Cutler
What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer

Publisher Resources

ISBN: 9798341659292