You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python读取Cloud ML Engine超参数调优作业的训练输出?

How to Download Cloud ML Engine Hyperparameter Tuning Trial Data with Python

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-aiplatform
    
  • Authenticate your environment
    You can either run this terminal command to set up application default credentials:

    gcloud auth application-default login
    

    Or set the GOOGLE_APPLICATION_CREDENTIALS environment 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.metric attribute 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-ml library, you can use the ml_v1 module 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.get permission to access the job's trial data.

内容的提问来源于stack exchange,提问作者Fariborz Ghavamian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:16:55