본문 바로가기
C.W.K.
Stream
Lesson 05 of 08 · published

model.fit()

~8 min · training

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

한 번의 호출로 전체 학습 루프를 실행해

model.fit()compile()에서 설정한 내용을 실제로 실행해. 배치 구성, 순전파, 손실 계산, 역전파, 옵티마이저 갱신, 평가지표 누적, 콜백 호출이 모두 안에 들어 있어. epochs는 데이터를 몇 번 훑을지, batch_size는 한 번의 갱신에 몇 샘플을 쓸지 정해.

검증 방법과 불균형 가중치를 정확히 골라

별도 검증 데이터가 없다면 validation_split=0.2를 쓸 수 있지만 학습 데이터의 마지막 20%를 떼므로 데이터가 정렬되어 있으면 잘못된 검증 집합이 될 수 있어. 가능하면 validation_data=(x_val, y_val)로 따로 만든 데이터를 넘겨. class_weight는 희귀 클래스의 오류를 더 무겁게 계산해 모델이 다수 클래스만 예측하는 일을 막아.

fit()은 에포크별 손실과 평가지표를 담은 History 객체를 돌려줘. .history에는 loss, val_loss와 모든 지표의 학습·검증 값이 들어 있어 학습 곡선을 그릴 수 있어. 큰 데이터는 generator, tf.Dataset, torch.DataLoader로 넘겨도 처리해.

Code

model.fit()의 핵심 인자와 History 객체·python
history = model.fit(
    x_train, y_train,
    epochs=20,                 # Number of passes through the data
    batch_size=32,              # Samples per gradient update
    validation_split=0.2,       # Use 20% of training data for validation
    # OR: validation_data=(x_val, y_val),
    class_weight={0: 1.0, 1: 5.0},  # For imbalanced data
    callbacks=[...],              # List of callback instances
)

# history.history contains loss/metric values per epoch
print(history.history["loss"])         # [0.83, 0.54, 0.41, ...]
print(history.history["val_accuracy"]) # [0.72, 0.81, 0.85, ...]

External links

Exercise

MNIST를 10에포크 학습하고 history.history를 저장해. 학습·검증 손실과 정확도를 두 그래프로 그리고 검증 손실이 다시 오르기 시작한 에포크를 찾아.

Progress

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

댓글 0

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

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