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

Hyperparameter tuning

~8 min · advanced

Level 0Keras 도제
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Keras tutorial 졸업한 사람 vs 실제로 ship 하는 사람 가르는 패턴들. KerasTuner 로 hyperparameter sweep, mixed-precision (FP16/BF16) 으로 2-3 배 속도, multi-GPU distribution strategy, post-training quantization, knowledge distillation, training 이 조용히 망가질 때의 debugging playbook.

손으로 다이얼 돌리는 거 그만

learning rate 찍고 layer 폭 찍어가며 오후 내내 돌리면 정확도 몇 점은 올라가. 근데 그거 사실 *직감을 optimizer 삼은* 편향된 blind search 야. KerasTuner 는 그 루프를 갈아치워 — build_model(hp) 안에 *search space* 한 번만 선언하면, tuner 가 trial 돌리고 점수 기록하고 best model 돌려줘.

search 의 구조

핵심은 hyperparameter 가 *타이핑하는 상수* 가 아니라 *sampling 하는 인자* 가 된다는 거. hp.Int('units', 32, 512, step=32) 는 layer 폭을 tunable 축으로 만들고, hp.Float('lr', 1e-4, 1e-2, sampling='log') 는 learning rate 에 같은 걸 해줘. 여기서 sampling='log' 가 중요해 — learning rate 는 로그 스케일에 살거든. trial 을 자릿수마다 고르게 퍼뜨려야지, 범위 위쪽에 몰리면 안 돼.

search 전략 고르기

  • RandomSearch — uniform sampling. 정직한 baseline 인데, 작은 space 에선 의외로 이기기 어려워.
  • BayesianOptimization — 지난 trial 에 Gaussian-process surrogate 를 fit 해서 개선 기대되는 곳을 sampling. 똑똑하지만 landscape 가 꽤 매끄럽다고 가정해.
  • Hyperband — bandit 방식. trial 여러 개를 싸게 시작해서 약한 놈들 일찍 죽이고, 아낀 budget 을 생존자한테 몰아줘. DL 에선 대부분 config 가 별로니까 그걸 빨리 알아내는 게 이득 — 보통 이게 정답 default.

Code

search space 선언 후 BayesianOptimization 실행·python
import keras_tuner

def build_model(hp):
    model = keras.Sequential([
        keras.Input(shape=(784,)),
        layers.Dense(
            units=hp.Int("units", min_value=32, max_value=512, step=32),
            activation="relu",
        ),
        layers.Dropout(hp.Float("dropout", 0.0, 0.5, step=0.1)),
        layers.Dense(10, activation="softmax"),
    ])
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Float("lr", 1e-4, 1e-2, sampling="log")
        ),
        loss="sparse_categorical_crossentropy",
        metrics=["accuracy"],
    )
    return model

# Search strategies
tuner = keras_tuner.BayesianOptimization(
    build_model, objective="val_accuracy", max_trials=20,
)
tuner.search(x_train, y_train, epochs=10, validation_split=0.2)
best_model = tuner.get_best_models()[0]

External links

Exercise

MNIST 의 작은 MLP 튜닝 — layer 수 (1-4), unit per layer (32-256), learning rate (1e-4 ~ 1e-2). Hyperband (max_epochs=20). best model 을 직접 튜닝한 baseline 과 비교.

Progress

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

댓글 0

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

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