Bird
Raised Fist0

Overfitting and Underfitting - Diagnosing and Fixing

THEORYbeginnerInterview freq: very_high
Choose your preparation mode6 modes available
β–Ά
Execution Trace
Step 1/5
Model Complexity

Model complexity increases, moving from left (simple) to right (complex).

🎯 Understanding how model complexity affects bias and variance is crucial.
from sklearn.linear_model import LinearRegression
model = LinearRegression()
# Simple model with high bias
Underfitting

Training error is high due to the model's inability to capture data patterns.

🎯 Recognizing underfitting helps in adjusting model complexity.
model.fit(X_train, y_train)
train_error = mean_squared_error(y_train, model.predict(X_train))
# High training error indicates underfitting
Overfitting

Training error is low, but test error is high, indicating overfitting.

🎯 Overfitting can be diagnosed by comparing training and test errors.
from sklearn.tree import DecisionTreeRegressor
complex_model = DecisionTreeRegressor(max_depth=None)
complex_model.fit(X_train, y_train)
test_error = mean_squared_error(y_test, complex_model.predict(X_test))
# High test error indicates overfitting
High Bias

Model predictions are consistently off, indicating high bias.

🎯 High bias is a sign of underfitting and requires model complexity increase.
simple_model = LinearRegression()
simple_model.fit(X_train, y_train)
bias_error = mean_squared_error(y_test, simple_model.predict(X_test))
# Consistent prediction error indicates high bias
High Variance

Model predictions vary greatly with different data samples, indicating high variance.

🎯 High variance is a sign of overfitting and requires model complexity reduction.
from sklearn.ensemble import RandomForestRegressor
rf_model = RandomForestRegressor(n_estimators=100)
rf_model.fit(X_train, y_train)
variance_error = np.var([rf_model.predict(X_test) for _ in range(10)])
# High variance in predictions indicates high variance
πŸ“Š
Diagram
Diagram
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       Model Complexity        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Underfittingβ”‚ Overfitting     β”‚
β”‚            β”‚                  β”‚
β”‚ Simple     β”‚ Complex          β”‚
β”‚ Model      β”‚ Model            β”‚
β”‚            β”‚                  β”‚
β”‚ High Bias  β”‚ High Variance    β”‚
β”‚ Low Var    β”‚ Low Bias         β”‚
β”‚            β”‚                  β”‚
β”‚ Training   β”‚ Training         β”‚
β”‚ Error      β”‚ Error            β”‚
β”‚ High       β”‚ Low              β”‚
β”‚            β”‚                  β”‚
β”‚ Test Error β”‚ Test Error       β”‚
β”‚ High       β”‚ High             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • Model Complexity: The degree of complexity in the model's structure
  • Underfitting: Occurs when the model is too simple to capture the data patterns
  • Overfitting: Occurs when the model is too complex and captures noise as patterns
  • High Bias: The model's predictions are consistently off in one direction
  • High Variance: The model's predictions vary greatly with different data samples
πŸ”‘
Key Moments
β–²
Q: Why does increasing model complexity lead to overfitting?
A: Increasing complexity allows the model to capture noise as patterns, reducing training error but increasing test error.
Q: How can you identify underfitting in a model?
A: Underfitting is identified by high training error, indicating the model is too simple to capture data patterns.
Q: What is the relationship between bias and variance in model performance?
A: Bias and variance are inversely related; reducing bias often increases variance and vice versa, affecting model generalization.

This diagram and trace explain the concepts of overfitting and underfitting in machine learning models. Overfitting occurs when a model is too complex, capturing noise as patterns, leading to low training error but high test error. Underfitting happens when a model is too simple, resulting in high training and test errors due to its inability to capture data patterns. The balance between model complexity, bias, and variance is crucial for optimal model performance. Understanding these concepts helps in diagnosing and fixing model issues, ensuring better generalization to unseen data.

Primary Focus

βœ“ Understanding the mechanisms of overfitting and underfitting in ML models

Practice

