使用 export_model 方法导出用于表格分类的模型。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Java
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Java 设置说明执行操作。如需了解详情,请参阅 Vertex AI Java API 参考文档。
如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1.ExportModelOperationMetadata;
import com.google.cloud.aiplatform.v1.ExportModelRequest;
import com.google.cloud.aiplatform.v1.ExportModelResponse;
import com.google.cloud.aiplatform.v1.GcsDestination;
import com.google.cloud.aiplatform.v1.ModelName;
import com.google.cloud.aiplatform.v1.ModelServiceClient;
import com.google.cloud.aiplatform.v1.ModelServiceSettings;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class ExportModelTabularClassificationSample {
public static void main(String[] args)
throws InterruptedException, ExecutionException, TimeoutException, IOException {
// TODO(developer): Replace these variables before running the sample.
String gcsDestinationOutputUriPrefix = "gs://your-gcs-bucket/destination_path";
String project = "YOUR_PROJECT_ID";
String modelId = "YOUR_MODEL_ID";
exportModelTableClassification(gcsDestinationOutputUriPrefix, project, modelId);
}
static void exportModelTableClassification(
String gcsDestinationOutputUriPrefix, String project, String modelId)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
ModelServiceSettings modelServiceSettings =
ModelServiceSettings.newBuilder()
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
.build();
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ModelServiceClient modelServiceClient = ModelServiceClient.create(modelServiceSettings)) {
String location = "us-central1";
ModelName modelName = ModelName.of(project, location, modelId);
GcsDestination.Builder gcsDestination = GcsDestination.newBuilder();
gcsDestination.setOutputUriPrefix(gcsDestinationOutputUriPrefix);
ExportModelRequest.OutputConfig outputConfig =
ExportModelRequest.OutputConfig.newBuilder()
.setExportFormatId("tf-saved-model")
.setArtifactDestination(gcsDestination)
.build();
OperationFuture<ExportModelResponse, ExportModelOperationMetadata> exportModelResponseFuture =
modelServiceClient.exportModelAsync(modelName, outputConfig);
System.out.format(
"Operation name: %s\n", exportModelResponseFuture.getInitialFuture().get().getName());
System.out.println("Waiting for operation to finish...");
ExportModelResponse exportModelResponse =
exportModelResponseFuture.get(300, TimeUnit.SECONDS);
System.out.format(
"Export Model Tabular Classification Response: %s", exportModelResponse.toString());
}
}
}
Node.js
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Node.js 设置说明执行操作。如需了解详情,请参阅 Vertex AI Node.js API 参考文档。
如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/
// const gcsDestinationOutputUriPrefix ='YOUR_GCS_DESTINATION_\
// OUTPUT_URI_PREFIX'; eg. "gs://<your-gcs-bucket>/destination_path"
// const modelId = 'YOUR_MODEL_ID';
// const project = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION';
// Imports the Google Cloud Model Service Client library
const {ModelServiceClient} = require('@google-cloud/aiplatform');
// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};
// Instantiates a client
const modelServiceClient = new ModelServiceClient(clientOptions);
async function exportModelTabularClassification() {
// Configure the name resources
const name = `projects/${project}/locations/${location}/models/${modelId}`;
// Configure the outputConfig resources
const outputConfig = {
exportFormatId: 'tf-saved-model',
artifactDestination: {
outputUriPrefix: gcsDestinationOutputUriPrefix,
},
};
const request = {
name,
outputConfig,
};
// Export Model request
const [response] = await modelServiceClient.exportModel(request);
console.log(`Long running operation : ${response.name}`);
// Wait for operation to complete
await response.promise();
console.log(`Export model response : ${JSON.stringify(response.result)}`);
}
exportModelTabularClassification();
Python
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。如需了解详情,请参阅 Vertex AI Python API 参考文档。
如需向 Vertex AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
from google.cloud import aiplatform_v1beta1
def export_model_tabular_classification_sample(
project: str,
model_id: str,
gcs_destination_output_uri_prefix: str,
location: str = "us-central1",
api_endpoint: str = "us-central1-aiplatform.googleapis.com",
timeout: int = 300,
):
# The AI Platform services require regional API endpoints.
client_options = {"api_endpoint": api_endpoint}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform_v1beta1.ModelServiceClient(client_options=client_options)
gcs_destination = {"output_uri_prefix": gcs_destination_output_uri_prefix}
output_config = {
"artifact_destination": gcs_destination,
"export_format_id": "tf-saved-model",
}
name = client.model_path(project=project, location=location, model=model_id)
response = client.export_model(name=name, output_config=output_config)
print("Long running operation:", response.operation.name)
print("output_info:", response.metadata.output_info)
export_model_response = response.result(timeout=timeout)
print("export_model_response:", export_model_response)
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。