本页面介绍如何创建和管理 AML AI 模型。 创建模型以启动 AML AI 的机器学习训练流水线。您可以使用模型针对各种数据集运行预测或回测。
您只需在此阶段创建模型并导出相关元数据。为了完整性,我们还提供了其他模型方法。
准备工作
-
如需获得创建和管理引擎模型所需的权限,请让您的管理员为您授予项目的 Financial Services Admin (
financialservices.admin) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。 - 创建实例
- 创建引擎配置
- 创建数据集
创建模型
某些 API 方法会返回长时间运行的操作 (LRO)。这些方法是异步执行的,并会返回一个 Operation 对象;如需了解详情,请参阅 REST 参考。当方法返回响应时,操作可能尚未完成。对于这些方法,请发送请求,然后检查结果。一般来说,所有 POST、PUT、UPDATE 和 DELETE 操作都是长时间运行的操作。
发送请求
如需创建模型,请使用 projects.locations.instances.models.create 方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:IAM 设置中列出的 Google Cloud 项目 IDLOCATION:实例的位置;请使用其中一个受支持的区域显示位置us-central1us-east1asia-south1europe-west1europe-west2europe-west4northamerica-northeast1southamerica-east1australia-southeast1
INSTANCE_ID:用户定义的实例标识符MODEL_ID:用户定义的模型标识符ENGINE_CONFIG_ID:用户定义的引擎配置标识符DATASET_ID:用于训练相应模型的主数据集的用户定义标识符TRAINING_END_DATE:用于生成训练特征的数据的最晚时间。该日期应与主数据集的结束时间相同或早于该时间。采用 RFC3339 UTC(即“祖鲁时”)格式(例如2014-10-02T15:01:23Z)。
请求 JSON 正文:
{
"engineConfig": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/engineConfigs/ENGINE_CONFIG_ID",
"primaryDataset": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/datasets/DATASET_ID",
"endTime": "TRAINING_END_DATE"
}
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF'
{
"engineConfig": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/engineConfigs/ENGINE_CONFIG_ID",
"primaryDataset": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/datasets/DATASET_ID",
"endTime": "TRAINING_END_DATE"
}
EOF然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models?model_id=MODEL_ID"
PowerShell
将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@'
{
"engineConfig": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/engineConfigs/ENGINE_CONFIG_ID",
"primaryDataset": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/datasets/DATASET_ID",
"endTime": "TRAINING_END_DATE"
}
'@ | Out-File -FilePath request.json -Encoding utf8然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models?model_id=MODEL_ID" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.financialservices.v1.OperationMetadata",
"createTime": "2023-03-14T15:52:55.358979323Z",
"target": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID",
"verb": "create",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
检查结果
使用 projects.locations.operations.get 方法检查模型是否已创建。如果响应包含 "done": false,请重复执行该命令,直到响应包含 "done": true。这些操作可能需要几分钟到几小时才能完成。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:IAM 设置中列出的 Google Cloud 项目 IDLOCATION:实例的位置;请使用其中一个受支持的区域显示位置us-central1us-east1asia-south1europe-west1europe-west2europe-west4northamerica-northeast1southamerica-east1australia-southeast1
OPERATION_ID:操作的标识符
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.financialservices.v1.OperationMetadata",
"createTime": "2023-03-14T15:52:55.358979323Z",
"endTime": "2023-03-14T16:52:55.358979323Z",
"target": "projects/PROJECT_ID/locations/LOCATION/models/MODEL_ID",
"verb": "create",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": true,
"response": {
"@type": "type.googleapis.com/google.cloud.financialservices.v1.Model",
"name": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID",
"createTime": CREATE_TIME,
"updateTime": UPDATE_TIME,
"state": "ACTIVE",
"engineVersion": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/engineVersions/ENGINE_VERSION_ID",
"engineConfig": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/engineConfigs/ENGINE_CONFIG_ID",
"primaryDataset": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/datasets/DATASET_ID",
"endTime": "TRAINING_END_DATE",
"lineOfBusiness": "RETAIL"
}
}
导出元数据
如需从模型中导出元数据,请使用 projects.locations.instances.models.exportMetadata 方法。
如需了解详情,请参阅 AML 输出数据模型中的导出元数据。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:IAM 设置中列出的 Google Cloud 项目 IDLOCATION:实例的位置;请使用其中一个受支持的区域显示位置us-central1us-east1asia-south1europe-west1europe-west2europe-west4northamerica-northeast1southamerica-east1australia-southeast1
INSTANCE_ID:用户定义的实例标识符MODEL_ID:用户定义的模型标识符BQ_OUTPUT_DATASET_NAME:用于导出表的 BigQuery 数据集,该表描述了模型的结构化元数据STRUCTURED_METADATA_TABLE:用于写入结构化元数据的表WRITE_DISPOSITION:如果目标表已存在,则执行的操作;请使用以下某个值:-
WRITE_EMPTY:仅当 BigQuery 表为空时才导出数据。 -
WRITE_TRUNCATE:在写入 BigQuery 表之前,先清空该表中的所有现有数据。
-
请求 JSON 正文:
{
"structuredMetadataDestination": {
"tableUri": "bq://PROJECT_ID.BQ_OUTPUT_DATASET_NAME.STRUCTURED_METADATA_TABLE",
"writeDisposition": "WRITE_DISPOSITION"
}
}
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF'
{
"structuredMetadataDestination": {
"tableUri": "bq://PROJECT_ID.BQ_OUTPUT_DATASET_NAME.STRUCTURED_METADATA_TABLE",
"writeDisposition": "WRITE_DISPOSITION"
}
}
EOF然后,执行以下命令以发送 REST 请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID:exportMetadata"
PowerShell
将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@'
{
"structuredMetadataDestination": {
"tableUri": "bq://PROJECT_ID.BQ_OUTPUT_DATASET_NAME.STRUCTURED_METADATA_TABLE",
"writeDisposition": "WRITE_DISPOSITION"
}
}
'@ | Out-File -FilePath request.json -Encoding utf8然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID:exportMetadata" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.financialservices.v1.OperationMetadata",
"createTime": "2023-03-14T15:52:55.358979323Z",
"target": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID",
"verb": "exportMetadata",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
如需详细了解如何获取长时间运行的操作 (LRO) 的结果,请参阅检查结果。
获取模型
如需获取模型,请使用 projects.locations.instances.models.get 方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:IAM 设置中列出的 Google Cloud 项目 IDLOCATION:实例的位置;请使用其中一个受支持的区域显示位置us-central1us-east1asia-south1europe-west1europe-west2europe-west4northamerica-northeast1southamerica-east1australia-southeast1
INSTANCE_ID:用户定义的实例标识符MODEL_ID:用户定义的模型标识符
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID",
"createTime": "2023-03-14T15:52:55.358979323Z",
"updateTime": "2023-03-15T15:52:55.358979323Z",
"state": "ACTIVE",
"engineVersion": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/engineVersions/ENGINE_VERSION_ID",
"engineConfig": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/engineConfigs/ENGINE_CONFIG_ID",
"primaryDataset": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/datasets/DATASET_ID",
"endTime": "TRAINING_END_DATE",
"lineOfBusiness": "RETAIL"
}
更新模型
如需更新模型,请使用 projects.locations.instances.models.patch 方法。
只能更新模型中的标签字段。以下示例会更新与模型关联的这些键值对用户标签。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:IAM 设置中列出的 Google Cloud 项目 IDLOCATION:实例的位置;请使用其中一个受支持的区域显示位置us-central1us-east1asia-south1europe-west1europe-west2europe-west4northamerica-northeast1southamerica-east1australia-southeast1
INSTANCE_ID:用户定义的实例标识符MODEL_ID:用户定义的模型标识符KEY:用于组织模型的键值对中的键。如需了解详情,请参阅labels。VALUE:用于组织模型的键值对中的值。如需了解详情,请参阅labels。
请求 JSON 正文:
{
"labels": {
"KEY": "VALUE"
}
}
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
cat > request.json << 'EOF'
{
"labels": {
"KEY": "VALUE"
}
}
EOF然后,执行以下命令以发送 REST 请求:
curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID?updateMask=labels"
PowerShell
将请求正文保存在名为 request.json 的文件中。在终端中运行以下命令,在当前目录中创建或覆盖此文件:
@'
{
"labels": {
"KEY": "VALUE"
}
}
'@ | Out-File -FilePath request.json -Encoding utf8然后,执行以下命令以发送 REST 请求:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID?updateMask=labels" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.financialservices.v1.OperationMetadata",
"createTime": "2023-03-14T15:52:55.358979323Z",
"target": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID",
"verb": "update",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
如需详细了解如何获取长时间运行的操作 (LRO) 的结果,请参阅检查结果。
列出模型
如需列出指定实例的模型,请使用 projects.locations.instances.models.list 方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:IAM 设置中列出的 Google Cloud 项目 IDLOCATION:实例的位置;请使用其中一个受支持的区域显示位置us-central1us-east1asia-south1europe-west1europe-west2europe-west4northamerica-northeast1southamerica-east1australia-southeast1
INSTANCE_ID:用户定义的实例标识符
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"models": [
{
"name": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID",
"createTime": "2023-03-14T15:52:55.358979323Z",
"updateTime": "2023-03-15T15:52:55.358979323Z",
"state": "ACTIVE",
"engineVersion": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/engineVersions/ENGINE_VERSION_ID",
"engineConfig": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/engineConfigs/ENGINE_CONFIG_ID",
"primaryDataset": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/datasets/DATASET_ID",
"endTime": "TRAINING_END_DATE",
"lineOfBusiness": "RETAIL"
}
]
}
删除模型
如需删除模型,请使用 projects.locations.instances.models.delete 方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:IAM 设置中列出的 Google Cloud 项目 IDLOCATION:实例的位置;请使用其中一个受支持的区域显示位置us-central1us-east1asia-south1europe-west1europe-west2europe-west4northamerica-northeast1southamerica-east1australia-southeast1
INSTANCE_ID:用户定义的实例标识符MODEL_ID:用户定义的模型标识符
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "https://financialservices.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.cloud.financialservices.v1.OperationMetadata",
"createTime": "2023-03-14T15:52:55.358979323Z",
"target": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID/models/MODEL_ID",
"verb": "delete",
"requestedCancellation": false,
"apiVersion": "v1"
},
"done": false
}
如需详细了解如何获取长时间运行的操作 (LRO) 的结果,请参阅检查结果。