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

第 8 章 使用TensorFlow 服务部署模型

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

机器学习模型的部署是其他人使用模型进行预测的最后一步。 遗憾的是,在当今数字世界的分工思维中,机器学习模型的部署属于灰色地带。这不仅仅是 DevOps 的任务,因为它需要对模型架构及其硬件要求有一定的了解。同时,部署机器学习模型也有点超出机器学习工程师和数据科学家的舒适范围。他们对自己的模型了如指掌,但在部署机器学习模型时却往往举步维艰。在本章和下一章中,我们希望弥合两个世界之间的差距,指导数据科学家和 DevOps 工程师完成部署机器学习模型的步骤。图 8-1显示了部署步骤在机器学习管道中的位置。

Model Deployments as part of ML Pipelines
图 8-1. 作为 ML 管道一部分的模型部署

机器学习模型可以通过三种主要方式部署:模型服务器、用户浏览器或边缘设备。目前部署机器学习模型最常见的方式是使用模型服务器,我们将在本章重点介绍这种方式。请求预测的客户端将输入数据提交给模型服务器,然后收到预测结果。这就要求客户端能与模型服务器连接。

在某些情况下,您不想将输入数据提交给模型服务器(例如,当输入数据比较敏感或存在隐私问题时)。 在这种情况下,可以将机器学习模型部署到用户的浏览器上。例如,如果要确定图像是否包含敏感信息,可以在图像上传到 Cloud 服务器之前对其敏感程度进行分类。

不过,还有第三种模型部署方式:部署到边缘设备。在某些情况下,您无法连接到模型服务器进行预测(如远程传感器或物联网设备)。部署到边缘设备上的应用越来越多,因此这也是模型部署的一种有效选择。在第 10 章中,我们将讨论如何将 TensorFlow 模型转换为 TFLite 模型,后者可以在边缘设备上执行。

在本章中,我们将重点介绍 TensorFlow 的服务模块,这是一种通过模型服务器部署 TensorFlow 模型的简单而一致的方式。我们将介绍其设置并讨论高效的部署选项。这并不是部署 Deep Learning 模型的唯一方法;还有一些替代选项,我们将在本章结尾进行讨论。

在深入学习 TensorFlow 服务之前,让我们先来看看本章开始时不应该如何设置模型服务器。

简单的模型服务器

大多数关于部署机器学习模型的介绍都遵循大致相同的工作流程:

  • 使用 Python 创建网络应用程序(即使用 Flask 或 Django 等网络框架)。

  • 在网络应用程序中创建一个 API 端点,如例 8-1 所示。

  • 加载模型结构及其权重。

  • 在加载的模型上调用预测方法。

  • 以 HTTP 请求的形式返回预测结果。

例 8-1. 设置 Flask 端点以推断模型预测的示例
import json
from flask import Flask, request
from tensorflow.keras.models import load_model
from utils import preprocess 1

model = load_model
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