파이프라인에서 하이퍼 매개 변수를 튜닝하는 방법(v2)
적용 대상:Azure CLI ml 확장 v2(현재)Python SDK azure-ai-ml v2(현재)
이 문서에서는 Azure Machine Learning 파이프라인에서 하이퍼 매개 변수를 튜닝하는 방법을 알아봅니다.
전제 조건
- 하이퍼 매개 변수 튜닝란 무엇이며 SweepJob을 사용하여 Azure Machine Learning에서 하이퍼 매개 변수를 튜닝하는 방법을 이해합니다.
- Azure Machine Learning 파이프라인을 이해합니다.
- 하이퍼 매개 변수를 입력으로 사용하는 명령 구성 요소를 빌드합니다.
Azure Machine Learning 파이프라인에서 하이퍼 매개 변수를 튜닝하는 방법
이 섹션에서는 CLI v2 및 Python SDK를 사용하여 Azure Machine Learning 파이프라인에서 하이퍼 매개 변수를 튜닝하는 방법을 설명합니다. 두 방법 모두 명령 구성 요소 이미 생성 및 명령 구성 요소에서 하이퍼 매개 변수를 입력으로 사용 등과 같은 필수 구성 요소를 공유합니다. 명령 구성 요소가 아직 없는 경우. 다음 링크를 따라 먼저 명령 구성 요소를 만듭니다.
CLI v2
azureml-example 리포지토리에서 이 문서에 사용된 예를 찾을 수 있습니다. [azureml-examples/cli/jobs/pipelines-with-components/pipeline_with_hyperparameter_sweep으로 이동하여 예를 확인합니다.
train.yaml
에 정의된 명령 구성 요소가 이미 있다고 가정합니다. 2단계 파이프라인 작업(학습 및 예측) YAML 파일은 다음과 같습니다.
$schema: https://azuremlschemas.azureedge.net/latest/pipelineJob.schema.json
type: pipeline
display_name: pipeline_with_hyperparameter_sweep
description: Tune hyperparameters using TF component
settings:
default_compute: azureml:cpu-cluster
jobs:
sweep_step:
type: sweep
inputs:
data:
type: uri_file
path: wasbs://datasets@azuremlexamples.blob.core.windows.net/iris.csv
degree: 3
gamma: "scale"
shrinking: False
probability: False
tol: 0.001
cache_size: 1024
verbose: False
max_iter: -1
decision_function_shape: "ovr"
break_ties: False
random_state: 42
outputs:
model_output:
test_data:
sampling_algorithm: random
trial: ./train.yml
search_space:
c_value:
type: uniform
min_value: 0.5
max_value: 0.9
kernel:
type: choice
values: ["rbf", "linear", "poly"]
coef0:
type: uniform
min_value: 0.1
max_value: 1
objective:
goal: minimize
primary_metric: training_f1_score
limits:
max_total_trials: 5
max_concurrent_trials: 3
timeout: 7200
predict_step:
type: command
inputs:
model: ${{parent.jobs.sweep_step.outputs.model_output}}
test_data: ${{parent.jobs.sweep_step.outputs.test_data}}
outputs:
predict_result:
component: ./predict.yml
sweep_step
은 하이퍼 매개 변수 튜닝 단계입니다. 해당 형식은 sweep
이어야 합니다. 그리고 trial
에서 train.yaml
에 정의된 명령 구성 요소를 참조합니다. search space
필드에서 하이퍼 매개 변수 3개(c_value
, kernel
및 coef
)가 검색 공간에 추가됩니다. 이 파이프라인 작업을 제출하면 Azure Machine Learning에서 평가판 구성 요소를 여러 번 실행하여 검색 공간에 따라 하이퍼 매개 변수를 비우고 sweep_step
에서 정의한 정책을 종료합니다. 비우기 작업의 전체 스키마에 대한 비우기 작업 YAML 스키마를 확인합니다.
다음은 평가판 구성 요소 정의(train.yml 파일)입니다.
$schema: https://azuremlschemas.azureedge.net/latest/commandComponent.schema.json
type: command
name: train_model
display_name: train_model
version: 1
inputs:
data:
type: uri_folder
c_value:
type: number
default: 1.0
kernel:
type: string
default: rbf
degree:
type: integer
default: 3
gamma:
type: string
default: scale
coef0:
type: number
default: 0
shrinking:
type: boolean
default: false
probability:
type: boolean
default: false
tol:
type: number
default: 1e-3
cache_size:
type: number
default: 1024
verbose:
type: boolean
default: false
max_iter:
type: integer
default: -1
decision_function_shape:
type: string
default: ovr
break_ties:
type: boolean
default: false
random_state:
type: integer
default: 42
outputs:
model_output:
type: mlflow_model
test_data:
type: uri_folder
code: ./train-src
environment: azureml://registries/azureml/environments/sklearn-1.0/labels/latest
command: >-
python train.py
--data ${{inputs.data}}
--C ${{inputs.c_value}}
--kernel ${{inputs.kernel}}
--degree ${{inputs.degree}}
--gamma ${{inputs.gamma}}
--coef0 ${{inputs.coef0}}
--shrinking ${{inputs.shrinking}}
--probability ${{inputs.probability}}
--tol ${{inputs.tol}}
--cache_size ${{inputs.cache_size}}
--verbose ${{inputs.verbose}}
--max_iter ${{inputs.max_iter}}
--decision_function_shape ${{inputs.decision_function_shape}}
--break_ties ${{inputs.break_ties}}
--random_state ${{inputs.random_state}}
--model_output ${{outputs.model_output}}
--test_data ${{outputs.test_data}}
pipeline.yml의 검색 공간에 추가된 하이퍼 매개 변수는 평가판 구성 요소의 입력이어야 합니다. 평가판 구성 요소의 소스 코드가 ./train-src
폴더 아래에 있습니다. 이 예에서는 단일 train.py
파일입니다. 이 파일은 비우기 작업의 모든 평가판에서 실행되는 코드입니다. 평가판 구성 요소 소스 코드에 메트릭이 pipeline.yml 파일의 primary_metric
값과 정확하게 같은 이름과 함께 로그되었는지 확인합니다. 이 예에서는 ML 실험을 추적하는 데 권장되는 방법인 mlflow.autolog()
를 사용합니다. mlflow에 대한 자세한 내용은 여기를 참조하세요.
다음 코드 조각은 평가판 구성 요소의 소스 코드입니다.
# imports
import os
import mlflow
import argparse
import pandas as pd
from pathlib import Path
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
# define functions
def main(args):
# enable auto logging
mlflow.autolog()
# setup parameters
params = {
"C": args.C,
"kernel": args.kernel,
"degree": args.degree,
"gamma": args.gamma,
"coef0": args.coef0,
"shrinking": args.shrinking,
"probability": args.probability,
"tol": args.tol,
"cache_size": args.cache_size,
"class_weight": args.class_weight,
"verbose": args.verbose,
"max_iter": args.max_iter,
"decision_function_shape": args.decision_function_shape,
"break_ties": args.break_ties,
"random_state": args.random_state,
}
# read in data
df = pd.read_csv(args.data)
# process data
X_train, X_test, y_train, y_test = process_data(df, args.random_state)
# train model
model = train_model(params, X_train, X_test, y_train, y_test)
# Output the model and test data
# write to local folder first, then copy to output folder
mlflow.sklearn.save_model(model, "model")
from distutils.dir_util import copy_tree
# copy subdirectory example
from_directory = "model"
to_directory = args.model_output
copy_tree(from_directory, to_directory)
X_test.to_csv(Path(args.test_data) / "X_test.csv", index=False)
y_test.to_csv(Path(args.test_data) / "y_test.csv", index=False)
def process_data(df, random_state):
# split dataframe into X and y
X = df.drop(["species"], axis=1)
y = df["species"]
# train/test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=random_state
)
# return split data
return X_train, X_test, y_train, y_test
def train_model(params, X_train, X_test, y_train, y_test):
# train model
model = SVC(**params)
model = model.fit(X_train, y_train)
# return model
return model
def parse_args():
# setup arg parser
parser = argparse.ArgumentParser()
# add arguments
parser.add_argument("--data", type=str)
parser.add_argument("--C", type=float, default=1.0)
parser.add_argument("--kernel", type=str, default="rbf")
parser.add_argument("--degree", type=int, default=3)
parser.add_argument("--gamma", type=str, default="scale")
parser.add_argument("--coef0", type=float, default=0)
parser.add_argument("--shrinking", type=bool, default=False)
parser.add_argument("--probability", type=bool, default=False)
parser.add_argument("--tol", type=float, default=1e-3)
parser.add_argument("--cache_size", type=float, default=1024)
parser.add_argument("--class_weight", type=dict, default=None)
parser.add_argument("--verbose", type=bool, default=False)
parser.add_argument("--max_iter", type=int, default=-1)
parser.add_argument("--decision_function_shape", type=str, default="ovr")
parser.add_argument("--break_ties", type=bool, default=False)
parser.add_argument("--random_state", type=int, default=42)
parser.add_argument("--model_output", type=str, help="Path of output model")
parser.add_argument("--test_data", type=str, help="Path of output model")
# parse args
args = parser.parse_args()
# return args
return args
# run script
if __name__ == "__main__":
# parse args
args = parse_args()
# run main function
main(args)
Python SDK
Azure Machine Learning-example 리포지토리에서 Python SDK 예를 찾을 수 있습니다. azureml-examples/sdk/jobs/pipelines/1c_pipeline_with_hyperparameter_sweep으로 이동하여 예를 확인합니다.
Azure Machine Learning Python SDK v2에서 .sweep()
메서드를 호출하여 명령 구성 요소에 하이퍼 매개 변수 튜닝을 사용할 수 있습니다.
다음 코드 조각에서는 train_model
에 비우기를 사용하는 방법을 보여줍니다.
train_component_func = load_component(source="./train.yml")
score_component_func = load_component(source="./predict.yml")
# define a pipeline
@pipeline()
def pipeline_with_hyperparameter_sweep():
"""Tune hyperparameters using sample components."""
train_model = train_component_func(
data=Input(
type="uri_file",
path="wasbs://datasets@azuremlexamples.blob.core.windows.net/iris.csv",
),
c_value=Uniform(min_value=0.5, max_value=0.9),
kernel=Choice(["rbf", "linear", "poly"]),
coef0=Uniform(min_value=0.1, max_value=1),
degree=3,
gamma="scale",
shrinking=False,
probability=False,
tol=0.001,
cache_size=1024,
verbose=False,
max_iter=-1,
decision_function_shape="ovr",
break_ties=False,
random_state=42,
)
sweep_step = train_model.sweep(
primary_metric="training_f1_score",
goal="minimize",
sampling_algorithm="random",
compute="cpu-cluster",
)
sweep_step.set_limits(max_total_trials=20, max_concurrent_trials=10, timeout=7200)
score_data = score_component_func(
model=sweep_step.outputs.model_output, test_data=sweep_step.outputs.test_data
)
pipeline_job = pipeline_with_hyperparameter_sweep()
# set pipeline level compute
pipeline_job.settings.default_compute = "cpu-cluster"
먼저 train.yml
파일에 정의된 train_component_func
을 로드합니다. train_model
을 만들 때 검색 공간(줄 15~17)에 c_value
, kernel
및 coef0
을 추가합니다. 줄 30~35에서는 기본 메트릭, 샘플링 알고리즘 등을 정의합니다.
스튜디오에서 비우기 단계를 사용하여 파이프라인 작업 확인
파이프라인 작업을 제출하면 SDK 또는 CLI 위젯에서 스튜디오 UI에 대한 웹 URL 링크를 제공합니다. 링크는 기본적으로 파이프라인 그래프 보기로 안내합니다.
비우기 단계의 세부 정보를 확인하려면 비우기 단계를 두 번 클릭하고 오른쪽 패널의 자식 작업 탭으로 이동합니다.
그러면 다음 스크린샷과 같이 비우기 작업 페이지에 연결됩니다. 자식 작업 탭으로 이동하면 여기에서 모든 자식 작업의 메트릭과 모든 자식 작업 목록을 볼 수 있습니다.
자식 작업이 실패한 경우 해당 자식 작업의 이름을 선택하여 해당 자식 작업의 세부 정보 페이지를 입력합니다(아래 스크린샷 참조). 유용한 디버그 정보는 출력 + 로그 아래에 있습니다.