CloudML HyperTune

0.1.0.dev6 · active · verified Thu Apr 16

Cloudml-hypertune is a lightweight Python library providing helper functions to report hyperparameter tuning metrics to Google Cloud's Vertex AI (formerly Cloud ML Engine). It enables the hyperparameter tuning service to track and optimize model training trials by collecting objective metrics. Despite its `0.1.0.dev6` version being quite old (last released December 2019), it remains the standard way to report custom metrics for hyperparameter tuning on Google Cloud.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `cloudml-hypertune` within a training script to report a metric. The script accepts hyperparameters as command-line arguments, which is a critical requirement for Google Cloud's hyperparameter tuning service to inject trial-specific values. The `HyperTune` instance then reports the objective metric and the current training step. You would run this script within a container on Vertex AI/Cloud ML Engine.

import hypertune
import argparse
import os

def train_model(learning_rate, num_epochs, metric_tag):
    # Simulate model training with hyperparameters
    # In a real scenario, this would be your ML training loop
    print(f"Training with learning_rate={learning_rate}, num_epochs={num_epochs}")
    
    # Simulate a metric, e.g., validation accuracy
    # In a real scenario, you'd get this from your model's evaluation
    metric_value = 0.5 + (learning_rate * 0.1) + (num_epochs * 0.01)
    
    # Report the metric to Cloud AI Platform / Vertex AI
    hpt = hypertune.HyperTune()
    hpt.report_hyperparameter_tuning_metric(
        hyperparameter_metric_tag=metric_tag, # Must match config.yaml objective metricTag
        metric_value=metric_value,
        global_step=num_epochs # Or current training step
    )
    print(f"Reported metric '{metric_tag}': {metric_value} at step {num_epochs}")

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    # Define hyperparameters as command-line arguments
    parser.add_argument(
        '--learning_rate',
        type=float,
        default=0.01,
        help='Learning rate for training.'
    )
    parser.add_argument(
        '--num_epochs',
        type=int,
        default=10,
        help='Number of epochs for training.'
    )
    parser.add_argument(
        '--metric_tag',
        type=str,
        default='accuracy',
        help='Tag for the metric reported to HyperTune.'
    )
    args = parser.parse_args()
    
    train_model(args.learning_rate, args.num_epochs, args.metric_tag)

view raw JSON →