Optuna로 하이퍼파라미터 최적화

Python으로 배우는 Deep Reinforcement Learning

Timothée Carayol

Principal Machine Learning Engineer, Komment

하이퍼파라미터란

 

 

  • DRL 알고리즘에는 하이퍼파라미터가 많음
  • 성능에 큰 영향
  • 하이퍼파라미터 수가 늘면 탐색 복잡도 증가

 

예시
할인율
PPO: 클리핑 엡실론, 엔트로피 보너스
경험 재현: 버퍼 크기, 배치 크기
감소형 엡실론 탐욕 스케줄
고정 Q-타깃: $\tau$
학습률
레이어 수, 레이어당 노드 수...
Python으로 배우는 Deep Reinforcement Learning

하이퍼파라미터 값 선택 방법

 

목표: 평균 누적 보상

하이퍼파라미터 탐색 기법:

  • 수동 시행착오
  • 그리드 서치
  • 랜덤 서치
  • 전용 알고리즘

수십 개의 노브와 다이얼이 있는 거대한 기계

Python으로 배우는 Deep Reinforcement Learning

Optuna 로고

 

Optuna 워크플로:

  • 목표 함수 정의
  • Optuna study 생성
  • Optuna로 trials 반복

 

 

import optuna

def objective(trial): ...
study = optuna.create_study()
study.optimize(objective, n_trials=100)
study.best_params
{'learning_rate': 0.001292481, 'batch_size': 8}
Python으로 배우는 Deep Reinforcement Learning

목표 함수 지정

 

객관식 함수에서:

  • 관심 하이퍼파라미터 정의
  • 최적화할 지표 정의

하이퍼파라미터 지정 완전 지원:

  • float
  • integer
  • categorical

 

def objective(trial: optuna.trial.Trial):

# Hyperparameters x and y between -10 and 10
x = trial.suggest_float('x', -10, 10) y = trial.suggest_float('y', -10, 10)
# Return the metric to minimize return (x - 2) ** 2 + 1.2 * (y + 3) ** 2
Python으로 배우는 Deep Reinforcement Learning

Optuna study

 

  • sqlite로 study 저장
  • 기본 샘플러(TPE)로 n_trials 시도
    • 초기에는 무작위로 선택
    • 이후 유망한 영역에 집중
  • n_trials 생략 시: 중단할 때까지 실행
  • 나중에 DB에서 study 로드 가능

 

import sqlite
study = optuna.create_study(
                 storage="sqlite:///DRL.db",
                 study_name="my_study")

study.optimize(objective, n_trials=100)
loaded_study = optuna.load_study(
                        study_name="my_study", 
                        storage="sqlite:///DRL.db")
Python으로 배우는 Deep Reinforcement Learning

study 결과 탐색

optuna.visualization.plot_param_importances(study)

x와 y 하이퍼파라미터 중요도 막대차트: y는 0.71, x는 0.29.

optuna.visualization.plot_contour(study)

시도당 점 하나가 있는 등고선도. 점들은 x = 2, y = -3 근처에 밀집.

Python으로 배우는 Deep Reinforcement Learning

연습해 봅시다!

Python으로 배우는 Deep Reinforcement Learning

Preparing Video For Download...