如何通过Python读取Cloud ML Engine超参数调优作业的训练输出?
Absolutely! You can pull those trial details and finalMetric JSON data right into Python using Google's official client libraries for Cloud AI Platform (the rebranded version of Cloud ML Engine). Here's how to do it step by step:
Set up your dependencies
Install the latest Vertex AI client library (since Cloud ML Engine is now integrated into Vertex AI):pip install google-cloud-aiplatformAuthenticate your environment
You can either run this terminal command to set up application default credentials:gcloud auth application-default loginOr set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to point to your service account key file.Write Python code to fetch trial data
Here's a complete example that retrieves your hyperparameter tuning job, loops through each trial, and extracts the final metric (plus other key trial details):from google.cloud import aiplatform import json # Initialize the client with your project and region aiplatform.init(project="your-project-id", region="us-central1") # Replace with your actual region # Fetch the tuning job using its full resource name or ID (find this in the Cloud Console job details) tuning_job = aiplatform.HyperparameterTuningJob.get( "projects/your-project-id/locations/us-central1/jobs/your-hpt-job-id" ) # Iterate through each trial to extract and save data for trial in tuning_job.trials: print(f"=== Trial {trial.id} ===") print(f"Status: {trial.state}") print(f"Final Metric Value: {trial.final_metric.value}") print(f"Final Metric Raw JSON: {trial.final_metric.metric}\n") # Save the final metric to a local JSON file with open(f"trial_{trial.id}_final_metric.json", "w") as outfile: json.dump(trial.final_metric.metric, outfile, indent=2)Key details to note
- The
trial.final_metric.metricattribute gives you the exact JSON structure you see in the Training Output section of the Cloud Console. - If you're still using the older
google-cloud-mllibrary, you can use theml_v1module to fetch job data, but the Vertex AI library is the recommended, up-to-date option. - Ensure your user or service account has the
aiplatform.jobs.getpermission to access the job's trial data.
- The
内容的提问来源于stack exchange,提问作者Fariborz Ghavamian

