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

model.compile()

~8 min · training

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

compile + fit + callback. 세 줄 뒤에 state machine, optimizer step, loss reduction, metrics accumulator, callback bus 다 숨어있어. fit() 이 이상한 짓 할 때 *어느 손잡이* 가 풀렸는지 정확히 알게 해주는 unpack 트랙.

compile 이 진짜 하는 일

compile 은 weight 를 건드리지도, forward pass 한 번 돌리지도 않아. 순수 *환경 설정* 이야. 셋을 model 에 박아둬서 fit() 이 어떻게 배워야 할지 알게 해주는 거지. optimizer 는 gradient 를 weight 업데이트로 바꾸는 방법, loss 는 optimizer 가 0 으로 밀어붙일 scalar 목표값, metrics 는 gradient 엔 손 안 대고 진행 상황만 읽어주는 observer.

Code 섹션에 두 형태 — classification compile 과 regression compile. 둘 사이에 바뀌는 건 loss 랑 metrics 뿐이고 호출 구조는 똑같아. 패턴 하나 외우면 둘 다 끝.

문자열 vs 인스턴스 — 나중에 왜 중요한지

빠르게는 문자열 ('adam', 'mse'), 제대로 조절하려면 인스턴스. 'adam' 은 default learning rate 의 Adam 을 주고, keras.optimizers.Adam(learning_rate=1e-3) 은 직접 정하게 해줘. hyperparameter 튜닝하거나 learning-rate schedule 붙이거나 model 에 저장될 config 를 정확히 못박고 싶으면 바로 인스턴스로 가. 딱 하나 규칙 — fit() 전에 무조건 compile(). compile 안 한 model 은 '더 나아진다' 가 뭔지조차 몰라.

Code

classification 과 regression 의 compile()·python
model.compile(
    optimizer="adam",                            # or keras.optimizers.Adam(1e-3)
    loss="sparse_categorical_crossentropy",      # or Loss instance
    metrics=["accuracy"],                        # list of metric names/instances
)

# For regression:
model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=1e-3),
    loss="mse",
    metrics=["mae"],
)

External links

Exercise

같은 MNIST 모델을 두 방식으로 compile — optimizer='adam' / keras.optimizers.Adam(learning_rate=1e-3). 둘 다 5 epoch 학습, curve 동일한지 확인.

Progress

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

댓글 0

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

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