(1/5)
1. In the context of backpropagation, which of the following best describes the role of the chain rule?
easy
A. It updates the weights directly without considering the gradient.
B. It calculates the gradient of the loss function with respect to each weight.
C. It prevents the vanishing gradient problem by normalizing gradients.
D. It ensures that the learning rate is adaptive for each layer.

Solution

  1. Step 1: Understand the chain rule in backpropagation

    The chain rule is used to compute the gradient of the loss function with respect to each weight by propagating gradients backward through the network.
  2. Step 2: Eliminate wrong options

    It updates the weights directly without considering the gradient. is incorrect because the chain rule involves gradients. It prevents the vanishing gradient problem by normalizing gradients. is incorrect because it doesn't address vanishing gradients directly. It ensures that the learning rate is adaptive for each layer. is incorrect because the chain rule is not about adaptive learning rates.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Chain rule computes gradients for weights. [OK]
Hint: Chain rule computes gradients for weights. [OK]
Common Mistakes:
2. Given the following Python code, what is the output when evaluating the calibration of a model using Brier Score?
from sklearn.metrics import brier_score_loss
true_labels = [0, 1, 1, 0]
predicted_probs = [0.1, 0.9, 0.8, 0.4]
score = brier_score_loss(true_labels, predicted_probs)
print(score)
easy
A. 0.35
B. 0.15
C. 0.05
D. 0.25

Solution

  1. Step 1: Understand Brier Score calculation.

    Brier Score is the mean squared difference between predicted probabilities and actual outcomes.
  2. Step 2: Calculate the score.

    For each pair: (0.1-0)^2, (0.9-1)^2, (0.8-1)^2, (0.4-0)^2. Mean = (0.01 + 0.01 + 0.04 + 0.16) / 4 = 0.055.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Brier Score = mean squared error of probabilities. [OK]
Hint: Brier Score = mean squared error of probabilities. [OK]
Common Mistakes:
3. Given the following Python code snippet using a pre-trained YOLO model, what will be the output shape of the detection results?
import torch
from torchvision.models.detection import yolo_v3

model = yolo_v3(pretrained=True)
model.eval()

input_tensor = torch.rand(1, 3, 416, 416)
output = model(input_tensor)
print(output[0]['boxes'].shape)
easy
A. (1, 4)
B. (N, 4)
C. (N, 5)
D. (1, 5)

Solution

  1. Step 1: Understand YOLO output

    YOLO outputs a list of detections, each with a bounding box of 4 coordinates.
  2. Step 2: Determine output shape

    The shape is (N, 4) where N is the number of detected objects.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    YOLO boxes = (N, 4) [OK]
Hint: YOLO boxes = (N, 4) [OK]
Common Mistakes:
4. A model trained with Adam optimizer is not converging. What is the most likely cause?
medium
A. Batch size is too large
B. Learning rate is too high
C. Adam is not suitable for non-convex problems
D. Weight decay is too low

Solution

  1. Step 1: Identify common issues with Adam.

    Adam can be sensitive to high learning rates, causing divergence.
  2. Step 2: Eliminate less likely causes.

    Adam handles non-convex problems well, and batch size/weight decay are less likely to cause non-convergence.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    High lr can cause Adam to diverge. [check]
Hint: High lr can cause Adam to diverge. [check]
Common Mistakes:
5. In a system where data distribution shifts gradually over time, how should you design the learning process?
hard
A. Use continual learning without any additional mechanisms.
B. Use periodic retraining with a fixed schedule regardless of data shifts.
C. Use continual learning with a mechanism to detect and adapt to shifts.
D. Use periodic retraining only when performance drops significantly.

Solution

  1. Step 1: Consider the nature of gradual shifts.

    Gradual shifts require a system that can adapt over time without waiting for performance to drop significantly.
  2. Step 2: Evaluate the options.

    Use periodic retraining with a fixed schedule regardless of data shifts. is incorrect because a fixed schedule may not align with shifts. Use continual learning without any additional mechanisms. is incorrect because continual learning alone may not handle shifts effectively. Use periodic retraining only when performance drops significantly. is incorrect because waiting for performance drops can lead to suboptimal results.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Adapt to shifts with detection. [OK]
Hint: Adapt to shifts with detection. [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