Bird
Raised Fist0

Scaling Snafu

CODINGbeginnerInterview freq: high
Choose your preparation mode6 modes available
🐛
Scaling Snafu
Started occurring after the last data pipeline update, persisting for 3 days.

A recommendation system for an e-commerce platform, processing 500k user interactions per day.

Symptom
Model training fails with NaN values in loss after recent data pipeline update.
📡
Signals
metric
Training loss becomes NaN after a few epochs.
Loss is NaN starting from epoch 3.
log
Logs show warnings about division by zero during feature scaling.
Warning: divide by zero encountered in true_divide
distribution
Feature distribution shows zero variance for some features.
Feature 'user_age' has zero variance in the latest batch.
code
Recent changes in the feature scaling code.
Updated feature scaling to include new features without variance checks.
🔬
Hypotheses
Feature scaling is causing division by zero due to zero variance features.
Logs show warnings about division by zero during feature scaling.Feature distribution shows zero variance for some features.
Verify: Check if any features have zero variance before scaling.
import numpy as np
features = np.array([...])
zero_variance_features = np.where(np.var(features, axis=0) == 0)[0]
print('Zero variance features:', zero_variance_features)
Data pipeline update introduced features with constant values.
Feature distribution shows zero variance for some features.Recent changes in the feature scaling code.
Verify: Review the data pipeline changes for introduction of constant features.
pipeline_changes = 'Check recent commits for data pipeline updates'
print(pipeline_changes)
🎯
Root Cause
Feature scaling was applied without checking for zero variance, leading to division by zero.

The data pipeline update introduced new features without variance checks, causing scaling issues.

🔧
Fix Strategy
ImmediateDisable the new features in the data pipeline to prevent NaN values.
Short-termAdd variance checks before scaling to handle zero variance features.
Long-termImplement automated checks in the data pipeline to ensure feature validity before scaling.
🐍
Python Fix
Python
import numpy as np

def scale_features(features):
    variances = np.var(features, axis=0)
    non_zero_var_indices = variances != 0
    scaled_features = np.zeros_like(features)
    scaled_features[:, non_zero_var_indices] = (features[:, non_zero_var_indices] - np.mean(features[:, non_zero_var_indices], axis=0)) / np.std(features[:, non_zero_var_indices], axis=0)
    return scaled_features
✗
Common Wrong Diagnoses
▼ Show
The model architecture is faulty, causing NaN values.
Why candidates think this: NaN values in loss often suggest issues with model computations.
Why wrong: The logs specifically indicate division by zero during feature scaling, not model computation errors.
The data contains NaN values leading to NaN loss.
Why candidates think this: NaN values in data can propagate to loss calculations.
Why wrong: The issue is with division by zero during scaling, not NaN values in the input data.
📊
Monitoring to Add
  • Monitor feature variance distribution in the data pipeline.
  • Set alerts for NaN values in training metrics.
  • Log warnings for any division by zero encountered during scaling.
📋
Process Change

Introduce a mandatory review of feature statistics before deploying data pipeline changes.

🏗️
Architectural Change

Design the data pipeline to automatically exclude or handle features with zero variance.

💡
Key Insight
Always check for zero variance features before applying scaling to prevent division by zero errors.

Practice

(1/5)
1.
import numpy as np
from sklearn.metrics import confusion_matrix

y_true = [0, 1, 0, 1, 0, 1, 1]
y_pred = [0, 0, 0, 1, 1, 1, 1]

cm = confusion_matrix(y_true, y_pred)
print(cm)
What does the code output?
easy
A. [[2, 1], [1, 3]]
B. [[3, 1], [1, 2]]
C. [[2, 2], [1, 2]]
D. [[3, 0], [2, 2]]

Solution

  1. Step 1: Understand confusion matrix structure

    The confusion matrix is structured as [[TN, FP], [FN, TP]].
  2. Step 2: Calculate values

    TN=2 (true negatives), FP=1 (false positives), FN=1 (false negatives), TP=3 (true positives).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Confusion matrix is [[TN, FP], [FN, TP]]. [OK]
Hint: Confusion matrix is [[TN, FP], [FN, TP]]. [OK]
Common Mistakes:
2. What is the time complexity of calculating the Euclidean distance between two vectors of length n?
easy
A. O(n^2)
B. O(1)
C. O(n log n)
D. O(n)

Solution

  1. Step 1: Understand Euclidean distance calculation

    Euclidean distance involves element-wise operations and a square root.
  2. Step 2: Analyze operations

    Each element is processed once, leading to O(n) complexity.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Element-wise operations imply O(n). [OK]
Hint: Element-wise operations imply O(n). [OK]
Common Mistakes:
3. What does the following Python code snippet output when implementing a simple bagging technique for a Random Forest?
import numpy as np
from sklearn.utils import resample

X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])

# Bagging
bagged_X = resample(X, n_samples=3, replace=True, random_state=42)
print(bagged_X)
easy
A. An array with 3 samples, possibly with duplicates, from X
B. An array with 3 unique samples from X
C. An array with 4 samples, each appearing once
D. An array with 2 samples, each appearing twice

Solution

  1. Step 1: Understand Bagging with Replacement

    Bagging involves sampling with replacement, meaning samples can be repeated.
  2. Step 2: Analyze Code Output

    The code uses resample with replace=True, so duplicates are possible.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Bagging allows duplicates [OK]
Hint: Bagging allows duplicates [OK]
Common Mistakes:
4. What is the time complexity of performing a train-test split with stratification on a dataset with n samples?
easy
A. O(n)
B. O(n^2)
C. O(n log n)
D. O(log n)

Solution

  1. Step 1: Analyze the operation

    Train-test split with stratification involves iterating over the dataset once to ensure class distribution.
  2. Step 2: Determine complexity

    Since each element is processed once, the complexity is O(n).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Stratified split processes each sample once. [OK]
Hint: Stratified split processes each sample once. [OK]
Common Mistakes:
5. In the context of implementing backpropagation using NumPy, which of the following is a correct approach to handle vanishing gradients?
medium
A. Use a smaller learning rate.
B. Switch to a sigmoid activation function.
C. Implement gradient clipping.
D. Increase the number of layers in the network.

Solution

  1. Step 1: Identify vanishing gradient issue

    Vanishing gradients occur when gradients become too small to update weights effectively.
  2. Step 2: Correct approach

    Gradient clipping prevents gradients from becoming too small or too large.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Clip gradients to prevent vanishing [OK]
Hint: Clip gradients to prevent vanishing [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