C.W.K.
Stream
Lesson 03 of 06 · published

Full manual loop

~8 min · custom-train

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

fit() 안 쓰고 직접 epoch loop 짤 때: for epoch in range(N); for batch in dataset; with GradientTape; forward + loss; tape.gradient; optimizer.apply_gradients. callback 없으니까 logging / checkpoint / EarlyStopping 다 직접.

이 자유의 대가는 인프라 부재. progress bar 안 떠 (tqdm 직접), checkpoint 매 N batch 직접 저장, validation 도 직접 호출. 짧은 실험엔 OK, production 엔 train_step override 가 더 단단.

백엔드 노트:
⚙️ Backend Note

Code

# Backend-agnostic manual training loop
optimizer = keras.optimizers.Adam(1e-3)
loss_fn = keras.losses.SparseCategoricalCrossentropy()

for epoch in range(10):
    for step, (x_batch, y_batch) in enumerate(train_dataset):
        # Forward pass with gradient tracking
        y_pred = model(x_batch, training=True)
        loss = loss_fn(y_batch, y_pred)

        # Compute gradients and update weights
        grads = optimizer.compute_gradients(
            loss, model.trainable_variables
        )
        optimizer.apply(grads)

    print(f"Epoch {{epoch}}, Loss: {{loss:.4f}}")

External links

Exercise

MNIST 의 full manual training loop (fit 없이) 짜. validation pass + best-model 저장 포함. fit 기반 버전과 비교 — fit 이 공짜로 주던 거를 어디서 직접 구현했는지 메모.

Progress

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

댓글 0

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

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