Skip to Content
设计机器学习系统
book

设计机器学习系统

by Chip Huyen
May 2025
Beginner to intermediate
388 pages
4h 42m
Chinese
O'Reilly Media, Inc.
Content preview from 设计机器学习系统

第 7 章 模型部署和预测服务 模型部署和预测服务

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

第 4章至第 6 章中,我们讨论了开发 ML 模型的注意事项,从创建训练数据、提取特征、开发模型到制定评估模型的指标。如图 7-1 所示,这些考虑因素构成了模型的逻辑--即如何从原始数据转化为 ML 模型的指令。开发这一逻辑既需要 ML 知识,也需要学科专业知识。在许多公司,这是流程的一部分,由 ML 或数据科学团队完成。

图 7-1. 构成 ML 模型逻辑的不同方面

在本章中,我们将讨论迭代过程的另一个部分:部署模型。"部署 "是一个宽泛的术语,一般指使模型运行并可访问。在模型开发过程中,模型通常在开发环境中运行。1要部署模型,就必须离开开发环境。模型可以部署到暂存环境中进行测试,也可以部署到生产环境中供最终用户使用。在本章中,我们将重点讨论将模型部署到生产环境的问题。

在我们继续讨论之前,我想强调,生产是一个范围。对于某些团队来说,生产意味着在笔记本中生成漂亮的图表,向业务团队展示。而对于其他团队来说,生产意味着让你的模型每天为数百万用户正常运行。如果您的工作属于第一种情况,那么您的生产环境与开发环境类似,本章与您的关系不大。如果您的工作更接近第二种情况,请继续阅读。

我曾在网上看到过这样一句话:如果你忽略了所有困难的部分,部署工作就会变得很简单。如果你想部署一个模型给你的朋友们玩,你所要做的就是用 Flask 或 FastAPI 将你的预测函数封装在一个 POST 请求端点中,将这个预测函数运行所需的依赖关系放在一个容器中、2并将您的模型及其相关容器推送到 AWS 或 GCP 等云服务,以公开该端点:

# Example of how to use FastAPI to turn your predict function 
# into a POST endpoint
@app.route('/predict', methods=['POST'])
def predict():
    X = request.get_json()['X']
    y = MODEL.predict(X).tolist()
    return json.dumps({'y': y}), 200

您可以在下游应用程序中使用该暴露端点:例如,当应用程序收到用户的预测请求时,该请求会被发送到暴露端点,而暴露端点会返回预测结果。如果你熟悉必要的工具,你可以在一小时内完成功能部署。我的学生经过 10 周的课程学习后,都能部署一个 ML 应用程序作为他们的最终项目,尽管很少有人有过部署经验。3

困难的部分包括:以毫秒级的延迟和 99% 的正常运行时间向数百万用户提供模型;设置基础架构,以便在 出现问题时立即通知正确的人员;找出问题所在;以及无缝部署更新以修复问题。

在许多公司,部署模型的责任由开发这些模型的人承担。在其他许多公司,一旦模型准备好部署,就会将其导出并移交给另一个团队进行部署。然而,这种职责分离会造成跨团队沟通的高开销,并使模型更新缓慢。如果出现问题,也很难进行调试。我们将在第 11 章详细讨论团队结构。

备注

导出模型是指将模型转换成另一个应用程序可以使用的格式。有人将 这一过程称为 "序列化"。4您可以导出模型的两个部分:模型定义和模型参数值。模型定义定义了模型的结构,例如有多少个隐藏层,每层有多少个单元。参数值提供了这些单元和层的值。通常,这两部分是一起输出的。 ...

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

生成式AI在软件开发中的应用

生成式AI在软件开发中的应用

Sergio Pereira
Python机器学习基础教程

Python机器学习基础教程

Andreas C. Müller, Sarah Guido
低代码AI

低代码AI

Gwendolyn Stripling, Michael Abel

Publisher Resources

ISBN: 9798341656659