[I3 - sweep

from azure.ai.ml import command
from azure.ai.ml.sweep import Choice, Uniform, BanditPolicy

# define the job
job = command(
    code="./src",
    command=(
        "python train.py "
        "--learning-rate ${{inputs.learning_rate}} "
        "--max-depth ${{inputs.max_depth}}"
    ),
    inputs={
        "learning_rate": 0.01,
        "max_depth": 5,
    },
    environment="azureml:train-env:3",
    compute="cpu-cluster",
)

# sweep: replace fixed values with distributions
job_for_sweep = job(
    learning_rate=Uniform(
        min_value=0.001,
        max_value=0.1,
    ),
    max_depth=Choice(
        values=[3, 5, 8, 12]
    ),
)

# define sweep job
sweep_job = job_for_sweep.sweep(
    sampling_algorithm="random",
    primary_metric="f1_score",
    goal="Maximize",
    max_total_trials=30,
    max_concurrent_trials=5,
)

# early-termination
policy = BanditPolicy(
    slack_factor=0.15,  # relative tolerance
    # slack amount=0.05,  # absolute tolerance
    evaluation_interval=1,
    delay_evaluation=10,
)

# submit
returned_sweep = ml_client.jobs.create_or_update(
    sweep_job,
    experiment_name="detector-sweep"
)