Bird
0
0

Find the bug in this early stopping implementation:

medium📝 Debug Q7 of 15
Agentic AI - Production Agent Architecture
Find the bug in this early stopping implementation:
best_loss = float('inf')
epochs_no_improve = 0
for epoch in range(10):
    val_loss = get_val_loss(epoch)
    if val_loss > best_loss:
        epochs_no_improve += 1
    else:
        best_loss = val_loss
        epochs_no_improve = 0
    if epochs_no_improve == 3:
        print('Stop training')
        break
AThe condition should be val_loss >= best_loss
BThe epochs_no_improve reset is missing
CThe break should happen when epochs_no_improve > 3
DThe initial best_loss should be zero
Step-by-Step Solution
Solution:
  1. Step 1: Understand early stopping logic

    Early stopping triggers when validation loss does not improve (i.e., is not less than best_loss).
  2. Step 2: Check condition correctness

    Using val_loss > best_loss misses the case when val_loss equals best_loss, which should also count as no improvement.
  3. Final Answer:

    The condition should be val_loss >= best_loss -> Option A
  4. Quick Check:

    Early stopping condition = val_loss >= best_loss [OK]
Quick Trick: Use >= to catch no improvement cases [OK]
Common Mistakes:
  • Using > misses equal loss cases
  • Not resetting epochs_no_improve properly
  • Starting best_loss at zero causes wrong logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes