Bird
Raised Fist0

Implement Early Stopping and Learning Rate Scheduler

CODINGintermediateInterview freq: high
Choose your preparation mode6 modes available
▶
Execution Trace
Step 1/5
Epoch 1

Initial loss is 0.95, learning rate is set to 0.01.

🎯 Understanding initial setup of training parameters.
learning_rate = 0.01
early_stopping_counter = 0
best_loss = float('inf')
for epoch in range(1, num_epochs + 1):
    loss = train_one_epoch(model, data_loader)
Epoch 2

Loss decreases to 0.85, learning rate decays to 0.0095.

🎯 Learning rate decay mechanism in action.
scheduler.step()
if loss < best_loss:
    best_loss = loss
    early_stopping_counter = 0
Epoch 3

Loss increases to 0.88, early stopping counter increments to 1.

🎯 Tracking lack of improvement with early stopping.
if loss >= best_loss:
    early_stopping_counter += 1
if early_stopping_counter >= patience:
    break
Epoch 4

Loss increases to 0.90, early stopping counter increments to 2.

🎯 Understanding when to stop training due to no improvement.
if early_stopping_counter >= patience:
    print('Early stopping')
    break
Epoch 5

Loss decreases to 0.87, early stopping counter resets to 0.

🎯 Resetting early stopping counter on improvement.
if loss < best_loss:
    best_loss = loss
    early_stopping_counter = 0
📊
Diagram
Diagram
┌──────────────────────────────────────────────────────────────┐
│                      Training Loop                           │
├──────────────────────────────────────────────────────────────┤
│ Epoch 1:                                                     │
│   - Loss: 0.95                                               │
│   - Learning Rate: 0.01                                      │
│   - Early Stopping Counter: 0                                │
├──────────────────────────────────────────────────────────────┤
│ Epoch 2:                                                     │
│   - Loss: 0.85                                               │
│   - Learning Rate: 0.0095 (decayed)                          │
│   - Early Stopping Counter: 0                                │
├──────────────────────────────────────────────────────────────┤
│ Epoch 3:                                                     │
│   - Loss: 0.88                                               │
│   - Learning Rate: 0.009025 (decayed)                        │
│   - Early Stopping Counter: 1                                │
├──────────────────────────────────────────────────────────────┤
│ Epoch 4:                                                     │
│   - Loss: 0.90                                               │
│   - Learning Rate: 0.00857375 (decayed)                      │
│   - Early Stopping Counter: 2                                │
├──────────────────────────────────────────────────────────────┤
│ Epoch 5:                                                     │
│   - Loss: 0.87                                               │
│   - Learning Rate: 0.0081450625 (decayed)                    │
│   - Early Stopping Counter: 0 (reset)                        │
└──────────────────────────────────────────────────────────────┘
  • Epoch: Iteration of training over the dataset
  • Loss: Measure of model error
  • Learning Rate: Step size for updating model weights
  • Early Stopping Counter: Tracks epochs without improvement
🔑
Key Moments
▲
Q: Why does the learning rate decrease each epoch?
A: The learning rate decreases due to the learning rate scheduler, which helps in fine-tuning the model by taking smaller steps as training progresses.
Q: What triggers the early stopping mechanism?
A: Early stopping is triggered when the loss does not improve for a specified number of consecutive epochs, indicating potential overfitting.
Q: How does the early stopping counter reset?
A: The early stopping counter resets to zero whenever there is an improvement in the loss, indicating that the model is still learning.

This visual trace document explains the implementation of early stopping and learning rate scheduling in a training loop. The diagram shows the state of the training process at each epoch, highlighting changes in loss, learning rate, and the early stopping counter. The trace steps provide a detailed view of how these components interact, with code snippets illustrating the logic behind each step. Key moments focus on understanding the purpose and function of learning rate decay and early stopping, ensuring a comprehensive grasp of these techniques.

Primary Focus

✓ Implementing early stopping and learning rate scheduling in a training loop

Practice

(1/5)
1. What does the following Python code output when computing the ROC curve?
from sklearn.metrics import roc_curve
import numpy as np

