* 拥有一个 Google Cloud Platform (GCP) 账号并创建一个项目。 * 启用 Vertex AI API。 * 配置好身份验证,例如使用服务账号。 * 安装 Google Cloud SDK (gcloud CLI)。
* 将您的训练数据上传到 Cloud Storage (GCS) 桶中。 * 确保数据格式正确,例如 CSV, JSONL, TFRecord 等。 * 考虑数据预处理和特征工程,可以使用 Dataflow 或其他工具。
Vertex AI 支持多种模型开发方式: * AutoML: 自动训练模型,无需编写代码。 适合快速原型设计和baseline模型。 * 自定义训练: 使用 TensorFlow, PyTorch, scikit-learn 等框架,编写自定义训练代码。 提供更大的灵活性。 * 预训练模型: 使用 Vertex AI Model Garden 中的预训练模型,进行微调或直接使用。
* 使用 Vertex AI Evaluation 服务评估模型的性能。 * 可以选择不同的评估指标,例如准确率、精确率、召回率、F1-score 等。 * 可以比较不同模型的性能,选择最佳模型。
* 将训练好的模型部署到 Vertex AI Endpoint。 * Endpoint 提供在线预测服务,可以通过 API 调用进行预测。 * 可以选择不同的 Endpoint 配置,例如机器类型、最小/最大节点数等。
* 使用 Vertex AI Prediction API 发送预测请求。 * API 请求需要包含输入数据,格式应与训练数据一致。 * API 响应包含模型的预测结果。
可以使用 gcloud CLI, Python SDK 或 REST API 发送预测请求。 例如,使用 gcloud CLI:
gcloud ai endpoints predict ENDPOINT_ID --region=REGION --json-request=INPUT_DATA
其中,ENDPOINT_ID 是 Endpoint 的 ID,REGION 是 Endpoint 所在的区域,INPUT_DATA 是包含输入数据的 JSON 文件。
* 使用 Vertex AI Model Monitoring 监控模型的性能和数据漂移。 * 可以设置告警,当模型性能下降或数据漂移超过阈值时,会收到通知。 * Model Monitoring 可以帮助您及时发现问题,并采取措施进行修复。
from google.cloud import aiplatform
# 初始化 Vertex AI 客户端
aiplatform.init(project="YOUR_PROJECT_ID", location="YOUR_REGION")
# 创建 CustomJob
job = aiplatform.CustomJob(
display_name="my-custom-job",
script_path="path/to/your/training_script.py",
container_uri="gcr.io/YOUR_PROJECT_ID/your_training_image:latest",
requirements=["tensorflow==2.8.0"],
project="YOUR_PROJECT_ID",
location="YOUR_REGION",
)
# 运行 CustomJob
job.run(
replica_count=1,
machine_type="n1-standard-4",
accelerator_type="NVIDIA_TESLA_T4",
accelerator_count=1,
)
# 获取模型 URI
model_uri = job.gca_resource.model_uri
# 部署模型
model = aiplatform.Model.upload(
display_name="my-model",
artifact_uri=model_uri,
serving_container_image_uri="gcr.io/cloud-aiplatform/prediction/tf2-cpu.2-8:latest",
)
endpoint = aiplatform.Endpoint.create(
display_name="my-endpoint",
)
model.deploy(
endpoint=endpoint,
traffic_split={"0": 100},
machine_type="n1-standard-4",
)
# 发送预测请求
instances = [
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
]
prediction = endpoint.predict(instances=instances)
print(prediction)
请替换 YOUR_PROJECT_ID 和 YOUR_REGION 为您的 GCP 项目 ID 和区域。
Vertex AI 提供了一整套工具和服务,帮助您轻松地开发、训练和部署机器学习模型。 无论是使用 AutoML 快速构建 baseline 模型,还是使用自定义训练获得更大的灵活性,Vertex AI 都能满足您的需求。 通过模型评估、部署和监控,您可以确保模型的性能和稳定性。 祝您使用愉快!😊