Hint: Use named arguments with correct types for early stopping [OK]
Common Mistakes:
Swapping patience and min_delta values
Using positional args without clarity
Passing wrong data types for parameters
3. Given this snippet, what will be printed after 4 epochs if validation losses are [0.5, 0.4, 0.42, 0.43] and patience=2?
early_stopping = EarlyStopping(patience=2, min_delta=0.01)
for epoch, val_loss in enumerate([0.5, 0.4, 0.42, 0.43]):
early_stopping(val_loss)
if early_stopping.early_stop:
print(f"Stop at epoch {epoch}")
break
medium
A. Stop at epoch 3
B. Stop at epoch 2
C. No stop, training continues
D. Stop at epoch 1
Solution
Step 1: Track validation loss improvements
Loss improves from 0.5 to 0.4 (improvement 0.1 > 0.01), then worsens 0.4 to 0.42 (no improvement), then 0.42 to 0.43 (no improvement).
Step 2: Apply patience logic
Patience=2 means stop if no improvement for 2 consecutive epochs. However, min_delta=0.01 means improvement must be at least 0.01 to reset patience. The increases from 0.4 to 0.42 and 0.42 to 0.43 are less than min_delta, so they count as no improvement. But patience=2 allows 2 such epochs before stopping. After epoch 3, patience is exhausted, so early stopping triggers at epoch 3. But since the loop breaks after printing, the print statement occurs at epoch 3.
Step 3: Check code behavior
The code prints "Stop at epoch 3" and breaks.
Final Answer:
Stop at epoch 3 -> Option A
Quick Check:
Patience 2 triggers stop after 2 bad epochs [OK]
Hint: Count consecutive no-improvement epochs to patience limit [OK]
Common Mistakes:
Stopping too early after 1 bad epoch
Ignoring min_delta threshold
Assuming stop only after patience+1 epochs
4. Identify the bug in this early stopping usage:
early_stopping = EarlyStopping(patience=3, min_delta=0.01)
for val_loss in val_losses:
if early_stopping.early_stop:
break
early_stopping(val_loss)
medium
A. val_losses should be a tensor, not a list
B. patience value is too high
C. min_delta should be zero
D. Check for early_stop before calling early_stopping(val_loss)
Solution
Step 1: Analyze loop order
The code checks early_stop before updating early_stopping with current val_loss, so it misses stopping at the right time.
Step 2: Correct order for early stopping check
Call early_stopping(val_loss) first to update state, then check early_stop to break if needed.
Final Answer:
Check for early_stop before calling early_stopping(val_loss) -> Option D
Quick Check:
Update early stopping before checking early_stop flag [OK]
Hint: Call early_stopping(val_loss) before checking early_stop [OK]
Common Mistakes:
Checking early_stop before updating with new loss
Misunderstanding patience and min_delta roles
Assuming val_losses must be tensors
5. You want to implement early stopping that only triggers if validation loss improves by at least 0.005 within 4 epochs. Which settings for patience and min_delta should you use?
hard
A. patience=4, min_delta=0.005
B. patience=0.005, min_delta=4
C. patience=4, min_delta=0.05
D. patience=5, min_delta=0.005
Solution
Step 1: Understand patience and min_delta roles
Patience is how many epochs to wait for improvement; min_delta is minimum improvement size.
Step 2: Match requirement to parameters
To trigger after 4 epochs without improvement of at least 0.005, set patience=4 and min_delta=0.005.
Final Answer:
patience=4, min_delta=0.005 -> Option A
Quick Check:
Patience=4 and min_delta=0.005 matches requirement [OK]