y_true = np.array([0, 0, 1, 1])
y_scores = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = roc_curve(y_true, y_scores)
print(fpr, tpr, thresholds)
easy
A. array([0. , 0.5, 0.5, 1. ]), array([0. , 0. , 1. , 1. ]), array([1.8, 0.8, 0.4, 0.1])
B. array([0. , 0. , 0.5, 1. ]), array([0. , 0.5, 1. , 1. ]), array([0.8, 0.4, 0.35, 0.1])
C. array([0. , 0.5, 1. ]), array([0. , 0.5, 1. ]), array([0.8, 0.35, 0.1])
D. array([0. , 0.5, 1. ]), array([0. , 1. , 1. ]), array([0.8, 0.35, 0.1])

Solution

  1. Step 1: Understand ROC curve output

    The ROC curve function returns false positive rates (fpr), true positive rates (tpr), and thresholds.
  2. Step 2: Analyze the code

    Given the scores and true labels, the ROC curve is calculated correctly, resulting in fpr, tpr, and thresholds as in array([0. , 0.5, 1. ]), array([0. , 0.5, 1. ]), array([0.8, 0.35, 0.1]).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    ROC curve outputs fpr, tpr, thresholds [OK]
Hint: ROC curve outputs fpr, tpr, thresholds [OK]
Common Mistakes:
2. What is the time complexity of updating the embeddings for a single word pair in the Skip-Gram model with negative sampling?
easy
A. O(k)
B. O(vocab_size)
C. O(n)
D. O(1)

Solution

  1. Step 1: Identify the operations involved

    Updating embeddings involves computing gradients and updating weights for the target word and k negative samples.
  2. Step 2: Determine the complexity

    Each update involves a constant number of operations per sample, leading to O(k) complexity where k is the number of negative samples.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Complexity is O(k) due to k negative samples [OK]
Hint: Complexity is O(k) due to k negative samples [OK]
Common Mistakes:
3. You implemented a 2D convolution but the output is all zeros. What is the most likely cause?
medium
A. The kernel is not normalized.
B. The kernel is filled with zeros.
C. The image is padded incorrectly.
D. The stride is set too high.

Solution

  1. Step 1: Identify the zero output cause

    If the kernel is filled with zeros, every convolution operation will result in zero, regardless of the image content.
  2. Step 2: Eliminate wrong options

    The kernel is not normalized. affects scaling, not zero output. The image is padded incorrectly. affects boundary handling, not zero output. The stride is set too high. affects output size, not zero output.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Zero kernel leads to zero output. [OK]
Hint: Zero kernel leads to zero output. [OK]
Common Mistakes:
4.
import numpy as np

# Backpropagation for a single neuron
inputs = np.array([1.0, 2.0, 3.0])
weights = np.array([0.2, 0.8, -0.5])
bias = 2.0
learning_rate = 0.01

d_output = np.array([1.0])  # Gradient of loss w.r.t output

# Incorrect gradient calculation
weights_gradient = d_output * inputs
weights -= learning_rate * weights_gradient
print(weights)
medium
A. Weights are updated correctly.
B. Weights are updated incorrectly due to incorrect learning rate.
C. Weights are not updated; learning rate is too small.
D. Weights are updated incorrectly due to missing chain rule.

Solution

  1. Step 1: Identify error in gradient calculation

    The code incorrectly applies the gradient directly without considering the chain rule.
  2. Step 2: Correct approach

    Should multiply by the derivative of the activation function.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Always apply chain rule in backpropagation [OK]
Hint: Always apply chain rule in backpropagation [OK]
Common Mistakes:
5. How would you modify the scaled dot-product attention mechanism to handle variable-length sequences efficiently?
medium
A. Use padding and mask the padded values.
B. Use a fixed-size window for all sequences.
C. Truncate all sequences to the same length.
D. Ignore sequences longer than a threshold.

Solution

  1. Step 1: Identify the challenge

    Variable-length sequences require handling of different sequence lengths.
  2. Step 2: Choose the correct approach

    Padding with masking is efficient and maintains sequence integrity.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Pad and mask for variable lengths [OK]
Hint: Pad and mask for variable lengths [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard