C.W.K.
Stream
Lesson 02 of 07 · published

Hyperparameter

~28 min · hyperparameters, tuning, search

Level 0Scout
0 XP0/48 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

hyperparameter가 하는 일

Hyperparameter는 학습 알고리즘이 데이터에서 안 배우는 손잡이 — regularization 강도, tree depth, learning rate, estimator 수. 용량과 bias-variance trade-off를 control. 튜닝은 CV score 최대화하려고 configuration space를 search 하는 거.

세 search 전략

  • Grid search — 작은 grid 위 exhaustive. 추론 쉬움, grid 커지면 비싸짐.
  • Random search — distribution에서 sample. 소수 hyperparameter만 중요할 때 unit budget 당 더 넓게 cover.
  • Bayesian / Optuna — objective 모델링하고 promising config 제안. 비싼 evaluation에 best.

search 인플레이션 trap

같은 validation set 위 100 trial 튜닝하면, 그 set에 한 번도 train 안 해도 overfit 하고 있어. nested CV 쓰거나 별도 test set untouched 유지. search budget도 free resource가 아니라 네가 고르는 hyperparameter야.

Code

넓은 space 위 RandomizedSearchCV·python
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import loguniform, randint

param_dist = {
    "clf__C": loguniform(1e-3, 1e3),
    "clf__l1_ratio": [0.0, 0.2, 0.5, 0.8, 1.0],
}
search = RandomizedSearchCV(
    pipe, param_distributions=param_dist, n_iter=40, cv=5,
    scoring="average_precision", random_state=7, n_jobs=-1
)
search.fit(X_train, y_train)
print(search.best_params_, search.best_score_)
tree ensemble을 위한 Optuna·python
import optuna
import lightgbm as lgb
from sklearn.model_selection import cross_val_score

def objective(trial):
    params = {
        "n_estimators": trial.suggest_int("n_estimators", 100, 800),
        "learning_rate": trial.suggest_float("learning_rate", 0.01, 0.3, log=True),
        "num_leaves": trial.suggest_int("num_leaves", 16, 256),
        "min_child_samples": trial.suggest_int("min_child_samples", 5, 100),
    }
    model = lgb.LGBMClassifier(**params)
    scores = cross_val_score(model, X_train, y_train, cv=5, scoring="average_precision")
    return scores.mean()

study = optuna.create_study(direction="maximize")
study.optimize(objective, n_trials=40)

External links

Exercise

RandomizedSearchCV(40 trial)로 모델의 hyperparameter 하나 튜닝. mean CV score를 hyperparameter 값에 대해 plot. score가 멈추는 plateau 식별하고 거기서 멈춰.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.