Ax 빠른 시작 (Service API)
Ax 빠른 시작 (Service API)
Ax를 시작하는 가장 권장되는 방법은 Service API를 쓰는 거예요. Client 객체로 실험을 구성하고, 파라미터를 제안받고, 결과를 돌려주는 단순한 루프로 최적화를 진행해요.
출처: Ax Quickstart
설치부터 볼게요.
pip install ax-platform
Ax의 핵심 개념을 먼저 짚어볼게요.
- Experiment — 파라미터를 제안·평가하며 어떤 목표를 개선하는 과정
- Parameter — 조절할 수 있는 변수. 이 변수들의 모음이 탐색 공간
- Objective — 최적화 대상이 되는 값
- Trial — 파라미터 집합과 그에 따른 목표값
- Client — 실험을 관리하고 상호작용 메서드를 제공하는 객체
Service API 코드를 볼게요. 두 개의 실수 파라미터로 Booth 함수를 최적화하는 예시예요.
from ax.api.client import Client
from ax.api.configs import RangeParameterConfig
client = Client()
client.configure_experiment(
name="booth_function",
parameters=[
RangeParameterConfig(name="x1", bounds=(-10.0, 10.0), parameter_type="float"),
RangeParameterConfig(name="x2", bounds=(-10.0, 10.0), parameter_type="float"),
],
)
client.configure_optimization(objective="-1 * booth")
for _ in range(20):
for trial_index, parameters in client.get_next_trials(max_trials=1).items():
client.complete_trial(
trial_index=trial_index,
raw_data={"booth": (parameters["x1"] + 2*parameters["x2"] - 7)**2
+ (2*parameters["x1"] + parameters["x2"] - 5)**2},
)
client.get_best_parameterization()
configure_experiment로 탐색 공간을, configure_optimization으로 목표를 정하고, get_next_trials로 제안받고 complete_trial로 결과를 돌려주는 루프예요. max_trials를 높이면 트라이얼을 병렬로 실행할 수 있어요. 이 함수의 최소점은 (1, 3)인데, Ax가 20회 안에 최적 파라미터를 찾아주는 걸 볼 수 있어요.