当前位置: 首页 > news >正文

机器学习实操 第二部分 第19章 大规模训练和部署 TensorFlow 模型

机器学习实操 第二部分 第19章 大规模训练和部署 TensorFlow 模型

内容概要

第19章深入探讨了如何大规模训练和部署TensorFlow模型。章节涵盖了从模型训练到部署的全过程,包括使用TF Serving和Vertex AI进行模型服务、在移动和嵌入式设备上部署模型、利用GPU加速计算、以及在多设备和多服务器上进行分布式训练。此外,还讨论了如何在云平台上进行大规模训练和超参数调优。
在这里插入图片描述

主要内容

  1. 模型部署

    • TF Serving:一个高效的模型服务器,支持多版本模型自动部署和版本管理。
    • Vertex AI:Google Cloud Platform提供的AI服务,支持模型训练、部署和管理,提供自动扩展和监控工具。
  2. 移动和嵌入式设备部署

    • TFLite:将模型转换为轻量级格式,支持模型压缩和量化,适用于移动和嵌入式设备。
    • TensorFlow.js:在网页中直接运行模型,支持客户端预测和隐私保护。
  3. GPU加速

    • 使用GPU:通过TensorFlow自动检测和利用GPU加速计算。
    • 管理GPU资源:控制GPU内存分配和使用,避免资源冲突。
  4. 分布式训练

    • 数据并行和模型并行:利用多GPU和多服务器进行分布式训练,提高训练效率。
    • TensorFlow Distribution Strategies API:简化分布式训练的实现,支持多种策略如MirroredStrategy和MultiWorkerMirroredStrategy。
  5. 大规模训练和超参数调优

    • Vertex AI:支持大规模训练作业和超参数调优,提供自动化的模型部署和管理。
    • AutoML:自动化模型架构搜索和训练,适合快速开发和部署。

精彩语录

  1. 中文:使用TF Serving可以高效地部署模型,支持多版本自动部署和版本管理。
    英文原文:TF Serving is a very efficient, battle-tested model server, written in C++. It can sustain a high load, serve multiple versions of your models and watch a model repository to automatically deploy the latest versions.
    解释:强调了TF Serving在模型部署中的高效性和可靠性。

  2. 中文:通过Vertex AI,你可以轻松地在云平台上训练和部署模型,支持自动扩展和监控。
    英文原文:Vertex AI allows you to create custom training jobs with your own training code, and it takes care of provisioning and managing all the infrastructure for you.
    解释:介绍了Vertex AI在云平台上的优势,包括自动扩展和监控。

  3. 中文:TFLite通过模型压缩和量化,显著减少了模型大小和计算量,适用于移动和嵌入式设备。
    英文原文:TFLite’s model converter can take a SavedModel and compress it to a much lighter format based on FlatBuffers, reducing the model size and computation requirements.
    解释:说明了TFLite在移动和嵌入式设备上的应用优势。

关键代码

使用TF Serving部署模型

import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2# 导出模型为SavedModel格式
model.save("my_model", save_format="tf")# 安装和启动TF Serving
!pip install -q -U tensorflow-serving-api
!tensorflow_model_server --port=8500 --rest_api_port=8501 --model_name=my_model --model_base_path="./my_model"# 使用REST API查询TF Serving
import requests
import jsonserver_url = "http://localhost:8501/v1/models/my_model:predict"
X_new = [...]  # 新数据
request_json = json.dumps({"signature_name": "serving_default", "instances": X_new.tolist()})
response = requests.post(server_url, data=request_json)
response.raise_for_status()
y_proba = np.array(response.json()["predictions"])

使用Vertex AI进行分布式训练

from google.cloud import aiplatform# 初始化Vertex AI
aiplatform.init(project="my_project", location="us-central1")# 创建自定义训练作业
custom_training_job = aiplatform.CustomTrainingJob(display_name="my_custom_training_job",script_path="my_vertex_ai_training_task.py",container_uri="gcr.io/cloud-aiplatform/training/tf-gpu.2-4:latest",model_serving_container_image_uri="gcr.io/cloud-aiplatform/prediction/tf2-gpu.2-8:latest",staging_bucket="gs://my_bucket/staging"
)# 运行训练作业
mnist_model2 = custom_training_job.run(machine_type="n1-standard-4",replica_count=2,accelerator_type="NVIDIA_TESLA_K80",accelerator_count=2
)

使用TensorFlow Distribution Strategies API进行分布式训练

import tensorflow as tf# 使用MirroredStrategy进行数据并行训练
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():model = tf.keras.Sequential([...])  # 创建Keras模型model.compile([...])  # 编译模型# 训练模型
model.fit(X_train, y_train, validation_data=(X_valid, y_valid), epochs=10)

总结

通过本章的学习,读者将掌握如何将TensorFlow模型部署到生产环境,包括使用TF Serving和Vertex AI进行模型服务,以及在移动、嵌入式设备和网页中运行模型。此外,还将学习如何利用GPU加速计算,并在多设备和多服务器上进行分布式训练。这些技能将帮助读者在实际项目中高效地部署和管理机器学习模型。

相关文章:

  • 关闭 Windows Server 2012 R2 的关机原因提示功能,从而实现直接执行关机或重启操作
  • 二维旋转矩阵:让图形动起来的数学魔法 ✨
  • QT 解决msvc fatal error C1060: 编译器的堆空间不足
  • js 两个数组中的指定参数(id)相同,为某个对象设置disabled属性
  • ROS导航局部路径规划算法
  • “点对点通信(Point-to-Point)”和“端对端通信(End-to-End)”
  • LearnOpenGL01:创建项目
  • 从逻辑学视角理解统计学在数据挖掘中的作用
  • rust-candle学习笔记10-使用Embedding
  • QT6(35)4.8定时器QTimer 与QElapsedTimer:理论,例题的界面搭建,与功能的代码实现。
  • 请求从发送到页面渲染的全过程
  • vscode 配置doxygen注释和snippet
  • 大模型备案环节如何评估模型的安全性
  • 简易版无人机飞控
  • C++ Dll创建与调用 查看dll函数 MFC 单对话框应用程序(EXE 工程)改为 DLL 工程
  • Spring Boot快速开发:从零开始搭建一个企业级应用
  • 《工业计算机硬件技术支持手册》适用于哪些人群?
  • STM32GPIO输入实战-key按键easy_button库移植
  • ES6新增Set、Map两种数据结构、WeakMap、WeakSet举例说明详细。(含DeepSeek讲解)
  • Qt开发经验 --- 避坑指南(10)
  • 会计江湖|年报披露关注什么:独董给出的“信号”
  • 夜读丨喜马拉雅山的背夫
  • 方正证券总裁何亚刚到龄退休,54岁副总裁姜志军接棒
  • 浙江一民企拍地后遭政府两次违约,“民告官”三年又提起民事诉讼
  • 异域拾异|大脚怪的形状:一项神秘社会学研究
  • 以军总参谋长:已进入“决定性打击计划的第二阶段”