机器学习实操 第二部分 第19章 大规模训练和部署 TensorFlow 模型
机器学习实操 第二部分 第19章 大规模训练和部署 TensorFlow 模型
内容概要
第19章深入探讨了如何大规模训练和部署TensorFlow模型。章节涵盖了从模型训练到部署的全过程,包括使用TF Serving和Vertex AI进行模型服务、在移动和嵌入式设备上部署模型、利用GPU加速计算、以及在多设备和多服务器上进行分布式训练。此外,还讨论了如何在云平台上进行大规模训练和超参数调优。
主要内容
-
模型部署
- TF Serving:一个高效的模型服务器,支持多版本模型自动部署和版本管理。
- Vertex AI:Google Cloud Platform提供的AI服务,支持模型训练、部署和管理,提供自动扩展和监控工具。
-
移动和嵌入式设备部署
- TFLite:将模型转换为轻量级格式,支持模型压缩和量化,适用于移动和嵌入式设备。
- TensorFlow.js:在网页中直接运行模型,支持客户端预测和隐私保护。
-
GPU加速
- 使用GPU:通过TensorFlow自动检测和利用GPU加速计算。
- 管理GPU资源:控制GPU内存分配和使用,避免资源冲突。
-
分布式训练
- 数据并行和模型并行:利用多GPU和多服务器进行分布式训练,提高训练效率。
- TensorFlow Distribution Strategies API:简化分布式训练的实现,支持多种策略如MirroredStrategy和MultiWorkerMirroredStrategy。
-
大规模训练和超参数调优
- Vertex AI:支持大规模训练作业和超参数调优,提供自动化的模型部署和管理。
- AutoML:自动化模型架构搜索和训练,适合快速开发和部署。
精彩语录
-
中文:使用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在模型部署中的高效性和可靠性。 -
中文:通过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在云平台上的优势,包括自动扩展和监控。 -
中文: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加速计算,并在多设备和多服务器上进行分布式训练。这些技能将帮助读者在实际项目中高效地部署和管理机器学习模型。