Bird
Raised Fist0

The Diverging Descent

CODINGadvancedInterview freq: very_high
Choose your preparation mode6 modes available
🐛
The Diverging Descent
This issue started occurring after a recent update to the training script last week.

A neural network model for predicting user engagement on a social media platform, trained using Mini-Batch SGD with Momentum and Adam optimizers.

Symptom
The model's training loss starts to increase after a few epochs, and the validation accuracy drops significantly.
📡
Signals
metric
Training loss over epochs
Loss decreases initially but starts increasing after epoch 5.
metric
Validation accuracy over epochs
Accuracy drops from 0.85 to 0.60 after epoch 5.
log
Optimizer configuration logs
Momentum parameter set to 0.99, learning rate set to 0.001.
distribution
Gradient norms during training
Gradient norms spike significantly after epoch 5.
code
Recent changes in the training script
Switched from SGD to Adam optimizer without adjusting hyperparameters.
🔬
Hypotheses
The learning rate is too high for the Adam optimizer.
Training loss over epochsGradient norms during training
Verify: Check if reducing the learning rate stabilizes the training.
import matplotlib.pyplot as plt
learning_rates = [0.001, 0.0005, 0.0001]
losses = []
for lr in learning_rates:
    # Assume train_model returns the final loss
    loss = train_model(learning_rate=lr)
    losses.append(loss)
plt.plot(learning_rates, losses)
plt.xlabel('Learning Rate')
plt.ylabel('Final Loss')
plt.title('Loss vs Learning Rate')
plt.show()
Momentum parameter is too high, causing instability.
Optimizer configuration logsTraining loss over epochs
Verify: Experiment with lower momentum values and observe the effect on training stability.
for momentum in [0.9, 0.95, 0.99]:
    train_model(momentum=momentum)
🎯
Root Cause
The momentum parameter was set too high for the Adam optimizer, causing gradient updates to overshoot.

A recent update to the training script changed the optimizer without adjusting the momentum parameter appropriately.

🔧
Fix Strategy
ImmediateRollback to the previous optimizer configuration with known stable parameters.
Short-termAdjust the momentum parameter to a lower value (e.g., 0.9) and monitor training stability.
Long-termImplement a hyperparameter tuning process to automatically adjust optimizer settings based on training feedback.
🐍
Python Fix
Python
optimizer = torch.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.999))
✗
Common Wrong Diagnoses
▼ Show
The model architecture is flawed, causing poor generalization.
Why candidates think this: The drop in validation accuracy suggests overfitting or architectural issues.
Why wrong: The issue was resolved by adjusting optimizer parameters, not changing the model architecture.
The training data has become corrupted or biased.
Why candidates think this: A sudden drop in accuracy could indicate data issues.
Why wrong: Data remained unchanged; the problem was with optimizer settings.
📊
Monitoring to Add
  • Track gradient norms and alert if they exceed a threshold.
  • Monitor training and validation loss trends for divergence.
  • Log optimizer configuration changes and their impact on training metrics.
📋
Process Change

Introduce a review process for changes in optimizer configurations to ensure appropriate hyperparameter adjustments.

🏗️
Architectural Change

Develop a configuration management system that automatically suggests hyperparameter adjustments based on historical training data.

💡
Key Insight
Optimizer hyperparameters must be carefully tuned when switching algorithms to prevent training instability.

Practice

(1/5)
1. What is the time complexity of cropping an image of size n x n to a smaller size m x m using NumPy slicing?
easy
A. O(n^2)
B. O(1)
C. O(n*m)
D. O(m^2)

Solution

  1. Step 1: Understand NumPy slicing

    NumPy slicing creates a view, not a copy, so it operates in constant time.
  2. Step 2: Evaluate the complexity

    The operation does not depend on the size of the arrays but on the slicing indices.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    NumPy slicing is O(1) due to views. [OK]
Hint: NumPy slicing is O(1) due to views. [OK]
Common Mistakes:
2.
import numpy as np

def numerical_gradient(f, x):
    h = 1e-4
    grad = np.zeros_like(x)
    for i in range(x.size):
        x[i] += h
        fxh1 = f(x)
        x[i] -= 2 * h
        fxh2 = f(x)
        grad[i] = (fxh1 - fxh2) / (2 * h)
        x[i] += h
    return grad

# Test function
f = lambda x: x[0]**2 + x[1]**2
x = np.array([3.0, 4.0])
print(numerical_gradient(f, x))
easy
A. [6.0, 8.0]
B. [6.0001, 8.0001]
C. [5.9999, 7.9999]
D. [0.0, 0.0]

Solution

  1. Step 1: Understand the numerical gradient calculation

    The function calculates the gradient using central difference approximation.
  2. Step 2: Calculate the gradient for each element

    For x = [3.0, 4.0], the gradient is [6.0, 8.0] because the derivative of x^2 is 2x.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Central difference gives exact gradient for quadratic. [OK]
Hint: Central difference gives exact gradient for quadratic. [OK]
Common Mistakes:
3. In the context of implementing Skip-Gram with negative sampling, which approach is best for handling rare words?
medium
A. Use subsampling of frequent words.
B. Increase the number of negative samples.
C. Increase the context window size.
D. Use a higher learning rate for rare words.

Solution

  1. Step 1: Understand the problem with rare words

    Rare words can be overshadowed by frequent words, leading to poor embeddings.
  2. Step 2: Evaluate solutions

    Subsampling frequent words reduces their dominance, allowing rare words to have more influence during training.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Subsampling frequent words helps rare words [OK]
Hint: Subsampling frequent words helps rare words [OK]
Common Mistakes:
4. How can you optimize the memory usage of a Min-Max scaling implementation for a large dataset?
hard
A. Process data in chunks instead of loading all at once
B. Use float64 data type for higher precision
C. Use a single loop to calculate min and max
D. Store scaled data in a new array to avoid overwriting

Solution

  1. Step 1: Identify Memory Bottlenecks

    Loading large datasets entirely into memory can be inefficient.
  2. Step 2: Optimize Memory Usage

    Processing data in chunks reduces memory footprint, making it feasible to handle large datasets.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Chunk processing saves memory. [OK]
Hint: Chunk processing saves memory. [OK]
Common Mistakes:
5. How can you optimize the memory usage of a Random Forest implementation with bagging and feature sampling?
hard
A. Use a single decision tree instead of a forest
B. Use integer data types for feature values
C. Reduce the number of features sampled per tree
D. Store trees as serialized objects on disk

Solution

  1. Step 1: Identify Memory Optimization Techniques

    Using smaller data types can significantly reduce memory usage.
  2. Step 2: Evaluate Options

    Using a single tree (A) reduces model complexity, not memory; reducing features (B) affects accuracy; serialization (D) affects storage, not runtime memory.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Smaller data types save memory [OK]
Hint: Smaller data types save memory [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