当前位置: 首页 > 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加速计算,并在多设备和多服务器上进行分布式训练。这些技能将帮助读者在实际项目中高效地部署和管理机器学习模型。

http://www.xdnf.cn/news/364033.html

相关文章:

  • RPG11.创建玩家Ability类
  • 基于CNN的猫狗图像分类系统
  • 推荐系统(二十五):基于阿里DIN(Deep Interest Network)的CTR模型实现
  • SpringCloud的作用
  • Java高频基础面试题
  • EMC|AC/DC转换器ESD静电防护
  • The 2024 ICPC Kunming Invitational Contest G. Be Positive
  • 【Python-Day 11】列表入门:Python 中最灵活的数据容器 (创建、索引、切片)
  • 【Spring】手动创建Spring|Boot项目
  • 【Golang】gin框架动态更新路由
  • C++--NULL和nullptr的区别
  • ATH12K 驱动框架
  • ch09 题目参考思路
  • 不黑文化艺术学社首席艺术家孙溟㠭浅析“雪渔派”
  • AI赋能智能客服革新:R²AIN SUITE 如何破解医疗行业服务难题?
  • 哈希表扩容怎么处理新插入的值?Swift 是怎么做的?
  • 力扣-19.删除链表的倒数第N个结点
  • Nacos源码—Nacos配置中心实现分析
  • Mysql数据库进阶
  • LMMSE、MMSE和LS
  • vscode 配置doxygen注释和snippet
  • RT-Thread 深入系列 Part 1:RT-Thread 全景总览
  • 【赛元8523触摸按键开发调试】
  • 【vue3】vue3中封装懒加载指令
  • C++ Lambda表达式详解:匿名函数的艺术与现代编程实践
  • 数字经济时代下的消费行为变迁与经济学启示
  • 解决 Redis 缓存与数据库一致性问题的技术指南
  • 【Linux网络】Socket-TCP相关函数
  • 大模型提示词策略
  • 赋能智能交通:时空图卷积网络引领速度预测新变革