Skip to content
Machine Learning & Data Science

Cost Function interview questions and answers

43 questions, 18 with code. About 4 h 11 min to read them all.

By TestLab · Updated

Plan for an interview
43 questions

Track what you know and what to revise.

Sign in to track progress

You can open a few full answers without signing in. Sign in to read all

1

How does a cost function work in machine learning?

A cost function (also called an objective function) measures how far a model’s predictions are from the true targets by aggregating a per-example loss over the dataset, often plus a regularization term. It turns training into an optimization problem: gradient-based algorithms adjust parameters to minimize this cost. The main trade-off is choosing a function that is smooth enough for stable optimization while reflecting which errors matter most, such as sensitivity to large outliers versus robustness.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12)

Loss, Cost, and Differentiability

  • Loss: The error for a single example, for example loss_i = (y_i - y_hat_i)^2, where y_i is the true target and y_hat_i is the model prediction for example i.

  • Cost / objective: The quantity optimized during training, typically the average loss over n examples plus an optional regularization term: J(w) = (1/n) * sum(loss_i) + regularization(w), where w represents model parameters and n is the number of examples.

  • How optimization works: During each training step, the model makes predictions, the cost is computed, and parameters are updated in the direction that reduces the cost. For gradient-based methods (gradient descent, Adam), the update is w := w - lr * dJ/dw, where lr is the learning rate and dJ/dw is the gradient of the cost with respect to w.

  • Why differentiable: If the cost is not differentiable (or provides uninformative gradients), gradient-based updates cannot be computed reliably, which slows or prevents learning.

Common Cost Functions

Cost function Formula (plain notation) When to use Notes
MSE (Mean Squared Error) MSE = (1/n) * sum((y_i - y_hat_i)^2) Regression with roughly Gaussian errors Penalizes large errors more because of squaring; sensitive to outliers.
MAE (Mean Absolute Error) ‘MAE = (1/n) * sum( y_i - y_hat_i )‘ Regression with outliers Robust to outliers; gradient magnitude is constant away from zero.
Huber loss Quadratic when ‘ y_i - y_hat_i <= delta, linear otherwise (delta` is a threshold) Regression needing a balance Behaves like MSE near zero and MAE for large errors.
Binary cross-entropy (log loss) BCE = -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)), where p_i is the predicted probability of class 1 Binary classification (often with sigmoid) Logistic regression uses cross-entropy instead of MSE because, with a sigmoid link, the objective is convex in the weights and provides stronger, more useful gradients when predictions are confidently wrong.
Categorical cross-entropy CCE = -(1/n) * sum(sum(y_{i,c} * log(p_{i,c}))) over classes c, with p_{i,c} from softmax Multi-class, one-label classification y_{i,c} is 1 for the true class and 0 otherwise.
Hinge loss Hinge = (1/n) * sum(max(0, 1 - y_i * s_i)), where y_i is in {-1, +1} and s_i is the decision score Support Vector Machines Encourages a margin; zero loss once an example is correctly classified beyond the margin.

| KL divergence | KL(P || Q) = sum(P(x) * log(P(x) / Q(x))) over outcomes x | Comparing distributions, distillation, variational models | Measures how distribution Q diverges from target distribution P. |

Regularized Cost Functions

  • L2 (Ridge / weight decay): Adds lambda * sum(w_j^2) over weights w_j. It shrinks weights smoothly and improves stability when features are correlated.

  • L1 (Lasso): Adds lambda * sum(|w_j|). It can drive some weights exactly to zero, producing sparse models useful for feature selection.

  • lambda: The regularization strength; larger values penalize complexity more and can reduce overfitting but may underfit.

Common Pitfalls and Interview Tips

  • Numerical instability: log(0) produces infinities in cross-entropy. Predictions should be clipped away from 0 and 1, or a stable logits-based loss such as torch.nn.BCEWithLogitsLoss in PyTorch 2.x should be used.

  • Training loss vs. evaluation metric: You may train on log loss but report F1, ROC-AUC, or PR-AUC for imbalanced classes. The metric should match the business decision, not just the optimizer.

  • Wrong cost for the problem: MSE on probabilities can optimize poorly for classification; MAE or Huber is often better than MSE when outliers dominate regression.

  • Preprocessing leakage: Fit scalers/encoders on training data only, typically inside an sklearn.pipeline.Pipeline.

  • Interview follow-ups: “Why is cross-entropy convex for logistic regression but MSE is not with a sigmoid?”, “How does L1 create sparsity?”, and “When would you choose Huber over MSE or MAE?”

2

How do cost functions differ from loss functions?

A loss function measures the error for one training example, while a cost function is the aggregate training objective, usually averaging losses across the dataset and optionally adding regularization. In regularized training, the main trade-off is fitting the training data well versus keeping the model simple enough to generalize. Some frameworks use “loss” and “cost” interchangeably, so context matters.

Read full answer

Applies tomachine learning concepts

Loss Versus Cost

For a dataset with n examples, a common cost function is:

J(w) = (1/n) * sum(i=1 to n) L_i(w) + lambda * R(w)

  • J(w): overall cost or training objective.

  • w: vector of trainable model parameters.

  • L_i(w): loss for example i.

  • i: example index.

  • n: number of training examples.

  • lambda: regularization strength.

  • R(w): penalty such as an L1 or L2 norm.

The individual loss measures one example’s error, while the cost aggregates those errors over the data. Some libraries use “loss” for the already-averaged objective, so the terminology is not completely standardized.

Common Cost Functions

In the formulas below, y_i is the true value, y_hat_i is the predicted value, and e_i = y_i - y_hat_i is the error.

Function Formula When to use it
Mean squared error MSE = (1/n) * sum((y_i - y_hat_i)^2) Regression; strongly penalizes large errors and provides smooth gradients.
Mean absolute error MAE = (1/n) * sum(abs(y_i - y_hat_i)) Regression with outliers; less sensitive to extreme errors.
Huber loss Huber(e_i) = e_i^2 if abs(e_i) <= delta, otherwise delta * (abs(e_i) - 0.5 * delta) Regression that behaves like MSE near zero and MAE farther away; delta is the nonnegative transition threshold.
Binary cross-entropy BCE = -(1/n) * sum(y_i * log(p_i) + (1-y_i) * log(1-p_i)) Binary classification; p_i is the predicted probability of the positive class.
Categorical cross-entropy CE = -(1/n) * sum(i) sum(c) y_ic * log(q_ic) Multiclass classification; y_ic is the true one-hot label, q_ic is the predicted softmax probability, and c is the class index.
Hinge loss (1/n) * sum(max(0, 1 - y_i * f(x_i))) Margin-based models such as linear SVMs; y_i is -1 or +1, f(x_i) is the decision score, and x_i contains the input features for example i.
KL divergence KL(r, s) = sum(c) r_c * log(r_c / s_c) Comparing distributions, such as in knowledge distillation or variational models; r_c and s_c are components of the target and predicted distributions, respectively.

Regularization and Differentiability

  • L2 regularization: Adds lambda * sum(j) w_j^2, where j indexes model parameters. It is associated with ridge regression and commonly implemented as weight decay; it discourages large weights and can reduce overfitting.

  • L1 regularization: Adds lambda * sum(j) abs(w_j). It is associated with lasso regression and can drive weights exactly to zero, producing sparse models.

  • The intercept is commonly excluded from regularization because shrinking it does not improve model flexibility.

  • Gradient-based training generally requires the objective to be differentiable at the relevant parameter values. MAE is not differentiable at zero, but optimization frameworks can use a subgradient at that point; Huber loss is a smooth alternative. Accuracy and F1 are nondifferentiable metrics, so training generally uses a smooth surrogate loss.

  • Logistic regression normally uses binary cross-entropy because, for the linear logistic model, it is convex in the weights and avoids the vanishing sigmoid factor that MSE acquires on sigmoid outputs. Binary cross-entropy has derivative sigmoid(z) - y with respect to the logit z, whereas MSE includes the additional factor sigmoid(z) * (1 - sigmoid(z)).

Common Pitfalls

  • Confusing loss with cost: Loss usually describes an individual error; cost describes the aggregate training objective.

  • Optimizing the wrong objective: A model may train with cross-entropy while F1 is the business-relevant evaluation metric. Training loss and reported metrics need not be identical.

  • Choosing unsuitable metrics: Accuracy can mislead with imbalanced classes; consider precision, recall, F1, ROC-AUC, or PR-AUC when appropriate.

  • Numerical instability: Clipping probabilities before logarithms prevents log(0). In production PyTorch code, prefer torch.nn.BCEWithLogitsLoss, which combines sigmoid and cross-entropy in a numerically stable operation.

  • Data leakage: Fit preprocessing only on training folds, never on validation or test data.

  • Poor scaling choices: Scaling often improves gradient-descent convergence but is unnecessary for tree-based models.

  • Ignoring generalization: A decreasing training cost does not guarantee better validation performance.

Common interview follow-ups include: “Why does logistic regression use cross-entropy instead of MSE?”, “How does L1 produce sparse weights?”, and “When is MAE preferable to MSE?”

3

What is a cost function in model training?

A cost function defines the mathematical objective a model tries to optimize during training, usually by minimizing an error measure such as squared error or cross-entropy. A loss usually measures error for one example, while the cost aggregates those losses across the data and may include regularization. The main trade-off is choosing an objective that reflects the intended behavior while avoiding excessive sensitivity to outliers or poorly behaved gradients.

Read full answer

Applies tomachine learning concepts (Python 3.12)

Loss, Cost, and Objective

  • Loss: Error associated with an individual prediction. For regression, an example’s loss might be (y - y_hat)^2.

  • Cost function: The sum or average of example losses over the training data.

  • Objective function: The complete quantity optimized during training, usually the cost plus any regularization penalty.

Gradient-based training relies on derivatives of the objective. A non-differentiable objective can sometimes still be optimized using subgradients, smoothing, or a reparameterization. MAE, for example, is non-differentiable at zero error but can be minimized with subgradient methods.

Common Cost Functions

The notation below uses n examples, observed values y, predictions y_hat, and model weights w.

  • Mean Squared Error (MSE): MSE = (1/n) * sum((y - y_hat)^2)
    Here, 1/n averages the squared differences. Squaring penalizes large errors heavily, making MSE suitable for regression but sensitive to outliers.

  • Mean Absolute Error (MAE): MAE = (1/n) * sum(|y - y_hat|)
    This averages absolute errors and is generally more robust to outliers than MSE, although its derivative is less smooth around zero.

  • Huber loss: For error e = y - y_hat, it uses 0.5 * e^2 for small errors and delta * (|e| - 0.5 * delta) for large errors. The threshold delta determines the transition, combining MSE’s smoothness with MAE’s outlier robustness.

  • Binary cross-entropy: BCE = -(1/n) * sum(y * log(p) + (1 - y) * log(1 - p))
    Here, p is the predicted probability of the positive class. It is used for binary classification and for independent multilabel outputs because it evaluates predicted probabilities.

  • Categorical cross-entropy: CCE = -(1/n) * sum(sum_k(y_k * log(p_k)))
    For each example, y_k is the true probability for class k, and p_k is the predicted probability, usually produced by softmax.

  • Hinge loss: Hinge = (1/n) * sum(max(0, 1 - y * f(x)))
    Here, f(x) is the raw model score and y is -1 or 1. SVMs use it to encourage correct classifications to have a margin of at least one.

  • KL divergence: KL(P || Q) = sum(P(x) * log(P(x) / Q(x)))
    It measures how distribution P differs from reference distribution Q. Cross-entropy satisfies H(P, Q) = KL(P || Q) + H(P), where H(P) is constant with respect to the predictions Q.

Regularization and Logistic Regression

Regularization adds a penalty to the cost:

  • L2 regularization: J = Cost + lambda * sum(w_j^2)

  • L1 regularization: J = Cost + lambda * sum(|w_j|)

Here, lambda controls penalty strength, w_j is weight j, and j indexes the weights. L2 regularization is used in ridge regression and is closely related to weight decay; it discourages large weights. L1 regularization is used in lasso and can drive some weights exactly to zero, producing sparse models.

Logistic regression usually uses binary cross-entropy rather than MSE. Cross-entropy is convex in the weights, matches the Bernoulli likelihood, and provides better gradients. MSE applied directly to sigmoid probabilities is generally non-convex and includes a p * (1 - p) factor in its logit gradient, which can vanish for confident incorrect predictions. Cross-entropy instead has a logit gradient of p - y.

Choosing and Evaluating the Objective

The training objective does not have to be the evaluation metric. A classifier may be trained with log loss while being evaluated with F1 when class imbalance or a specific business threshold makes F1 more relevant.

Objective Strength Weakness Typical use
MSE Strongly penalizes large errors Sensitive to outliers Regression
MAE Robust to outliers Less smooth at zero Regression with outliers
Huber loss Balances smoothness and robustness Requires choosing delta Regression with uncertain outliers
Cross-entropy Rewards confident correct probabilities Sensitive to incorrect overconfidence Classification
Hinge loss Directly optimizes a margin Less probabilistic SVM-style classification

Common Pitfalls

  • Objective-metric mismatch: Optimizing log loss does not guarantee the best F1 score, precision, or recall.

  • Wrong metric for imbalanced data: Accuracy can appear strong while minority classes are poorly detected; use precision, recall, F1, ROC-AUC, or PR-AUC as appropriate.

  • Numerical instability: Probability-based cross-entropy can suffer from log(0) or extreme gradients. In PyTorch, use torch.nn.BCEWithLogitsLoss for stable logit-based binary classification.

  • Data leakage: Fit preprocessing only on training data or within each cross-validation fold using a pipeline.

  • Overfitting the validation set: Repeated tuning against the same validation data makes its result optimistic. Use cross-validation and reserve a final untouched test set.

4

What makes a cost function effective for machine learning?

A cost function quantifies how undesirable a model’s predictions are relative to the observed targets. An effective cost aligns with the task’s real objective while producing useful optimization signals and stable numerical behavior. Its main trade-off is task fidelity versus optimization-friendly properties such as smoothness, robustness, conditioning, and computational cost.

Read full answer

Applies tomachine learning concepts (all versions); PyTorch 2.x for `torch.nn.BCEWithLogitsLoss`

Effective Characteristics

  • Task alignment: The cost should emphasize errors that matter for the intended use. For imbalanced classification, log loss can expose confidently wrong minority-class predictions that accuracy ignores.

  • Useful gradients: Gradient-based training requires a differentiable cost whose gradient indicates how changing parameters affects the objective. Not every learner needs differentiability; tree models commonly use non-differentiable split criteria.

  • Stable aggregation: Averaging per-example losses prevents the objective from growing merely because the dataset contains more examples.

  • Numerical stability: The implementation should avoid undefined operations such as log(0).

  • Appropriate robustness: The cost should not allow a small number of extreme errors to dominate unless that sensitivity is intentional.

  • Optimization behavior: Smoothness, useful curvature, and convexity can improve optimization, although many effective neural-network objectives are non-convex.

  • Regularization compatibility: The objective should support L1 or L2 penalties when overfitting must be controlled.

  • Metric distinction: The training cost need not equal the final evaluation metric, but their differences should be intentional and understood.

Loss Versus Cost

A loss measures the error for one example. A cost function or objective aggregates those losses and may add regularization:

J(w) = (1/n) * sum_i loss(y_i, prediction_i) + R(w)

Here, J(w) is the total objective, w represents the model parameters, n is the number of examples, i is an example index, y_i is the observed target, prediction_i is the model’s prediction, loss is the per-example loss, and R(w) is the optional regularization penalty.

Common Cost Functions

Cost Formula When to use Main trade-off
Mean Squared Error MSE = (1/n) * sum((y_i - y_hat_i)^2) Regression Strongly penalizes large errors
Mean Absolute Error MAE = (1/n) * sum(abs(y_i - y_hat_i)) Regression with outliers More robust but less smooth at zero
Huber loss Piecewise quadratic/linear function defined below Regression with moderate outliers Balances smoothness and robustness
Binary cross-entropy BCE = -(1/n) * sum(y_i * log(p_i) + (1-y_i) * log(1-p_i)) Binary classification Strongly penalizes confident mistakes
Categorical cross-entropy CE = -(1/n) * sum_i sum_c y_ic * log(p_ic) Multiclass classification Works with softmax probabilities
Hinge loss Hinge = (1/n) * sum_i max(0, 1 - y_i * f(x_i)) SVM-style classification Encourages correct margins

| KL divergence | KL(q || p) = sum_x q(x) * log(q(x) / p(x)) | Distribution matching | Asymmetric and sensitive to zero probabilities |

For regression, y_i is the observed value and y_hat_i is the prediction. For BCE, y_i is either 0 or 1, p_i is the predicted probability, and log is the natural logarithm. For categorical cross-entropy, c indexes classes, y_ic is the one-hot target indicator, and p_ic is the predicted probability for class c. For hinge loss, x_i is an input, f(x_i) is a real-valued model score, and y_i is either -1 or +1. For KL divergence, q(x) and p(x) are probability distributions over the same outcomes indexed by x.

Huber loss is defined with error e = y - y_hat:

H_delta(e) = 0.5 * e^2 when |e| <= delta; otherwise, H_delta(e) = delta * (|e| - 0.5 * delta).

The positive threshold delta determines where the loss changes from quadratic to linear behavior.

Logistic Regression and Regularized Costs

Logistic regression uses binary cross-entropy because it directly measures the likelihood of observed binary labels. Its objective is convex in the weights, whereas least squares applied through a sigmoid is non-convex in the weights and can provide poorly scaled gradients.

  • L2 regularization: J_L2(w) = J(w) + lambda * sum_j(w_j^2). Here, lambda is a nonnegative regularization strength and w_j is parameter j. This penalty is used in ridge regression and commonly implemented as weight decay.

  • L1 regularization: J_L1(w) = J(w) + lambda * sum_j(abs(w_j)). It can drive some parameters exactly to zero, producing a sparse model.

The effective value of lambda depends on the loss scaling, feature scaling, and validation procedure.

Common Pitfalls

  • Wrong objective: Training with log loss but judging only by accuracy can hide minority-class failures; also consider precision, recall, F1, ROC-AUC, and PR-AUC.

  • Confusing loss and metric: A lower training cost does not necessarily mean better business or test performance.

  • Ignoring outliers: MSE may let a few extreme values dominate training; MAE or Huber loss can be more robust.

  • Numerical instability: Compute BCE from logits when possible. PyTorch’s torch.nn.BCEWithLogitsLoss combines sigmoid and BCE without explicitly evaluating log(sigmoid(x)).

  • Poor regularization choices: Feature scale, preprocessing, and the regularization convention affect the effective penalty.

  • Validation leakage: Use validation data or cross-validation for model selection rather than repeatedly tuning against the test set.

Common follow-up questions include: “How would you choose between MSE and MAE?”, “Why does cross-entropy work better than MSE for logistic regression?”, “How would you implement BCE stably?”, and “How does regularization change the effective objective?”

5

Convex vs non-convex cost functions in machine learning

A convex cost function has a single global minimum, so any local minimum is also global. A non-convex cost function may contain local minima, local maxima, or saddle points, making optimization harder and potentially less reproducible. The main trade-off is that convex objectives provide reliable optimization, while non-convex objectives allow more expressive models but do not guarantee finding the best solution.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12)

Convex and Non-Convex Objectives

A loss measures the error for one example, while a cost function or objective aggregates loss across the training data and may include a regularization penalty.

For a differentiable function f(w), convexity means that every Hessian is positive semidefinite throughout the relevant domain:

H(w)[v, v] >= 0 for every w and vector v

Here, H(w) is the matrix of second derivatives, and v is any direction. This condition means the function curves upward and has no interior local minima below its global minimum.

Equivalently, for any two parameter vectors w1 and w2 and any value lambda between 0 and 1:

f(lambda * w1 + (1 - lambda) * w2) <= lambda * f(w1) + (1 - lambda) * f(w2)

The function value at a point between w1 and w2 cannot exceed the corresponding weighted average of the values at the two endpoints. A function is non-convex when it violates this property in at least one region of the domain.

Key Differences

Property Convex cost function Non-convex cost function
Shape Bowl-like or flat with no harmful curvature May have valleys, peaks, and saddle points
Local minima Every local minimum is global Local minima may be worse than the global minimum
Gradient descent Finds the global minimum under suitable convergence conditions May settle at a local minimum or saddle point
Optimization More predictable and generally easier More sensitive to initialization and hyperparameters
Model flexibility Usually limits model complexity Supports flexible models such as deep neural networks
Common examples Linear regression, logistic regression, ridge regression, LASSO Training most deep neural networks, matrix factorization, many clustering objectives

Examples

Linear regression with MSE uses:

MSE(w) = (1/n) * sum((y_i - y_hat_i)^2)

Here, n is the number of examples, y_i is the actual target, y_hat_i is the prediction, and sum adds all squared errors. This objective is convex in linear-regression weights.

Adding L2 regularization produces:

MSE(w) + lambda * sum(w_j^2)

The term lambda controls regularization strength, w_j is the weight at index j, and sum(w_j^2) penalizes large weights. L2 regularization remains convex.

L1 regularization adds lambda * sum(|w_j|). It also preserves convexity and can produce sparse weights, meaning many weights become exactly zero.

Neural-network training usually uses a non-convex objective because weight configurations interact across layers. For example, MSE followed by a nonlinear activation is not generally convex in the network weights. Multiple parameter configurations may achieve similar losses, and optimization may stop at different local minima depending on initialization, learning rate, or data order.

Why Logistic Regression Uses Cross-Entropy

For binary classification, logistic regression applies the sigmoid function to a linear score and optimizes binary cross-entropy:

BCE(w) = -(1/n) * sum(y_i * log(p_i) + (1 - y_i) * log(1 - p_i))

Here, p_i is the predicted probability for example i. The cross-entropy objective is convex in the logistic-regression weights, and its gradients behave better numerically than treating the probabilities as continuous values in an MSE objective. MSE can also penalize confident wrong predictions less effectively during optimization.

Practical Implications and Common Pitfalls

  • Local is not global: A non-convex optimizer reaching a local minimum does not mean the model is incorrectly implemented.

  • Reproducibility matters: Fix random seeds and compare multiple initializations when training non-convex models.

  • Regularization can change curvature: Convex losses remain convex when combined with convex penalties such as L1 or L2; non-convex losses are not generally made convex by regularization.

  • Differentiability is helpful but separate: Convexity describes the objective’s shape, while differentiability determines whether gradient-based optimization is directly applicable.

  • Loss and metric may differ: A classifier may be trained with log loss while being evaluated with F1, especially when class imbalance makes accuracy misleading.

  • Check the domain: A function may be convex over one restricted parameter region and non-convex over the entire domain.

Interview Tips

Common follow-up questions include:

  • Why does convexity guarantee a global minimum?

  • Is every non-convex objective guaranteed to have local minima?

  • Does a positive semidefinite Hessian prove convexity?

  • Why can gradient descent get stuck on a non-convex objective?

  • How does regularization affect convexity?

  • Why is cross-entropy preferred over MSE for logistic regression?

6

Why does convexity matter for cost functions?

Convexity describes a cost function whose graph curves upward so that any line segment between two points on the function lies on or above it, guaranteeing a single global minimum. This property matters because gradient-based optimization can reliably find that minimum, whereas non-convex cost functions may trap training in local minima or saddle points. The main trade-off is that highly expressive models such as deep neural networks have non-convex objectives, so practitioners trade guaranteed global optimality for model capacity and practical optimization techniques.

Read full answer

Applies tomachine learning concepts (optimization examples in NumPy 2.x, Python 3.12)

How Convexity Works

A function J(w) of parameters w is convex if, for any two parameter vectors w1 and w2 and any t in [0, 1]:

J(t * w1 + (1 - t) * w2) <= t * J(w1) + (1 - t) * J(w2)

  • J(w): the cost or objective value for parameters w.

  • w1, w2: any two points in the parameter space.

  • t: a mixing weight between 0 and 1.

For twice-differentiable functions, convexity means the Hessian (matrix of second derivatives) is positive semidefinite everywhere. Intuitively, the cost surface has no “dips” other than the global basin.

  • Convex examples: mean squared error for linear regression, MSE = (1/n) * sum((y_i - y_hat_i)^2) where y_i is the true value, y_hat_i is the prediction, and n is the number of examples; logistic regression with log loss / binary cross-entropy, which is convex in the weights.

  • Non-convex examples: training objectives for deep neural networks, k-means clustering (in the assignment and centroid variables jointly), and matrix factorization.

Why It Matters for Optimization

Gradient descent updates parameters in the direction of the negative gradient: w := w - alpha * grad(J(w)), where alpha is the learning rate and grad(J(w)) is the gradient of the cost with respect to w.

Property Convex cost function Non-convex cost function
Minima One global minimum (or a flat set of global minima) Multiple local minima and saddle points
Gradient descent behavior Converges to a global minimum with suitable alpha (for many convex problems) May converge to a local minimum or stall near saddle points
Solution reliability Same optimum in principle regardless of initialization Initialization and optimizer choices affect the result
Typical models Linear/logistic regression, ridge/lasso, linear SVM Neural networks, decision tree ensembles (training objective perspective)

Practical Implications

  • Algorithm choice: Convex problems can use specialized solvers (coordinate descent for lasso, quadratic programming for SVMs) with strong convergence guarantees.

  • Regularization: Adding L2 regularization, lambda * sum(w_j^2), or L1 regularization, lambda * sum(|w_j|), to a convex loss keeps the objective convex (the sum of convex functions is convex). Here lambda controls regularization strength and w_j are model weights.

  • Initialization: Convex objectives make initialization less critical; non-convex deep learning relies on careful initialization, learning-rate schedules, momentum, and adaptive optimizers.

  • Evaluation vs. training: The training loss may be chosen for convexity or smooth gradients, while the evaluation metric (for example, F1 for imbalanced classification) may differ because it better reflects the business goal.

Interview Tips

  • Common pitfalls: Saying neural network training is “impossible” because it is non-convex (in practice it often finds good solutions), confusing a convex loss with a convex model class, or claiming MSE is always appropriate for logistic regression (it is non-convex in that setting and gives poorer gradients than cross-entropy).

  • Follow-up questions: “Why is binary cross-entropy preferred over MSE for logistic regression?”, “How do L1 and L2 regularization affect the cost surface?”, “What are saddle points, and how do optimizers handle them in deep learning?”, and “When would you choose a convex model over a neural network?”

7

Why is the global minimum of a cost function important?

The global minimum of a cost function is the lowest objective value attainable across all valid parameter values. It identifies the best solution according to the chosen training objective, although reaching it can be difficult for non-convex functions. The main trade-off is that minimizing the training cost does not necessarily minimize validation error or guarantee the best generalization.

Read full answer

Applies tomachine learning cost-function concepts (language-agnostic)

Meaning and Significance

For parameters w and bias b, a typical objective is:

J(w, b) = (1/n) * sum(L(y_i, f(x_i; w, b))) + lambda * R(w)

  • n: number of training examples.

  • x_i: features of example i.

  • y_i: its target value.

  • f(x_i; w, b): the model’s prediction.

  • L: the loss for one example.

  • lambda: regularization strength.

  • R(w): regularization penalty.

  • J: total cost used for optimization.

The global minimum value and its parameters can be written as:

J* = min J(w, b) and w* = argmin J(w, b)

Its significance depends on the objective’s shape:

  • Convex objective: every local minimum is also global, so gradient-based optimization is more likely to find the global minimum.

  • Non-convex objective: multiple local minima and saddle points may exist. Gradient descent may converge to a local minimum rather than the global one.

  • Regularized objective: the global minimum balances fitting the training data against controlling model complexity.

  • Generalization caveat: the global minimum of the training cost is not necessarily the parameter setting with the lowest unseen-data error.

Loss Versus Cost

A loss measures the error for one example, while the cost or objective aggregates losses across the dataset and may add regularization.

Gradient-based training requires the objective to be differentiable, or to admit subgradients, so the optimizer can calculate a direction that reduces the cost. Functions such as MSE and cross-entropy are well suited to this; a non-differentiable loss can still be used when an appropriate subgradient exists.

Common Cost Functions

Cost or loss Formula Typical use
Mean Squared Error MSE = (1/n) * sum((y_i - y_hat_i)^2) Regression; penalizes large errors heavily
Mean Absolute Error ‘MAE = (1/n) * sum( y_i - y_hat_i )‘ Regression; more robust to outliers
Huber loss (1/n) * sum(huber(y_i, y_hat_i, delta)) Regression combining MSE near zero and MAE for large errors
Binary cross-entropy -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary classification with predicted probability p_i
Categorical cross-entropy -sum_i sum_c y_ic*log(p_ic) Multiclass classification with softmax probabilities
Hinge loss sum_i max(0, 1 - y_i*f(x_i)) Margin-based models such as linear SVMs
KL divergence D_KL(p || q) = sum_c p_c*log(p_c/q_c) Comparing distributions p and q

For Huber loss, delta determines the transition between quadratic and absolute error. For categorical cross-entropy, y_ic is the probability that example i belongs to class c, and p_ic is the predicted probability. For hinge loss, y_i*f(x_i) is the classification margin.

Logistic regression normally uses cross-entropy because it is convex in the weights of a linear model and generally provides better-conditioned gradients. MSE combined with sigmoid outputs can saturate, producing very small gradients for confidently wrong predictions.

Regularization

  • L2 regularization or ridge: lambda * sum(w_j^2). It shrinks weights toward zero and often improves numerical stability.

  • L1 regularization or lasso: lambda * sum(|w_j|). It can drive some weights exactly to zero, producing sparse models and performing feature selection.

Common Pitfalls

  • Confusing training loss with evaluation metrics: training may optimize log loss while reporting F1 or ROC-AUC.

  • Assuming the global minimum generalizes perfectly: use validation data or cross-validation and tune regularization.

  • Ignoring non-convexity: random initialization, restarts, or specialized optimizers may be necessary.

  • Using numerically unstable log calculations: clip probabilities or prefer logits-based losses such as BCEWithLogitsLoss in practice.

  • Checking only the final loss: record the loss curve to diagnose divergence, poor learning rates, or incorrect gradients.

8

How does the cost function choice affect model generalization?

A cost function (or objective) measures how well a model fits the training data and guides parameter updates during learning. The choice of cost function affects generalization because it determines which errors the model prioritizes, how sensitive training is to outliers, and how the optimization landscape behaves. The main trade-off is between fitting the training data closely (lower bias) and avoiding sensitivity to noise or extreme examples that can harm performance on unseen data (lower variance).

Read full answer

Applies tomachine learning concepts

Loss vs. Cost and the Role in Generalization

  • Loss: The error for a single example, e.g. L(y_i, ŷ_i).

  • Cost/objective: The average loss over n examples, often plus a regularization term: J(w) = (1/n) * Σ L(y_i, ŷ_i) + λ * R(w), where w are model weights, λ controls regularization strength, and R(w) penalizes model complexity.

  • For gradient-based training, the cost must be differentiable (or subdifferentiable) with respect to w so gradients can update parameters.

  • Generalization depends on whether the cost encourages learning the underlying signal or memorizing noise and outliers.

Common Cost Functions and Generalization Effects

Cost function Formula (plain notation) Generalization behavior When to use
MSE (1/n) * Σ(y_i - ŷ_i)^2 Squares errors, so large errors dominate; can overfit outliers Regression when errors are roughly Gaussian and outliers are rare
MAE (1/n) * Σ|y_i - ŷ_i| Linear penalty; more robust to outliers, but gradients are constant in magnitude Regression with outliers or heavy-tailed noise
Huber loss Quadratic near 0, linear beyond threshold δ Combines MSE stability and MAE robustness Regression needing a balance between sensitivity and robustness
Binary cross-entropy (log loss) -(1/n) * Σ(y_i*log(ŷ_i) + (1-y_i)*log(1-ŷ_i)) Strongly penalizes confident wrong predictions; convex in w for logistic regression Binary classification with probabilistic outputs
Categorical cross-entropy -(1/n) * Σ Σ y_{i,c}*log(ŷ_{i,c}) (with softmax) Encourages correct class probability; standard for multi-class Multi-class classification
Hinge loss (1/n) * Σ max(0, 1 - y_i*ŷ_i) (labels in {-1,1}) Focuses on margin violations; can yield sparse support vectors Margin-based classifiers like SVMs

Symbols: y_i is the true target, ŷ_i is the prediction (probability for cross-entropy), n is the number of examples, c indexes classes, and Σ denotes summation.

Why cross-entropy instead of MSE for logistic regression: With a sigmoid output, MSE can produce a non-convex surface in the weights and weaker gradients when predictions are confidently wrong. Cross-entropy is convex in w for logistic regression and gives gradients that push probabilities in the right direction.

Regularization in the Cost

  • L2 (ridge / weight decay): adds λ * Σ w_j^2, shrinking weights smoothly and reducing variance.

  • L1 (lasso): adds λ * Σ |w_j|, which can drive some weights to exactly zero (sparse model), acting as implicit feature selection.

  • Training loss and evaluation metric can differ (e.g., train on log loss, report F1 or PR-AUC for imbalanced classes).

Interview Tips

  • Common pitfalls: Using MSE for probabilities (prefer log loss), ignoring outliers when choosing MSE vs MAE/Huber, forgetting to clip probabilities before log (use logits-based losses like torch.nn.BCEWithLogitsLoss in PyTorch 2.x), and optimizing a metric that does not match the business goal.

  • Numerical stability: Compute cross-entropy from logits when possible to avoid overflow/underflow.

  • Follow-up questions: “Why is cross-entropy convex for logistic regression?”, “When would you choose MAE over MSE?”, “How does L1 regularization affect the cost and the learned weights?”, and “Can your training loss differ from your evaluation metric?”

9

When should Mean Squared Error be used for regression models?

Mean Squared Error (MSE) measures the average squared difference between predicted and actual values. It is useful for regression when larger errors should have a greater effect on training, but its sensitivity to outliers can make optimization less robust. The main trade-off is smooth, gradient-friendly optimization versus exaggerated penalties for unusually large errors.

Read full answer

Applies tomachine learning concepts (NumPy 2.x code with Python 3.12)

How MSE Works

For each example, the prediction error is:

error_i = y_i - y_hat_i

The loss for one example is its squared error, loss_i = (y_i - y_hat_i)^2. The cost function or objective averages those losses across the dataset:

MSE = (1/n) * sum((y_i - y_hat_i)^2)

  • n: number of examples.

  • i: index of an example.

  • y_i: actual target value.

  • y_hat_i: predicted value.

  • sum: addition across all examples.

MSE is differentiable, so standard gradient-based optimizers can calculate its gradient and update model parameters. For linear regression, the MSE objective is convex in the weights, helping gradient descent find a global minimum.

Common Cost Functions

Cost function Formula Typical use and trade-off
MSE (1/n) * sum((y - y_hat)^2) Regression; penalizes large errors strongly but is sensitive to outliers.
MAE (1/n) * sum(abs(y - y_hat)) Regression with outliers; less sensitive but non-differentiable at zero, where a subgradient is used.
Huber loss 0.5 * error² when abs(error) <= delta; otherwise delta * (abs(error) - 0.5 * delta) Combines MSE near zero with MAE for large errors; delta controls the transition.
Binary cross-entropy -(1/n) * sum(y * log(p) + (1-y) * log(1-p)) Binary classification; strongly penalizes confident wrong predictions.
Categorical cross-entropy -sum(y_k * log(p_k)) Multiclass classification with softmax outputs and one-hot targets.
Hinge loss (1/n) * sum(max(0, 1 - y * f(x))) SVM classification; encourages correctly classified examples to have a positive margin.
KL divergence sum(p * log(p/q)) Comparing probability distributions p and q; asymmetric and zero only when they match.

For cross-entropy, p is a predicted probability, k is a class index, and f(x) is an SVM decision score. In hinge loss, y is either -1 or +1.

Regularization and Logistic Regression

A regularized objective combines prediction loss with a penalty:

  • L2 regularization: cost + lambda * sum(w_j^2). This is the ridge penalty and is commonly implemented as weight decay; it discourages large weights.

  • L1 regularization: cost + lambda * sum(abs(w_j)). This is the lasso penalty and can drive weights exactly to zero, producing sparse models.

Here, lambda controls regularization strength, w_j is weight j, and sum covers all penalized weights.

Logistic regression normally uses binary cross-entropy rather than MSE. For sigmoid probability p = sigmoid(w*x + b), the cross-entropy weight gradient has the useful form (p - y) * x, where x is the feature vector and b is the intercept. MSE gradients contain an additional p * (1-p) factor, which becomes small for confident predictions and can slow learning. Binary cross-entropy also gives a convex objective in logistic-regression weights.

Code Example

import numpy as np

rng = np.random.default_rng(42)


def mse_cost(y, y_pred):
    return float(np.mean((y - y_pred) ** 2))


def mse_gradients(X, y, y_pred):
    error = y_pred - y
    n = X.shape[0]
    grad_weight = X.T @ (2.0 * error / n)
    grad_bias = 2.0 * error.mean()
    return grad_weight, grad_bias


def binary_cross_entropy(y, p, epsilon=1e-12):
    p_safe = np.clip(p, epsilon, 1.0 - epsilon)
    return float(-np.mean(
        y * np.log(p_safe) + (1.0 - y) * np.log(1.0 - p_safe)
    ))


def bce_gradients(X, y, p):
    error = p - y
    n = X.shape[0]
    grad_weight = X.T @ (error / n)
    grad_bias = error.mean()
    return grad_weight, grad_bias


def sigmoid(values):
    result = np.empty_like(values, dtype=float)
    positive = values >= 0
    result[positive] = 1.0 / (1.0 + np.exp(-values[positive]))
    exp_values = np.exp(values[~positive])
    result[~positive] = exp_values / (1.0 + exp_values)
    return result


# Minimize MSE on a synthetic regression problem.
X_reg = rng.normal(size=(200, 1))
y_reg = 3.0 * X_reg[:, 0] + 2.0 + rng.normal(0.0, 0.2, size=200)

weight = np.array([0.0])
bias = 0.0
learning_rate = 0.05
mse_costs = []

for _ in range(300):
    predictions = X_reg @ weight + bias
    mse_costs.append(mse_cost(y_reg, predictions))
    grad_weight, grad_bias = mse_gradients(X_reg, y_reg, predictions)
    weight -= learning_rate * grad_weight
    bias -= learning_rate * grad_bias

assert mse_costs[-1] < mse_costs[0]


# Minimize binary cross-entropy on synthetic binary labels.
X_binary = rng.normal(size=(200, 2))
true_logits = X_binary[:, 0] - 0.5 * X_binary[:, 1]
y_binary = (rng.uniform(size=200) < sigmoid(true_logits)).astype(float)

weights = np.zeros(2)
intercept = 0.0
learning_rate = 0.1
bce_costs = []

for _ in range(300):
    probabilities = sigmoid(X_binary @ weights + intercept)
    bce_costs.append(binary_cross_entropy(y_binary, probabilities))
    grad_weights, grad_intercept = bce_gradients(
        X_binary, y_binary, probabilities
    )
    weights -= learning_rate * grad_weights
    intercept -= learning_rate * grad_intercept

assert bce_costs[-1] < bce_costs[0]

print(f"MSE cost: {mse_costs[0]:.4f} -> {mse_costs[-1]:.4f}")
print(f"Binary cross-entropy: {bce_costs[0]:.4f} -> {bce_costs[-1]:.4f}")

Practical Evaluation

Use MSE as the training objective when squared errors reflect the business cost of mistakes. For reporting, consider RMSE when errors should remain in the target’s original units, MAE when outliers should receive less influence, and R² to describe explained variance.

The training objective need not equal the evaluation metric. For example, a classifier may train with log loss but be evaluated using F1 when class imbalance makes accuracy misleading. Evaluate regression models on held-out data or through cross-validation, using time-ordered splits for time-series data rather than relying only on training loss.

Common Pitfalls

  • Outliers dominate optimization: Use MAE or Huber loss when robustness matters.

  • Target scale and transformation: MSE is measured in squared target units, and heteroscedastic targets can let high-magnitude cases dominate. A common linear rescaling scales the loss, while a nonlinear target transformation changes the objective.

  • Feature scaling: Scale features for distance-based models, PCA, SVMs, and gradient descent; MSE itself does not require scaled features.

  • Missing values: Impute or otherwise handle missing values before computing MSE.

  • Numerical instability: Clip probabilities before logarithms, or use torch.nn.BCEWithLogitsLoss, which operates directly on logits.

  • Data leakage: Fit preprocessing only on training folds with a scikit-learn Pipeline.

  • Overfitting the validation set: Use validation data or cross-validation rather than repeatedly tuning against the same holdout set.

10

What is the Cross-Entropy cost function and where is it used?

Cross-entropy measures the loss between a model’s predicted probability distribution and the true distribution. It is used primarily for classification tasks such as logistic regression, softmax classification, neural networks, and language modeling. Its main trade-off is that confident incorrect predictions receive very large loss values, making the objective sensitive to mislabeled data.

Read full answer

Applies tomachine learning concepts; PyTorch 2.x for `torch.nn.BCEWithLogitsLoss`

Loss, Cost, and Differentiability

  • Loss: the error associated with one example.

  • Cost function: the average loss over the training data, often plus a regularization term.

  • Objective: the quantity the optimizer minimizes; it is often synonymous with the cost function.

Gradient-based training requires the objective to be differentiable or to provide usable subgradients. Cross-entropy is differentiable for valid probabilities, while MSE and Huber loss are also differentiable. MAE and hinge loss are not differentiable everywhere, although optimizers can use their subgradients.

Cross-Entropy Forms and Usage

For binary classification, logistic regression predicts p = sigmoid(z), and binary cross-entropy is:

BCE = -(1/n) * sum_i[y_i * log(p_i) + (1 - y_i) * log(1 - p_i)]

Here, n is the number of examples, y_i is the true binary label, p_i is the predicted probability of the positive class, and z is the logit. BCE is used by logistic regression and neural networks with one sigmoid output.

For multiclass classification, softmax converts logits into class probabilities, and categorical cross-entropy is:

CCE = -(1/n) * sum_i sum_c[y_ic * log(p_ic)]

Here, i indexes examples, c indexes classes, y_ic is the true class indicator, and p_ic is the predicted probability for class c. CCE is used with softmax outputs in multiclass classifiers and language models, where it scores the predicted next token.

Cross-entropy and KL divergence are related by:

CE(P, Q) = H(P) + KL(P || Q)

CE(P, Q) = -sum_x P(x) * log(Q(x)) compares predicted distribution Q with true distribution P; H(P) = -sum_x P(x) * log(P(x)) is the entropy of P; and KL(P || Q) = sum_x P(x) * log(P(x) / Q(x)) measures their additional divergence. When P is a one-hot target, its entropy is zero, so cross-entropy equals KL(P || Q).

Related Cost Functions

Cost Formula Typical use and behavior
MSE (1/n) * sum_i(y_i - y_hat_i)^2 Regression; penalizes large errors more heavily and is sensitive to outliers.
MAE (1/n) * sum_i abs(y_i - y_hat_i) Regression with outliers; more robust than MSE.
Huber loss (1/n) * sum_i h_delta(y_i - y_hat_i) Regression combining smooth quadratic behavior near zero with linear behavior for large errors.
Hinge loss (1/n) * sum_i max(0, 1 - y_i * f(x_i)) SVM classification; encourages correctly classified examples to have a margin of at least one.
KL divergence sum_x P(x) * log(P(x) / Q(x)) Comparing distributions or adding a distributional regularization term; it is not symmetric.

In the Huber formula, h_delta(e) is 0.5 * e^2 when abs(e) <= delta and delta * (abs(e) - 0.5 * delta) otherwise; delta is the positive threshold separating the two regions. Here, y_hat_i is a regression prediction, y_i is a binary label in {-1, +1} for hinge loss, and f(x_i) is an SVM decision score.

Regularization and Evaluation

  • L2 regularization or ridge: cost + lambda * sum_j(w_j^2). It continuously shrinks weights and can reduce overfitting.

  • L1 regularization or lasso: cost + lambda * sum_j(abs(w_j)). It can set some weights exactly to zero, producing sparse models.

Here, lambda controls regularization strength, and w_j is the jth model weight. Increasing lambda generally reduces overfitting but can cause underfitting.

Logistic regression is convex in its weights when trained with cross-entropy. Cross-entropy also gives a stronger corrective gradient than MSE applied through a sigmoid, whose gradient may become very small when predictions are saturated.

The training loss need not equal the evaluation metric. For example, a classifier may train with log loss while being evaluated using F1, precision, recall, ROC-AUC, or PR-AUC when appropriate. Accuracy can mislead on imbalanced classes.

Common Pitfalls

  • Numerical instability: probabilities of exactly zero or one make logarithms infinite. Use logits-based implementations such as PyTorch’s torch.nn.BCEWithLogitsLoss.

  • Noisy labels: cross-entropy strongly penalizes confident mistakes, so mislabeled examples can substantially affect training.

  • Wrong metric: select evaluation metrics from the business and class-distribution requirements rather than automatically using accuracy.

  • Data leakage: fit preprocessing only on training folds and evaluate on unseen validation or test data.

  • Ignoring regularization: an unregularized model may overfit, especially with limited data.

11

What is hinge loss in SVM classification?

Hinge loss is a margin-based loss used by support vector machines (SVMs) to encourage correctly classified observations to lie confidently beyond the decision boundary. It supports binary and multiclass classification while favoring a large separating margin. Its main trade-off is robust margin-based classification rather than inherently calibrated probability estimates.

Read full answer

Applies tomachine learning concepts (all versions)

Loss Versus Cost Objective

A loss measures the error for one training example, while a cost function or objective function aggregates those losses across the dataset and may add regularization:

J(w) = (1/n) * sum(ℓ_i) + regularization

Here, n is the number of training examples, i indexes them, ℓ_i is the loss for example i, and w contains the model parameters.

Gradient-based optimization requires a differentiable objective or usable subgradients at nondifferentiable points. Hinge loss is piecewise linear and nondifferentiable when the margin equals 1, but SVM optimizers can handle it using subgradients.

Binary and Multiclass Hinge Loss

For binary SVM classification, the standard formulation uses labels y_i of +1 or -1 and a signed decision score f(x_i):

ℓ_i = max(0, 1 - y_i * f(x_i))

  • y_i * f(x_i) is the functional margin.

  • The loss is zero when the functional margin is at least 1.

  • A correctly classified point inside the margin receives a positive loss.

  • A misclassified point receives a loss greater than 1, increasing with the severity of the violation.

Unlike 0-1 loss, hinge loss distinguishes small margin violations from severe misclassifications.

A common max-over-incorrect-classes multiclass extension is:

L_multiclass = (1/n) * sum_i max(0, max_{j != y_i}(1 + f_j(x_i) - f_{y_i}(x_i)))

Here, f_j(x_i) is the score for candidate class j, and f_{y_i}(x_i) is the score for the true class. Other multiclass SVM formulations sum the violations over incorrect classes instead.

Related Cost Functions

Function Formula Typical use
Mean squared error (MSE) MSE = (1/n) * sum((y_i - y_hat_i)^2) Regression; penalizes large errors more strongly.
Mean absolute error (MAE) MAE = (1/n) * sum(abs(y_i - y_hat_i)) Regression with noisy observations or important outliers.
Huber loss (1/n) * sum(huber(e_i, delta)) Regression that is quadratic near zero error and linear for large errors.
Binary cross-entropy BCE = -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary classification and logistic regression.
Categorical cross-entropy -(1/n) * sum_i sum_k(y_ik*log(q_ik)) Multiclass classification with softmax outputs.
Hinge loss (1/n) * sum(max(0, 1 - y_i*f(x_i))) Margin-maximizing SVM classification.

| KL divergence | D_KL(p||q) = sum_k(p_k*log(p_k/q_k)) | Comparing a true distribution p with an approximation q. |

In these formulas, y_i is a true label, y_hat_i is a prediction, p_i is a predicted probability, y_ik is a one-hot target, q_ik is a class probability, e_i is a regression error, and delta is the Huber threshold. For Huber loss, huber(e, delta) equals e^2/2 when abs(e) <= delta and delta*(abs(e) - delta/2) otherwise.

Logistic regression generally uses cross-entropy rather than MSE because the cross-entropy objective is convex in the linear model’s weights and avoids the vanishing gradients that sigmoid-composed MSE can produce for confident predictions.

Regularization and SVM Objective

  • L2 regularization adds lambda * sum_j(w_j^2). Ridge regression and weight decay use this approach.

  • L1 regularization adds lambda * sum_j(abs(w_j)). Lasso regression uses it and can produce sparse coefficients by setting some weights to exactly zero.

  • A common binary SVM objective is J(w, b) = (1/2)*||w||^2 + C*sum_i(ℓ_i). Here, C controls the penalty for margin violations: a large C penalizes violations heavily, while a small C tolerates more violations in exchange for stronger regularization.

Features should generally be scaled before SVM training because the learned margin depends on feature scale.

Common Pitfalls

  • Wrong label encoding: The standard binary hinge formulation expects -1 and +1, not 0 and 1.

  • Expecting probabilities: SVM decision scores are not probabilities; use probability calibration when estimates are required.

  • Ignoring preprocessing: Scale features, handle missing values, and fit preprocessing only on training data to avoid leakage.

  • Ignoring imbalance: Adjust class weights when appropriate, and remember that accuracy can be misleading for imbalanced classes.

  • Overfitting: Control C and kernel complexity, and select hyperparameters with stratified cross-validation rather than repeated tuning on the test set.

  • Confusing objectives and metrics: A model may train with one loss but report another metric, such as training with log loss and evaluating with F1, ROC-AUC, or PR-AUC.

  • Numerical instability: For neural-network binary classification, use logits-based losses such as torch.nn.BCEWithLogitsLoss rather than manually taking the logarithm of computed probabilities.

12

How does log loss work in logistic regression?

Log loss (binary cross-entropy) is the cost function logistic regression minimizes to learn weights that predict class probabilities. It measures how far predicted probabilities are from the true binary labels, strongly penalizing confident wrong predictions. Its main trade-off is sensitivity to poorly calibrated probabilities and numerical issues near 0 or 1, balanced by producing well-behaved gradients for probability models.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12 with NumPy 2.x)

Log Loss in Logistic Regression

Logistic regression models the probability of the positive class as p_i = sigmoid(z_i) = 1 / (1 + exp(-z_i)), where z_i = w · x_i + b. - x_i: feature vector for example i. - w: weight vector learned during training; b: bias (intercept). - z_i: linear score (logit) before the sigmoid. - y_i: true label for example i, either 0 or 1. - p_i: predicted probability that y_i = 1.

The loss is the error for one example: loss_i = -[y_i * log(p_i) + (1 - y_i) * log(1 - p_i)]. The cost/objective is the average loss over n examples, optionally plus regularization: J(w, b) = -(1/n) * sum_{i=1}^{n}(y_i * log(p_i) + (1 - y_i) * log(1 - p_i)) + regularization. During training, gradient-based optimization (e.g., gradient descent) updates w and b to minimize J.

Why Cross-Entropy Instead of MSE

Criterion Log loss / cross-entropy Mean squared error (MSE)
Formula -(1/n) * sum(y*log(p) + (1-y)*log(1-p)) (1/n) * sum((y - p)^2)
With sigmoid + logistic regression Convex in w, b; stable, informative gradients Can be non-convex; gradients can become very small when predictions are confident and wrong
Best use Classification with probabilities Regression, or when squared error matches the task

Log loss gives larger gradients when the model is confidently wrong, which helps logistic regression converge reliably.

Regularized Cost

Regularization adds a penalty to control overfitting: - L2 (ridge / weight decay): J + lambda * sum(w_j^2) shrinks weights smoothly. - L1 (lasso): J + lambda * sum(|w_j|) can drive some weights to exactly zero, producing sparse models. lambda controls the penalty strength (the bias/variance trade-off is usually tuned with cross-validation).

Code Example

import numpy as np

def sigmoid(z: np.ndarray) -> np.ndarray:
    return 1.0 / (1.0 + np.exp(-z))

def mse_loss(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    # MSE = (1/n) * sum((y - y_hat)^2)
    return float(np.mean((y_true - y_pred) ** 2))

def binary_cross_entropy(y_true: np.ndarray, p: np.ndarray, eps: float = 1e-15) -> float:
    # Clip to avoid log(0) and numerical instability
    p_safe = np.clip(p, eps, 1.0 - eps)
    return float(-np.mean(y_true * np.log(p_safe) + (1.0 - y_true) * np.log(1.0 - p_safe)))

def bce_gradients(X: np.ndarray, y_true: np.ndarray, p: np.ndarray) -> tuple[np.ndarray, float]:
    n = X.shape[0]
    error = p - y_true              # (n,)
    dw = (1.0 / n) * (X.T @ error)  # (d,)
    db = float((1.0 / n) * np.sum(error))
    return dw, db

# Synthetic, self-contained data
rng = np.random.default_rng(42)
n_samples, n_features = 200, 2
X = rng.standard_normal((n_samples, n_features))
true_w = np.array([1.5, -2.0])
y = (sigmoid(X @ true_w + 0.5) >= 0.5).astype(np.float64)  # binary labels (200,)

w = np.zeros(n_features)
b = 0.0
lr = 0.1

for step in range(1, 201):
    p = sigmoid(X @ w + b)
    cost = binary_cross_entropy(y, p)
    dw, db = bce_gradients(X, y, p)
    w -= lr * dw
    b -= lr * db
    if step % 50 == 0:
        print(f"Step {step:3d} | BCE cost: {cost:.4f}")  # cost decreases over steps

print(f"Final MSE (for comparison): {mse_loss(y, p):.4f}")
print(f"Learned weights shape: {w.shape}")  # (2,)

Common Pitfalls and Interview Tips

  • Numerical instability: never compute log(0). Clip probabilities as above, or prefer logits-based losses such as torch.nn.BCEWithLogitsLoss (PyTorch 2.x), which combine sigmoid and cross-entropy stably.

  • Loss vs. metric: you may train on log loss but report precision, recall, F1, ROC-AUC, or PR-AUC for imbalanced classes; accuracy can mislead.

  • Data leakage: split data before fitting any preprocessing, and use sklearn.pipeline.Pipeline so training folds only see training data.

  • Feature scaling: gradient-based logistic regression benefits from scaled features.

  • Interview follow-ups: “Derive the gradient of log loss,” “Why is the decision boundary linear in feature space?”, “When would you choose L1 vs. L2?”, and “How does class imbalance affect log loss and threshold selection?”

13

When is Huber loss preferable to MSE?

Huber loss is a regression loss that behaves like MSE for small errors and like MAE for large errors. It combines MSE’s smooth, informative gradients near zero with MAE’s robustness to outliers. The main trade-off is that Huber reduces sensitivity to extreme errors, but it requires choosing a transition threshold and does not penalize large errors as strongly as MSE.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12)

Loss Versus Cost Function

A loss measures the error for one example. A cost function, or objective, aggregates losses across the training data and may add regularization:

J(w) = (1/n) * sum(L(y_i, f(x_i; w))) + lambda * R(w)

  • n: number of training examples.

  • y_i: observed target for example i.

  • f(x_i; w): model prediction for example i with parameters w.

  • L: per-example loss.

  • lambda: regularization strength.

  • R(w): penalty on model parameters.

Gradient-based training requires a differentiable objective because optimizers compute parameter updates from derivatives. Some non-differentiable objectives, such as MAE at zero, can still be optimized with subgradients or specialized solvers.

Huber Loss Compared with MSE and MAE

For residual r = y - y_hat and threshold delta, Huber loss is:

L_delta(y, y_hat) = 0.5 * r^2 when |r| <= delta

L_delta(y, y_hat) = delta * (|r| - 0.5 * delta) otherwise

  • y: observed target.

  • y_hat: predicted target.

  • r: residual, the prediction error.

  • delta: error magnitude at which the loss transitions from quadratic to linear.

Loss Formula Behavior on large errors Strengths Weaknesses When to use
MSE (1/n) * sum((y - y_hat)^2) Grows quadratically Smooth gradients, strongly penalizes large mistakes Sensitive to outliers and label noise Residuals are roughly symmetric with few extreme errors
MAE ‘(1/n) * sum( y - y_hat )‘ Grows linearly Robust to outliers Not differentiable at zero, can be less smooth to optimize Many outliers and robustness matters more than smooth optimization
Huber Piecewise quadratic/linear Grows linearly after delta Smooth near zero, robust to outliers Requires tuning delta, less aggressive than MSE on large errors Regression targets contain occasional outliers or noisy labels

When Huber Loss Is Preferable to MSE

  • Outliers or heavy-tailed noise: A few extreme targets make squared errors dominate the objective. Huber limits each large residual’s influence to linear growth.

  • Noisy labels: Measurement errors or annotation mistakes should not pull the fitted model as strongly as MSE would.

  • Need for stable gradients near zero: Unlike MAE, Huber is differentiable at zero, which can make gradient-based optimization smoother.

  • MSE still preferred when assumptions fit: If residuals are approximately Gaussian and large errors are genuinely important to penalize, MSE is often the better choice.

The value of delta should be chosen relative to the target scale and residual distribution, often by validation (for example, using cross-validation and reporting MAE, RMSE, or R² depending on the goal). A small delta behaves more like MAE; a large delta behaves more like MSE.

Regularization and Related Costs

Cost functions often include regularization:

  • L2 regularization (ridge, weight decay): adds lambda * sum(w_j^2), shrinking weights continuously.

  • L1 regularization (lasso): adds lambda * sum(|w_j|), which can produce sparse weights.

For classification, logistic regression typically uses binary cross-entropy rather than MSE because cross-entropy is convex in the weights and gives better gradients when predictions are confidently wrong.

Common Pitfalls

  • Choosing delta arbitrarily: delta defines where robustness begins; tune it on validation data in the target’s units.

  • Assuming Huber is always better: Compare against MSE and MAE with the evaluation metric that matters for the problem.

  • Confusing training loss with evaluation metric: You might optimize Huber or MSE but report MAE, RMSE, or R².

  • Numerical instability in classification losses: When using cross-entropy in deep learning frameworks, prefer logits-based losses such as torch.nn.BCEWithLogitsLoss (PyTorch 2.x) to avoid unstable log(0) computations.

  • Interview follow-ups: Expect questions on how to choose delta, why MAE can be harder to optimize, why squared loss is sensitive to outliers, and when to use cross-entropy instead of MSE.

14

Why is the 0-1 loss function impractical for model training?

The 0-1 loss function counts prediction mistakes in classification by assigning a loss of 1 to an incorrect label and 0 to a correct one. It directly measures the error rate, which is the quantity many classification tasks ultimately care about, but its main trade-off is that it is non-differentiable and produces uninformative gradients, so standard gradient-based training cannot optimize it directly.

Read full answer

Applies tomachine learning concepts (all versions)

Definition and Formula

  • Loss vs. cost: A loss measures error for one example; a cost (objective) is usually the average loss over the dataset, sometimes plus a regularization term.

  • 0-1 loss formula: L(y, y_hat) = 0 if y == y_hat, else 1. Equivalently, L(y, y_hat) = I[y != y_hat], where I[.] is the indicator function (1 when the condition is true, 0 otherwise).

  • Symbols: y is the true class label for one example, y_hat is the predicted class label, and I[y != y_hat] returns 1 for a misclassification and 0 for a correct classification.

  • Cost over data: J = (1/n) * sum_{i=1}^{n} L(y_i, y_hat_i), where n is the number of examples and y_i, y_hat_i are the true and predicted labels for example i. This equals the misclassification rate.

Why It Is Impractical

  • Not differentiable for gradient-based training: The loss jumps abruptly at the decision boundary. Its gradient with respect to model parameters is zero almost everywhere and undefined at the jump, so gradient descent receives no useful direction for updating parameters.

  • Combinatorial optimization: Minimizing 0-1 loss over many examples is computationally hard in general, because small parameter changes do not smoothly improve the objective.

  • Insensitive to confidence: A barely wrong prediction and a very wrong prediction both incur loss 1, so the optimizer cannot learn from “almost correct” outputs.

  • Practical consequence: Models are trained on a smooth surrogate loss that approximates or upper-bounds 0-1 loss, while 0-1-style metrics (accuracy, error rate) are reported for evaluation.

Common Surrogate Losses

Surrogate Typical use Strength Weakness
Log loss / binary cross-entropy Logistic regression, neural nets Convex in the weights for linear models, smooth informative gradients Sensitive to confident wrong predictions
Categorical cross-entropy + softmax Multiclass neural nets Probabilistic outputs, standard for multiclass Needs numerically stable logits-based implementation
Hinge loss Linear SVMs Focuses on margin, can yield sparse support vectors Not probabilistic, not smooth at the margin
Huber loss Robust regression (not a 0-1 surrogate) Less sensitive to outliers than MSE Requires a threshold hyperparameter

Common Pitfalls

  • Trying to backpropagate through 0-1 loss: It provides no useful gradient; train on cross-entropy or hinge loss and report accuracy, error rate, or F1 as appropriate.

  • Confusing training loss with evaluation metric: A model may minimize log loss while the business metric is error rate, precision, recall, or F1; choose thresholds and report the metric that matches the decision.

  • Numerical instability in surrogates: In NumPy, clip probabilities (e.g., with np.clip) before taking log to avoid log(0); in PyTorch, prefer logits-based losses such as torch.nn.BCEWithLogitsLoss.

  • Interview follow-ups: Expect questions on why logistic regression uses cross-entropy instead of MSE (convex in the weights for the linear model and better gradients), how hinge loss differs from log loss, and why accuracy can mislead on imbalanced classes.

15

What is regularization in machine learning cost functions?

Regularization in cost functions is the practice of adding a penalty term to the data-fitting loss so the objective becomes the average prediction error plus a constraint on model complexity. It solves overfitting by discouraging overly large or overly specific weights, improving generalization to unseen data. Its main trade-off is bias versus variance: stronger regularization reduces variance but can increase bias and underfit.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12, NumPy 2.x)

Loss, Cost, and Differentiability

  • Loss: the error for one example, such as the squared difference (y - y_hat)^2 for a single prediction.

  • Cost/objective: the quantity optimized during training, usually the average loss over n examples plus any regularization term, e.g. J(w) = (1/n) * sum(L_i) + lambda * R(w).

  • Differentiability: gradient-based training (gradient descent and its variants) updates parameters using derivatives, so the cost should be differentiable (or have usable subgradients, as with L1 at zero) with respect to the model weights.

Common Cost Functions

In each formula, n is the number of examples, y_i is the true value/label, y_hat_i is the prediction, and w denotes model weights.

Cost function Formula (plain notation) When to use
MSE (1/n) * sum((y_i - y_hat_i)^2) Regression; penalizes large errors more because errors are squared.
MAE (1/n) * sum(|y_i - y_hat_i|) Regression when robustness to outliers matters.
Huber loss Quadratic for small errors, linear for large errors Regression balancing MSE sensitivity and MAE robustness.
Log loss / binary cross-entropy -(1/n) * sum(y_i*log(y_hat_i) + (1-y_i)*log(1-y_hat_i)) Binary classification with probability outputs in (0, 1).
Categorical cross-entropy -(1/n) * sum_i sum_c y_{i,c}*log(y_hat_{i,c}) after softmax Multi-class classification; c indexes classes.
Hinge loss (1/n) * sum(max(0, 1 - y_i * f(x_i))) with labels in {-1, +1} Support vector machines and margin-based classifiers.
KL divergence sum(P(x) * log(P(x)/Q(x))) Measuring difference between distributions P and Q, e.g. distillation or probabilistic models.

Regularization in the Cost Function

  • L2 (ridge, weight decay): adds lambda * sum(w_j^2) to the cost. lambda (the regularization strength) controls the penalty; larger lambda shrinks weights toward zero but keeps them dense.

  • L1 (lasso): adds lambda * sum(|w_j|). It can drive some weights exactly to zero, producing sparse models useful for feature selection.

  • Why cross-entropy for logistic regression: using MSE with a sigmoid output creates a non-convex objective with poor gradients near saturated predictions. Cross-entropy is convex in the weights for logistic regression and gives stronger, more useful gradients.

  • Training vs evaluation: the optimized cost and the reported metric can differ, e.g. train on log loss but report F1 for an imbalanced classification task.

Common Pitfalls

  • Numerical instability: log(0) produces infinities. Clip probabilities with np.clip to avoid log(0), or prefer logits-based losses such as torch.nn.BCEWithLogitsLoss (PyTorch 2.x), which combines sigmoid and cross-entropy stably.

  • Regularizing the intercept: penalizing the bias term can shift predictions unnecessarily; many implementations exclude it from R(w).

  • Wrong strength or scaling: features on different scales make lambda hard to tune. Standardize features (fit on training data only) and select lambda with cross-validation.

  • Metric mismatch: a decreasing training cost does not guarantee the business metric improves; monitor an appropriate validation metric (e.g. F1 or PR-AUC for imbalanced classes).

  • Interview follow-ups: “Why can L1 produce zeros but L2 usually does not?”, “How does weight decay differ from L2 in optimizers like Adam?”, “When would you choose MAE or Huber over MSE?”

16

How does gradient descent use cost functions to train models?

A cost function (or objective) measures how poorly a model fits the training data, usually as the average loss (error on one example) plus any regularization term, and gradient descent solves that problem by iteratively adjusting parameters to reduce this value. Its main trade-off is speed and stability versus accuracy: a large learning rate converges quickly but can overshoot, while a small rate is stable but slow.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12)

How Gradient Descent Uses Cost Functions

  • Loss vs cost/objective: A loss is the error for a single example; the cost/objective is the average loss over the dataset, often plus a regularization term, e.g. J(w) = (1/n) * Σ L(y_i, ŷ_i) + R(w), where w are parameters, n is the number of examples, L is the per-example loss, and R(w) is regularization.

  • Differentiability: For gradient-based training, J(w) must be differentiable (or subdifferentiable) so gradients exist for every parameter.

  • Gradient step: At each iteration gradient descent computes ∇J(w), the vector of partial derivatives showing the direction of steepest increase, and updates w = w - η * ∇J(w), where η (eta) is the learning rate controlling step size.

  • Role of the cost: The cost value tracks progress (it should generally decrease on the training data), while the gradient tells how to change each parameter. Batch, mini-batch, and stochastic gradient descent differ in how many examples are used to estimate ∇J(w).

Key Cost Functions and Formulas

  • MSE (regression): MSE = (1/n) * Σ(y_i - ŷ_i)^2, where n is the number of examples, y_i is the true value, and ŷ_i is the prediction. It penalizes large errors more and is sensitive to outliers.

  • MAE (regression): MAE = (1/n) * Σ|y_i - ŷ_i|, where |·| is absolute value. It is more robust to outliers but has a non-smooth gradient at zero error.

  • Huber loss: Huber_δ(e) = 0.5 * e^2 when |e| <= δ, else δ * (|e| - 0.5 * δ), where e = y_i - ŷ_i is the error and δ (delta) is the threshold. It behaves like MSE near zero and like MAE for large errors.

  • Binary cross-entropy / log loss: J = -(1/n) * Σ[y_i*log(ŷ_i) + (1-y_i)*log(1-ŷ_i)], where y_i ∈ {0,1} is the true label and ŷ_i is the predicted probability. Logistic regression uses cross-entropy instead of MSE because the MSE plus sigmoid objective is non-convex in the weights and can produce poor gradients, while cross-entropy is convex in the weights and gives stronger gradients when predictions are confidently wrong.

  • Categorical cross-entropy with softmax: J = -(1/n) * Σ_i Σ_c y_{i,c} * log(ŷ_{i,c}), where c indexes classes, y_{i,c} is 1 for the true class and 0 otherwise, and ŷ_{i,c} are softmax probabilities that sum to 1.

  • Hinge loss: J = (1/n) * Σ max(0, 1 - y_i * f(x_i)), where y_i ∈ {-1,+1} and f(x_i) is the model score, used for margin-based SVMs.

  • KL divergence: D_KL(P || Q) = Σ_x P(x) * log(P(x) / Q(x)), where P is the target distribution and Q is the model distribution; it measures how Q differs from P.

Regularization in the Objective

  • L2 (ridge / weight decay): J_reg = J + λ * Σ(w_j^2), where λ (lambda) controls penalty strength and w_j are weights (often excluding the bias). It shrinks weights smoothly.

  • L1 (lasso): J_reg = J + λ * Σ|w_j|. It encourages sparsity by driving some weights exactly to zero.

  • Effect on gradients: Regularization adds a penalty gradient (e.g. 2*λ*w_j for L2, or a subgradient for L1), so gradient descent balances fitting the data against keeping weights small.

Cost function Typical use Strength Weakness
MSE Regression Smooth, simple gradients Outlier-sensitive
MAE / Huber Robust regression Less affected by outliers MAE non-smooth; Huber needs δ
Cross-entropy Classification probabilities Convex for logistic regression, good gradients Numerically unstable without clipping or logits-based losses
Hinge Margin-based classification Focuses on support vectors Not a calibrated probability loss

Common Pitfalls

  • Numerical instability: Computing log(0) in cross-entropy can produce infinities; use probability clipping or logits-based losses such as torch.nn.BCEWithLogitsLoss.

  • Training loss vs evaluation metric: Models may train on log loss but be evaluated with F1, ROC-AUC, or PR-AUC; optimizing the wrong objective can misalign with the real goal.

  • Wrong cost for the task: Using MSE for probabilities or ignoring outliers in regression can distort learning and gradients.

  • Poor scaling or learning rate: Unscaled features and a poorly chosen η make gradients unstable or convergence slow; feature scaling helps gradient descent methods.

  • Interview follow-ups: “Why is cross-entropy preferred for logistic regression?”, “How do L1 and L2 change the objective and weights?”, and “What happens when the cost function is non-convex?”

17

How do batch and stochastic gradient descent differ?

Batch gradient descent (BGD) updates model parameters using a gradient computed over the entire training set, while stochastic gradient descent (SGD) uses one randomly selected example per update. Batch gradients are low-noise but expensive per update; SGD updates are cheap but noisy, making update stability and convergence speed the main trade-off.

Read full answer

Applies tomachine learning concepts; PyTorch 2.x for the logits-based BCE example

How They Differ

For parameters w, a typical training objective is:

J(w) = (1/n) * sum_i loss(y_i, prediction(x_i)) + regularization(w)

Here, n is the number of training examples, i indexes examples, x_i is an example’s features, y_i is its target, and w is the parameter vector. The optional regularization term penalizes certain parameter values.

  • Batch gradient descent: w <- w - eta * (1/n) * sum_i grad_w loss(y_i, prediction(x_i))

  • Stochastic gradient descent: w <- w - eta * grad_w loss(y_i, prediction(x_i)), where i is sampled from the training data.

Here, eta is the learning rate and grad_w denotes the gradient with respect to the parameters.

Characteristic Batch gradient descent Stochastic gradient descent
Data per update All n examples One example
Gradient Exact and low-noise Noisy estimate
Computation per update O(n*d) O(d)
Reproducibility Deterministic for fixed data and initialization Varies with example selection
Typical use Small datasets or full-batch training Large datasets and online learning

Here, d is the number of model parameters affected by each example. One epoch is one complete pass through the training data; an SGD epoch normally contains n updates. Mini-batch gradient descent, usually the practical compromise, processes several examples per update and vectorizes efficiently on CPUs or GPUs.

Loss and Cost Functions

A loss measures the error for one example, while a cost function or objective aggregates losses across the dataset and may include regularization. Gradient-based training needs a differentiable objective—or a valid subgradient where the derivative is undefined—so it can determine which parameter change reduces the objective.

  • Mean squared error: MSE = (1/n) * sum_i((y_i - y_hat_i)^2). It is common for regression and penalizes large errors more heavily.

  • Mean absolute error: MAE = (1/n) * sum_i(abs(y_i - y_hat_i)). It is more robust to outliers than MSE.

  • Huber loss: For e_i = y_i - y_hat_i, use 0.5*e_i^2 when abs(e_i) <= delta and delta*(abs(e_i) - 0.5*delta) otherwise, then average across examples. Here, delta is the threshold between quadratic and linear behavior.

  • Binary cross-entropy: BCE = -(1/n) * sum_i(y_i*log(p_i) + (1-y_i)*log(1-p_i)). It is used for binary and multilabel classification, where p_i is a predicted probability.

  • Categorical cross-entropy: CCE = -(1/n) * sum_i sum_k(y_ik*log(p_ik)). It is used for mutually exclusive multiclass classes with softmax outputs; y_ik is the one-hot target and p_ik is the predicted probability for class k.

  • Hinge loss: Hinge = (1/n) * sum_i(max(0, 1 - y_i*f(x_i))). It is used for linear SVMs, where y_i is -1 or +1 and f(x_i) is the decision score.

  • KL divergence: KL(q || p) = sum_k(q_k*log(q_k/p_k)). It measures how distribution q differs from reference distribution p.

Regularization and Logistic Regression

  • L2 regularization: Adds lambda * sum_j(w_j^2) to the objective. It discourages large weights and corresponds to ridge regression or weight decay.

  • L1 regularization: Adds lambda * sum_j(abs(w_j)). It can set weights exactly to zero, producing sparse models and forming the lasso objective.

Here, lambda is the regularization strength and j indexes parameters.

Logistic regression uses binary cross-entropy because its objective is convex in the weights and provides useful gradients even when sigmoid predictions are near 0 or 1. Squared error applied after a sigmoid can suffer from saturated gradients and generally produces a non-convex objective. Training loss need not equal the evaluation metric: a classifier may optimize log loss but report F1 when false positives and false negatives have different costs.

Practical Guidance

  • Mini-batch training is often preferred because it balances gradient noise with hardware utilization.

  • Standardize features for gradient descent when scale differs greatly, and handle missing values explicitly.

  • Split data before fitting preprocessing. Use Pipeline or ColumnTransformer so scalers, imputers, and encoders are fitted only on training data.

  • Use MAE or RMSE for regression. For imbalanced classification, report precision, recall, F1, ROC-AUC, and PR-AUC because accuracy can mislead.

  • Use StratifiedKFold for classification and TimeSeriesSplit for time-dependent data. Control randomness with fixed seeds and shuffle examples for SGD.

Common Pitfalls and Interview Tips

  • An individual SGD update does not necessarily reduce the full-dataset cost; its gradient is noisy.

  • A learning rate that is too large can diverge, while one that is too small trains slowly. Batch and SGD rates must be tuned independently.

  • Calling a mini-batch update “one-example SGD” is imprecise; SGD conventionally means one example per update.

  • Never evaluate log(0) directly. In PyTorch 2.x, prefer torch.nn.BCEWithLogitsLoss, which combines sigmoid and binary cross-entropy numerically stably.

  • Typical follow-up questions cover mini-batch training, learning-rate schedules, convergence criteria, regularization strength, and detecting overfitting.

18

How does mini-batch gradient descent trade speed and model performance?

Mini-batch gradient descent updates model parameters using a small group of training examples at each iteration. It offers a practical compromise between stochastic gradient descent, which makes frequent but noisy updates, and full-batch gradient descent, which computes stable but relatively expensive gradients. Its main trade-off is that larger batches provide smoother, more hardware-efficient updates, while smaller batches update more frequently and may benefit from the noise’s regularizing effect.

Read full answer

Applies tomachine learning concepts (all versions)

How It Works

For each epoch, the algorithm:

  1. Shuffles the training examples and divides them into mini-batches.

  2. Computes the loss and its gradient for the current batch.

  3. Updates each parameter: w ← w - learning_rate * gradient

For a batch B containing b examples, a typical objective is:

J_B(w) = (1/b) * sum_{i in B} L(y_i, f_w(x_i)) + lambda * R(w)

The corresponding full-data objective is:

J(w) = (1/n) * sum_{i=1}^n L(y_i, f_w(x_i)) + lambda * R(w)

  • w is the model’s parameter vector and learning_rate controls each update’s size.

  • B is the current mini-batch, b is its size, and n is the total number of training examples.

  • L is the loss, or error for one example; y_i is the target and f_w(x_i) is the prediction.

  • R(w) is an optional regularization penalty, and lambda controls its strength.

Gradient-based training requires an objective that is differentiable almost everywhere, or whose non-differentiable points admit suitable subgradients. Smooth losses such as MSE support ordinary gradients; MAE uses a subgradient at zero.

Main Cost Functions

Cost function Formula When to use it
Mean squared error (MSE) (1/n) * sum((y_i - y_hat_i)^2) Regression; strongly penalizes large errors
Mean absolute error (MAE) (1/n) * sum(abs(y_i - y_hat_i)) Regression where outliers should have less influence
Huber loss 0.5*e^2 when abs(e) <= delta; otherwise delta*(abs(e) - 0.5*delta) Regression combining MSE and MAE behavior
Binary cross-entropy -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary or multilabel classification
Categorical cross-entropy -sum_k(y_k*log(softmax(score_k))) Multiclass classification
Hinge loss (1/n) * sum(max(0, 1 - y_i*f_w(x_i))) Margin-based models such as SVMs
KL divergence sum_k(r_k*log(r_k/q_k)) Comparing probability distributions, such as in knowledge distillation

Here, y_hat_i is a prediction, e = y_i - y_hat_i, and delta is the positive threshold separating Huber loss’s quadratic and linear regions. For binary cross-entropy, p_i is a predicted probability. For categorical cross-entropy, score_k is a class score, y_k is a one-hot target, and k indexes classes. For hinge loss, y_i must be -1 or +1. For KL divergence, r_k and q_k are target and predicted probabilities.

Logistic regression normally uses binary cross-entropy because it produces a convex objective in the weights, including with L2 regularization. Applying MSE after a sigmoid generally produces a non-convex objective. Cross-entropy also gives the simple logit gradient p - y, whereas MSE through a sigmoid introduces an additional p*(1-p) factor.

L2 regularization, or ridge, adds lambda * sum(w_j^2) and discourages large weights. L1 regularization, or lasso, adds lambda * sum(abs(w_j)) and can drive some weights exactly to zero, producing sparse models.

Speed and Performance Trade-off

Strategy Update characteristics Typical trade-off
Stochastic gradient descent One example per update Most updates, but the noisiest gradients
Mini-batch gradient descent b examples per update Balances noise, update frequency, and hardware utilization
Full-batch gradient descent The complete dataset per gradient Smoothest gradient, but each update is most expensive

A larger mini-batch generally improves hardware utilization and produces a more stable gradient, but each update costs more and may require a different learning rate. A smaller batch performs cheaper, more frequent updates, but Python or dispatch overhead can dominate and the gradient becomes noisier.

For a dense linear model, one gradient computation costs approximately O(b*d), where d is the number of features. Processing an epoch still costs approximately O(n*d), but mini-batching performs many more parameter updates than full-batch training.

Batch size has no universal, monotonic effect on predictive performance. Gradient noise can act as a regularizer, but generalization also depends on the learning rate, number of epochs, regularization, architecture, and dataset. Large batches often use GPUs more efficiently, while very small batches may leave CPU or GPU resources underused.

Best Practices

  • Tune batch size and learning rate using validation data or cross-validation, never the test set.

  • Shuffle training examples each epoch, particularly when data are ordered.

  • Scale features for gradient-based models and fit preprocessing only on training data.

  • Use StratifiedKFold for classification comparisons and TimeSeriesSplit for temporal data.

  • Track both the optimization loss and the evaluation metric; a model may train with log loss but be assessed using F1, ROC-AUC, or PR-AUC.

  • Record random seeds and relevant library versions for reproducibility.

Common Pitfalls

  • Data leakage: preprocessing, feature selection, or tuning must not use test data.

  • Wrong metric: accuracy can mislead with imbalanced classes; also inspect precision, recall, F1, ROC-AUC, or PR-AUC.

  • Numerical instability: clip probabilities before logarithms. In PyTorch, use torch.nn.BCEWithLogitsLoss, which computes stable binary cross-entropy directly from logits.

  • Poor batch or learning-rate choices: very large batches can exhaust memory, while very small batches may spend too much time in framework overhead.

  • Overfitting: compare training and validation performance, use regularization, and avoid repeatedly tuning against the test set.

19

What effect does the learning rate have on cost optimization?

The learning rate controls the magnitude of each parameter update during gradient-based optimization. A small rate can make convergence slow, while a large rate can cause oscillation or divergence. The main trade-off is convergence speed versus stability and the quality of the resulting solution.

Read full answer

Applies tomachine learning concepts; PyTorch 2.x for the logits-based loss reference

Loss, Cost, and Gradient Updates

  • Loss: The error for one training example.

  • Cost function: The average loss over the training data, optionally plus a regularization term.

  • Objective function: A broader term that may also include constraints or other penalties.

For a gradient g, a parameter parameter is updated as:

parameter = parameter - learning_rate * g

Gradient-based training requires derivatives of the objective with respect to its parameters. Smooth losses such as MSE provide these derivatives directly, while non-smooth losses such as MAE can use subgradients; at zero error, MAE’s derivative is undefined and implementations must handle that point deliberately.

Effects on Convergence

  • Too small: Updates are stable but may require many iterations, making training unnecessarily slow.

  • Too large: The optimizer may repeatedly cross a minimum, oscillate in a narrow valley, or jump beyond a useful region.

  • Extremely large: Parameters may diverge, producing infinity or NaN values.

  • Suitable rate: The cost generally decreases toward a local or global minimum at a practical pace.

  • Scheduled or adaptive rates: A larger rate can support early progress, while later reductions can improve stability near a minimum. Methods such as Adam adapt the effective step size using gradient and moment estimates.

The optimal rate depends on the objective’s curvature, gradient scale, batch size, feature scaling, and optimizer. In non-convex neural-network objectives, it can also influence which minimum or basin the training process reaches.

Common Cost Functions

Shared notation: n is the number of examples; i indexes examples; y_i is the target; y_hat_i is the prediction; p_i is the predicted probability of the positive class; k indexes classes; y_ik is the one-hot or probability target for class k; p_ik is the predicted probability for class k; f_i is a signed decision score; and e_i = y_i - y_hat_i.

Cost or regularization term Formula Typical use
MSE (1/n) * sum((y_i - y_hat_i)^2) Regression; penalizes large errors strongly
MAE (1/n) * sum(abs(y_i - y_hat_i)) Regression when robustness to outliers matters
Huber loss (1/n) * sum(h(e_i)) Regression balancing MSE smoothness and MAE robustness
Binary cross-entropy -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary classification
Categorical cross-entropy -(1/n) * sum_i sum_k(y_ik*log(p_ik)) Multiclass classification with softmax
Hinge loss (1/n) * sum(max(0, 1 - y_i*f_i)) Margin classifiers such as SVMs
KL divergence sum_x(P(x)*log(P(x)/Q(x))) Measuring how distribution Q differs from reference distribution P

For Huber loss, delta is the threshold at which the penalty changes from quadratic to linear:

h(e) = e^2 when abs(e) <= delta; otherwise h(e) = delta * (2*abs(e) - delta).

Binary targets are commonly 0 and 1. Categorical targets are class indices or one-hot vectors, and hinge loss conventionally uses targets -1 and +1. In KL divergence, x ranges over the support of P.

Regularization and Logistic Regression

A regularized objective can be written as average loss + regularization penalty:

  • L2 regularization: Adds lambda * sum(w_j^2). It discourages large weights and is used in ridge regression and commonly implemented through weight decay.

  • L1 regularization: Adds lambda * sum(abs(w_j)). It can drive weights exactly to zero, producing sparse models and underpinning lasso.

Here, w_j is parameter j, and lambda controls regularization strength.

Logistic regression normally uses binary cross-entropy because the resulting objective is convex in the linear model’s weights and provides well-behaved gradients. Applying MSE after a sigmoid generally creates a non-convex objective and can produce small gradients when predictions are saturated near 0 or 1.

Common Pitfalls

  • Divergence or oscillation: If the cost consistently rises or repeatedly jumps across a minimum, reduce the learning rate and inspect gradient scales and feature scaling.

  • Slow progress: Increase the rate cautiously or use a schedule, while monitoring whether the validation cost also improves.

  • Numerical instability: Manual sigmoid-then-log binary cross-entropy can saturate and lose numerical precision. In PyTorch 2.x, torch.nn.BCEWithLogitsLoss accepts raw logits and combines sigmoid and loss calculations stably.

  • Metric mismatch: A model may train on log loss while reporting F1 for an imbalanced classification problem. Precision, recall, PR-AUC, and ROC-AUC may provide additional insight.

  • Leakage: Fit preprocessing only on training folds, select the learning rate using validation folds or cross-validation, and keep the final test set untouched.

20

What is a learning rate schedule, and why is it important?

A learning rate schedule changes the optimizer’s step size during training, often decreasing it from a large initial value to smaller values. It balances fast early progress against stable fine-tuning near an optimum. This is important because a single fixed learning rate may converge slowly, overshoot, oscillate, or become stuck.

Read full answer

Applies tomachine learning concepts (all versions); PyTorch 2.x where specifically named

Core Idea

For gradient descent, the update is:

parameter[t+1] = parameter[t] - learning_rate[t] * gradient(parameter[t])

Here, parameter[t] is the model parameter at iteration t, gradient is its gradient, and learning_rate[t] is the step size selected by the schedule.

A simple exponential schedule is:

learning_rate[t] = initial_rate * decay^t

  • initial_rate: The starting learning rate.

  • decay: A factor between 0 and 1.

  • t: The current training iteration.

A loss measures the error for one example. A cost or objective usually averages losses across the dataset and may include regularization. Objective functions used with gradient descent should be differentiable because optimization follows their gradients; non-differentiable objectives require alternatives such as surrogate losses or subgradients.

Why It Matters

  • Stability: A smaller learning rate near the optimum reduces oscillation and overshooting.

  • Convergence speed: Starting too small wastes iterations, while starting too large can destabilize training.

  • Feature scale: Gradient-based methods usually converge faster when numerical features have comparable scales.

  • Sparsity: L1 regularization can set some weights exactly to zero; ordinary L2 weight decay generally shrinks them without making the model sparse.

  • Metric alignment: The training objective and reported evaluation metric may differ. For example, optimize log loss while reporting F1 when classes are imbalanced.

Logistic regression normally uses binary cross-entropy rather than MSE because it matches probabilistic classification, penalizes confident incorrect predictions strongly, and is convex in the weights. Its gradients also behave better than those obtained by treating binary outputs as ordinary regression targets.

Common Schedules

Schedule Behavior Typical use
Step decay Drops the rate at selected epochs General image and language models
Exponential decay Multiplies the rate by a factor each step Simple stable training
Cosine decay Smoothly reduces the rate toward a minimum Training neural networks
Plateau scheduler Reduces the rate after validation improvement stops Adaptive fine-tuning
Warmup Starts at a small rate and gradually increases it Large-scale or transformer training

Cost Functions and Regularization

Cost Formula When to use
MSE (1/n) * sum_i((y_i - y_hat_i)^2) Regression; penalizes large errors more
MAE (1/n) * sum_i(abs(y_i - y_hat_i)) Regression with outliers
Huber (0.5*delta*r^2) if abs(r) <= delta, otherwise delta*(abs(r) - delta/2) Regression balancing MSE and MAE
Binary cross-entropy (-1/n) * sum_i(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary and probabilistic classification
Categorical cross-entropy -sum_i(sum_k(y_ik*log(p_ik))) Multiclass classification with softmax
Hinge loss (1/n) * sum_i(max(0, 1 - y_i*f(x_i))) SVMs and margin maximization

| KL divergence | KL(P || Q) = sum_x(P(x)*log(P(x)/Q(x))) | Comparing probability distributions |

In these formulas, n is the number of examples, i indexes examples, k indexes classes, and y is the true value or label. For binary cross-entropy, y is 0 or 1 and p is a predicted probability; for hinge loss, y is -1 or +1 and f(x) is a signed classifier score. In the Huber formula, r = y - y_hat is the residual and delta is the transition threshold. In the KL formula, x indexes an outcome, while P and Q are probability distributions.

Regularized objectives add:

  • L2 regularization: cost + lambda * sum(w^2), also called ridge or weight decay.

  • L1 regularization: cost + lambda * sum(abs(w)), also called lasso.

Here, lambda is a nonnegative regularization strength and w is a model parameter. L2 shrinks parameters continuously, while L1 can set parameters exactly to zero and therefore produce sparse models.

Common Pitfalls

  • A decaying schedule does not compensate for unscaled features or an unsuitable objective.

  • Never tune the schedule repeatedly against the test set; use validation data or cross-validation.

  • Accuracy can be misleading for imbalanced classes; consider precision, recall, F1, ROC-AUC, or PR-AUC.

  • MSE can be dominated by outliers; consider MAE or Huber loss.

  • Avoid evaluating log(0) directly. In PyTorch 2.x, prefer logits-based torch.nn.BCEWithLogitsLoss, which combines sigmoid and binary cross-entropy in a numerically stable operation.

  • A lower training cost does not necessarily mean better validation performance; monitor the metric relevant to the business problem.

21

How do bias and variance trade off during cost optimization?

The bias–variance trade-off describes how model capacity and regularization affect systematic error during optimization. Simple, strongly regularized models generally have high bias and low variance, while flexible models generally have low bias and high variance. Minimizing training cost therefore does not necessarily improve generalization.

Read full answer

Applies tomachine learning concepts (examples in NumPy 2.x and scikit-learn 1.5+, Python 3.12)

Bias and Variance During Optimization

The standard identity describes expected squared prediction error, averaging over possible training sets:

Expected squared error = Bias² + Variance + irreducible noise

  • Bias²: Difference between the model’s expected prediction and the true relationship.

  • Variance: Sensitivity of predictions to changes in the training sample.

  • Irreducible noise: Random variation that cannot be predicted reliably.

Underfitting indicates excessive bias, while overfitting indicates excessive variance. Increasing capacity or reducing regularization usually lowers bias but raises variance; stronger regularization does the reverse.

A loss is the error for one example, (l_i). A cost aggregates losses and may include regularization:

J(θ) = (1/n) * sum(l_i) + λ * R(θ)

Here, (n) is the number of examples, (theta) contains the parameters, (lambda) is the regularization strength, and (R(theta)) is the penalty. Gradient-based training requires a differentiable objective; non-smooth losses such as MAE and hinge loss have subgradients where ordinary derivatives do not exist.

Common Cost Functions

Loss Formula Typical use
MSE (1/n) * sum(e_i^2) Regression; strongly penalizes large errors
MAE (1/n) * sum(abs(e_i)) Regression with outliers
Huber 0.5*e^2 when abs(e) <= δ, otherwise δ*(abs(e) - 0.5*δ) Regression combining MSE and MAE behavior
Binary cross-entropy -mean(y*log(p) + (1-y)*log(1-p)) Binary probabilistic classification
Categorical cross-entropy -sum(y_k*log(p_k)) Multiclass classification with softmax
Hinge loss max(0, 1 - y*f(x)) Margin-based models such as SVMs

| KL divergence | D_KL(p || q) = sum_x(p(x)*log(p(x)/q(x))) | Comparing probability distributions |

Here, (e_i=y_i-hat{y}_i) is an error, (y) is the observed target, (hat{y}) is the prediction, (p) is a predicted probability, (y_k) is a class indicator, (p_k) is a class probability, and (K) is the number of classes. For hinge loss, (yin{-1,+1}) and (f(x)) is the model score. For KL divergence, (x) indexes the support and (p(x),q(x)) are distributions; KL divergence is asymmetric.

Logistic regression uses cross-entropy because it corresponds to maximum likelihood and is convex in the weights. Squared error with sigmoid predictions can yield very small gradients when predictions are saturated.

Regularization and Model Selection

  • L2 or ridge: Adds λ * sum(w²), continuously shrinking weights.

  • L1 or lasso: Adds λ * sum(abs(w)), potentially producing exactly zero weights and sparse models.

Increasing λ generally raises bias and lowers variance. Select capacity and regularization using cross-validation and validation metrics, not training cost alone; reserve the final test set for final evaluation.

Code Example

import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler


def mse(y, y_pred):
    return np.mean((y - y_pred) ** 2)


def mse_gradient(X, y, weights, bias):
    errors = X @ weights + bias - y
    return 2.0 / len(y) * X.T @ errors, 2.0 / len(y) * errors.sum()


def sigmoid(values):
    return 0.5 * (1.0 + np.tanh(0.5 * values))


def binary_cross_entropy(y, probabilities):
    probabilities = np.clip(probabilities, 1e-12, 1.0 - 1e-12)
    return -np.mean(
        y * np.log(probabilities)
        + (1.0 - y) * np.log1p(-probabilities)
    )


def bce_gradient(X, y, weights, bias):
    errors = sigmoid(X @ weights + bias) - y
    return X.T @ errors / len(y), errors.mean()


# Regression: split before fitting the scaler.
rng = np.random.default_rng(42)
X_reg = rng.normal(size=(400, 5))
y_reg = X_reg @ rng.normal(size=5) + rng.normal(scale=0.1, size=400)
X_train, X_test, y_train, y_test = train_test_split(
    X_reg, y_reg, test_size=0.2, random_state=42
)

reg_scaler = Pipeline([("scaler", StandardScaler())])
X_train = reg_scaler.fit_transform(X_train)
X_test = reg_scaler.transform(X_test)

weights = np.zeros(X_train.shape[1])
bias = 0.0
for step in range(301):
    if step % 100 == 0:
        print(f"MSE step {step}: {mse(y_train, X_train @ weights + bias):.4f}")
    grad_weights, grad_bias = mse_gradient(X_train, y_train, weights, bias)
    weights -= 0.05 * grad_weights
    bias -= 0.05 * grad_bias

print(f"Validation MSE: {mse(y_test, X_test @ weights + bias):.4f}")


# Classification: split and fit preprocessing on training data only.
X_clf, y_clf = make_classification(
    n_samples=600,
    n_features=8,
    n_informative=5,
    n_redundant=0,
    random_state=42,
)
X_train, X_test, y_train, y_test = train_test_split(
    X_clf, y_clf, test_size=0.2, random_state=42, stratify=y_clf
)

clf_scaler = Pipeline([("scaler", StandardScaler())])
X_train = clf_scaler.fit_transform(X_train)
X_test = clf_scaler.transform(X_test)

weights = np.zeros(X_train.shape[1])
bias = 0.0
for step in range(301):
    if step % 100 == 0:
        probabilities = sigmoid(X_train @ weights + bias)
        print(f"BCE step {step}: {binary_cross_entropy(y_train, probabilities):.4f}")
    grad_weights, grad_bias = bce_gradient(X_train, y_train, weights, bias)
    weights -= 0.1 * grad_weights
    bias -= 0.1 * grad_bias

probabilities = sigmoid(X_test @ weights + bias)
print(f"Validation BCE: {binary_cross_entropy(y_test, probabilities):.4f}")

Common Pitfalls

  • Confusing training cost with generalization: Compare validation performance and use learning curves.

  • Using the wrong evaluation metric: Log loss may train a model well while F1 is the business metric; accuracy can mislead on imbalanced classes.

  • Leaking information: Fit scalers, encoders, and imputers only on training folds.

  • Numerical instability: Clip probabilities before logarithms or use logits-based losses such as torch.nn.BCEWithLogitsLoss.

  • Repeatedly tuning on the test set: Use cross-validation and keep the final test set untouched until evaluation.

22

How do vanishing and exploding gradients affect cost functions?

Vanishing gradients make the cost function’s parameter gradients extremely small, so early layers learn slowly and training may appear stalled. Exploding gradients make those gradients excessively large, causing unstable updates, oscillating loss, divergence, or non-finite values. The trade-off is that clipping cheaply limits explosions but does not cure vanishing gradients, while architectural remedies can improve gradient flow at the cost of greater model complexity.

Read full answer

Applies tomachine learning concepts with NumPy 2.x and PyTorch 2.x (Python 3.12)

Effects on Cost Optimization

Backpropagation uses the chain rule:

early_gradient = upstream_gradient × local_derivative_1 × ... × local_derivative_L

Here, upstream_gradient is the signal arriving from a later layer, each local_derivative is a layer’s sensitivity to its inputs, and L is the number of layers.

  • Vanishing gradient: Repeated multiplication by small derivatives makes the product approach zero. Parameter updates become tiny even though the cost may still be improvable.

  • Exploding gradient: Repeated multiplication by large derivatives makes the product grow rapidly. Updates can overshoot the useful region, and the loss may oscillate or diverge.

  • Cost-function influence: MSE derivatives grow with prediction errors, while saturated probability losses can produce extremely small derivatives for confident predictions.

Common responses include suitable initialization, ReLU-family activations, residual connections, normalization, and a smaller learning rate. Global-norm gradient clipping bounds the gradient used by the optimizer, but it does not guarantee bounded optimizer steps and does not fix vanishing gradients.

Cost Functions and Formulas

A loss usually measures one example’s error. A cost function or objective averages losses across examples and may add regularization. Gradient-based training requires differentiability with respect to parameters, or a valid subgradient for a nonsmooth objective such as L1 loss.

In the following formulas, n is the number of examples, i identifies an example or distribution entry, c identifies a class, C is the number of classes, y is the target, y_hat is the prediction, p is a probability, f is a real-valued score, and w_j is parameter j.

Cost or loss Formula When to use
Mean squared error MSE = (1/n) * sum((y_i - y_hat_i)^2) Regression; penalizes large errors more strongly and has smooth derivatives.
Mean absolute error MAE = (1/n) * sum(abs(y_i - y_hat_i)) Regression when outliers should have less influence.
Huber loss H_delta(r) = 0.5*r^2 when abs(r) <= delta, otherwise delta*(abs(r) - 0.5) Regression balancing smooth squared-error gradients with outlier robustness; r is the residual and delta is the threshold.
Binary cross-entropy BCE = -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary or multilabel classification, where p_i is the class-1 probability.
Categorical cross-entropy CE = -(1/n) * sum_i sum_c(y_{i,c}*log(p_{i,c})) Multiclass classification with mutually exclusive classes and softmax probabilities.
Hinge loss (1/n) * sum(max(0, 1-y_i*f_i)) Margin-based classification such as linear SVMs.

| KL divergence | KL(P || Q) = sum(P_i*log(P_i/Q_i)) | Matching target and predicted distributions, such as knowledge distillation; P is the target distribution and Q is the prediction distribution. |

Regularization and Logistic Regression

A regularized objective is:

J(w) = C_data(w) + lambda*R(w)

C_data(w) is the data cost, R(w) is the penalty, and lambda controls penalty strength.

  • L2 regularization: R(w) = sum(w_j^2). It continuously shrinks parameters and is used by ridge regression and weight decay.

  • L1 regularization: R(w) = sum(abs(w_j)). Its subgradient can set parameters exactly to zero, producing sparse models.

Logistic regression uses binary cross-entropy because it is convex in the weights and evaluates the probabilistic model directly. Its gradient with respect to a logit is proportional to p-y; squared error over sigmoid probabilities includes an additional p*(1-p) factor, which can make gradients extremely small for confident wrong predictions and is not generally convex in the weights.

Code Example

import numpy as np

rng = np.random.default_rng(42)


def mse(y, predictions):
    return np.mean((y - predictions) ** 2)


def mse_gradients(X, y, weights, bias):
    predictions = X @ weights + bias
    error = predictions - y
    weight_gradient = (2.0 / len(y)) * X.T @ error
    bias_gradient = (2.0 / len(y)) * np.sum(error)
    return weight_gradient, bias_gradient


def sigmoid(logits):
    # Numerically stable sigmoid without changing its analytical derivative.
    return np.exp(-np.logaddexp(0.0, -logits))


def binary_cross_entropy(y, probabilities, epsilon=1e-12):
    # Clip probabilities to avoid log(0) and log(1).
    probabilities = np.clip(probabilities, epsilon, 1.0 - epsilon)
    return -np.mean(
        y * np.log(probabilities)
        + (1.0 - y) * np.log(1.0 - probabilities)
    )


def bce_gradients(X, y, weights, bias):
    probabilities = sigmoid(X @ weights + bias)
    logit_gradient = (probabilities - y) / len(y)
    weight_gradient = X.T @ logit_gradient
    bias_gradient = np.sum(logit_gradient)
    return weight_gradient, bias_gradient


# Regression with MSE.
X_reg = rng.normal(size=(200, 1))
y_reg = 3.0 * X_reg[:, 0] + 2.0 + rng.normal(scale=0.2, size=200)
train_reg = rng.random(y_reg.size) < 0.8
X_train, y_train = X_reg[train_reg], y_reg[train_reg]
X_valid, y_valid = X_reg[~train_reg], y_reg[~train_reg]

weights = np.zeros(X_train.shape[1])
bias = 0.0
initial_mse = mse(y_train, X_train @ weights + bias)

for _ in range(400):
    weight_gradient, bias_gradient = mse_gradients(
        X_train, y_train, weights, bias
    )
    weights -= 0.05 * weight_gradient
    bias -= 0.05 * bias_gradient

final_mse = mse(y_train, X_train @ weights + bias)
print(f"MSE train: {initial_mse:.4f} -> {final_mse:.4f}")
print(f"MSE validation: {mse(y_valid, X_valid @ weights + bias):.4f}")
assert final_mse < initial_mse

# Binary classification with BCE.
X_cls = rng.normal(size=(200, 2))
y_cls = (
    X_cls[:, 0] + 0.5 * X_cls[:, 1] + rng.normal(scale=0.2, size=200) > 0
).astype(float)
train_cls = rng.random(y_cls.size) < 0.8
X_train, y_train = X_cls[train_cls], y_cls[train_cls]
X_valid, y_valid = X_cls[~train_cls], y_cls[~train_cls]

weights = np.zeros(X_train.shape[1])
bias = 0.0
initial_bce = binary_cross_entropy(y_train, sigmoid(X_train @ weights + bias))

for _ in range(400):
    weight_gradient, bias_gradient = bce_gradients(
        X_train, y_train, weights, bias
    )
    weights -= 0.1 * weight_gradient
    bias -= 0.1 * bias_gradient

final_bce = binary_cross_entropy(y_train, sigmoid(X_train @ weights + bias))
print(f"BCE train: {initial_bce:.4f} -> {final_bce:.4f}")
print(
    "BCE validation: "
    f"{binary_cross_entropy(y_valid, sigmoid(X_valid @ weights + bias)):.4f}"
)
assert final_bce < initial_bce

Common Pitfalls

  • Treating clipping as a complete fix: It limits gradient magnitude but does not resolve vanishing gradients or guarantee bounded optimizer steps.

  • Using numerically unstable BCE: Prefer a logits-based implementation such as PyTorch 2.x’s torch.nn.BCEWithLogitsLoss.

  • Confusing training loss with evaluation metrics: A classifier may train with log loss but report F1, precision, recall, ROC-AUC, or PR-AUC. Accuracy can mislead with imbalanced classes.

  • Overfitting the validation set: Use validation results for model selection, not repeated manual tuning, and use cross-validation when appropriate.

  • Leaking data: Fit preprocessing only on training data and keep validation data isolated.

23

How do you handle non-convex cost functions in optimization?

A non-convex cost function is an objective whose landscape may contain local minima, saddle points, or other stationary points, so gradient-based training can stop away from the global minimum. Handle it with careful initialization, learning-rate control, restarts or global search, and validation-based selection. The main trade-off is exploration versus stable, reproducible convergence.

Read full answer

Applies tomachine learning concepts (examples in NumPy 2.x with Python 3.12)

Loss, Cost, and Differentiability

A loss measures one example’s error; the cost aggregates losses and may add regularization:

cost = (1/n) * sum(loss(y[i], y_hat[i])) + regularization

Here, n is the sample count, y and y_hat are targets and predictions, and regularization is an optional penalty. A differentiable objective permits gradient-based updates; non-differentiable objectives require alternatives such as subgradients, smoothing, or derivative-free methods.

Common Cost Functions

Let p be a probability, z its logit, f a signed margin, q a target distribution, k a class index, e = y - y_hat, delta the Huber threshold, and lambda the regularization strength.

Cost Formula When to use
Mean squared error (MSE) (1/n) * sum((y - y_hat)^2) Regression; strongly penalizes large errors
Mean absolute error (MAE) (1/n) * sum(abs(y - y_hat)) Regression with outliers; less sensitive to them
Huber loss sum(huber(e, delta)), where huber(e, delta) = 0.5*e^2 if abs(e) <= delta, otherwise delta*(abs(e) - 0.5*delta) Regression balancing MSE smoothness and MAE robustness
Binary cross-entropy -(1/n) * sum(y*log(p) + (1-y)*log(1-p)) Binary or multilabel classification
Categorical cross-entropy -(1/n) * sum(sum(q[k] * log(p[k]))) Multiclass classification with softmax outputs
Hinge loss (1/n) * sum(max(0, 1-y*f)), with y in {-1, 1} Linear SVMs and margin maximization
KL divergence sum(q[k] * log(q[k]/p[k])) Comparing distributions or regularizing probabilistic outputs

Logistic regression normally uses binary cross-entropy because its regularized objective is convex in the weights and BCE provides more useful gradients than MSE. With respect to a logit, BCE’s gradient is p - y; MSE introduces an additional p*(1-p) factor that can vanish near p = 0 or p = 1.

Regularization and Optimization Strategy

Regularized objectives often add lambda * sum(w^2) for L2 regularization, also called ridge or standard weight decay, or lambda * sum(abs(w)) for L1 regularization, also called lasso. Here, w is the weight vector; L1 can set some weights exactly zero, producing sparse models.

For non-convex optimization:

  • Standardize features when their scales differ, especially before gradient descent.

  • Use SGD with momentum, Adam, or AdamW to stabilize updates, but do not assume they escape local optima.

  • Tune learning rates with schedules, try several seeds, and restart from multiple initializations.

  • For low-dimensional problems, consider simulated annealing or another global-search method; grid and random search can tune initializations or hyperparameters.

  • Use appropriate regularization, early stopping, and validation metrics rather than assuming the lowest training cost generalizes best.

Dense batch gradients typically cost O(n*d), while an individual example’s gradient costs O(d). GPUs help when the data and model computations are sufficiently large.

Code Example

import numpy as np

rng = np.random.default_rng(42)


def mse_loss(y, predictions):
    return np.mean((y - predictions) ** 2)


def mse_gradient(X, y, weights):
    predictions = X @ weights
    return (2.0 / len(y)) * (X.T @ (predictions - y))


def sigmoid(values):
    values = np.asarray(values, dtype=float)
    result = np.empty_like(values)
    positive = values >= 0
    result[positive] = 1.0 / (1.0 + np.exp(-values[positive]))
    exp_values = np.exp(values[~positive])
    result[~positive] = exp_values / (1.0 + exp_values)
    return result


def binary_cross_entropy(y, probabilities):
    probabilities = np.clip(probabilities, 1e-15, 1.0 - 1e-15)
    return -np.mean(
        y * np.log(probabilities)
        + (1.0 - y) * np.log(1.0 - probabilities)
    )


def bce_from_logits(y, logits):
    return binary_cross_entropy(y, sigmoid(logits))


def bce_gradient(X, y, weights):
    return X.T @ (sigmoid(X @ weights) - y) / len(y)


def fit(loss, gradient, X, y, learning_rate, steps):
    weights = np.zeros(X.shape[1])
    initial = loss(y, X @ weights)
    for _ in range(steps):
        weights -= learning_rate * gradient(X, y, weights)
    return weights, initial, loss(y, X @ weights)


# Regression: fit only on the training split.
X = rng.normal(size=(100, 1))
X = np.column_stack([np.ones(len(X)), X])
y = X @ np.array([2.0, 3.0]) + rng.normal(scale=0.1, size=len(X))
order = rng.permutation(len(y))
train, valid = order[:80], order[80:]
weights, initial, final = fit(
    mse_loss, mse_gradient, X[train], y[train], 0.05, 100
)
mae = np.mean(np.abs(y[valid] - X[valid] @ weights))
print(f"MSE train cost: {initial:.6f} -> {final:.6f}")
print(f"Regression validation MAE: {mae:.6f}")

# Binary classification: fit only on the training split.
X_binary = rng.normal(size=(200, 1))
X_binary = np.column_stack([np.ones(len(X_binary)), X_binary])
probability = sigmoid(0.8 * X_binary[:, 1] + 0.2)
y_binary = (probability >= 0.5).astype(float)
order = rng.permutation(len(y_binary))
train, valid = order[:160], order[160:]
weights, initial, final = fit(
    bce_from_logits,
    bce_gradient,
    X_binary[train],
    y_binary[train],
    0.1,
    200,
)
predicted = (sigmoid(X_binary[valid] @ weights) >= 0.5).astype(float)
true_positive = np.sum((predicted == 1) & (y_binary[valid] == 1))
false_positive = np.sum((predicted == 1) & (y_binary[valid] == 0))
false_negative = np.sum((predicted == 0) & (y_binary[valid] == 1))
f1 = 2 * true_positive / max(
    2 * true_positive + false_positive + false_negative, 1
)
print(f"BCE train cost: {initial:.6f} -> {final:.6f}")
print(f"Classification validation F1: {f1:.6f}")


# Non-convex example: compare several random restarts.
def quartic_cost(weight):
    return (weight**2 - 1) ** 2


def quartic_gradient(weight):
    return 4 * weight * (weight**2 - 1)


restart_costs = []
for start in rng.uniform(-2.0, 2.0, 8):
    weight = float(start)
    for _ in range(100):
        weight -= 0.05 * quartic_gradient(weight)
    restart_costs.append(quartic_cost(weight))

print(f"Quartic best restart cost: {min(restart_costs):.6f}")

Common Pitfalls

  • Using one initialization and assuming it found the global minimum.

  • Setting the learning rate too high, causing divergence, or too low, causing slow convergence.

  • Reporting training cost instead of an appropriate evaluation metric. For example, train with log loss but report F1 for imbalanced classification; accuracy can be misleading.

  • Computing log(0). Clip probabilities or, in PyTorch, use torch.nn.BCEWithLogitsLoss for a numerically stable logits-based implementation.

  • Tuning against the test set or repeatedly selecting checkpoints on validation data, which eventually causes validation overfitting.

24

How does momentum accelerate cost function convergence?

Momentum accelerates gradient descent by accumulating velocity from recent gradients, making movement more consistent in steep valleys and reducing oscillation across narrow directions. It usually reaches a minimum in fewer updates, but an unsuitable momentum coefficient can cause overshooting, unstable oscillations, or reliance on stale gradients. Momentum changes the optimization path, not the cost function itself.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12 with NumPy 2.x)

Cost Functions and Differentiability

A loss measures the error for one example, while a cost or objective aggregates losses across the training data and may add regularization. Gradient-based training requires the objective to be differentiable so that its gradient can be calculated.

For example, a regularized objective is:

J(w) = mean loss + λ * R(w)

  • J(w): total training objective.

  • w: model parameters.

  • λ: regularization strength.

  • R(w): penalty on parameter size.

Momentum Update

A common momentum formulation is:

g_t = gradient of J(w_t)

v_t = β * v_(t-1) + g_t

w_(t+1) = w_t - η * v_t

  • t: optimization step.

  • g_t: gradient at the current parameters.

  • β: momentum coefficient, commonly 0.9.

  • v_t: accumulated velocity.

  • η: learning rate.

When gradients point consistently downhill, velocity grows and movement accelerates. In oscillating directions, positive and negative contributions partially cancel. Some frameworks scale the gradient by 1 - β; this changes the effective learning rate but preserves the same basic behavior.

Common Cost Functions

Here, n is the number of examples, y is the true value, y_hat is the prediction, p is a predicted probability, k denotes a class, and z is an SVM margin.

Function Formula Typical use
Mean squared error MSE = (1/n) * sum((y - y_hat)^2) Regression; penalizes large errors heavily
Mean absolute error MAE = (1/n) * sum(abs(y - y_hat)) Regression with outliers; less sensitive than MSE
Huber loss Quadratic for errors below δ, linear above δ Combines MSE smoothness with MAE robustness
Binary cross-entropy BCE = -(1/n) * sum(y*log(p) + (1-y)*log(1-p)) Binary and probabilistic classification
Categorical cross-entropy CCE = -(1/n) * sum_i sum_k(y_ik*log(p_ik)) Multiclass classification with softmax
Hinge loss (1/n) * sum(max(0, 1-z)) Linear SVM classification
KL divergence sum(p(x)*log(p(x)/q(x))) Comparing probability distributions

Regularization commonly uses:

  • L2 or ridge: λ * sum(w_j^2); discourages large weights.

  • L1 or lasso: λ * sum(abs(w_j)); can drive some weights exactly to zero, producing sparse models.

Why Logistic Regression Uses Cross-Entropy

Cross-entropy is convex with respect to logistic-regression weights and produces stronger, more informative gradients than applying MSE to binary labels. Its gradient contains the p * (1-p) relationship naturally, whereas MSE-based gradients can become very small for confident predictions, slowing learning. In practice, implementations such as torch.nn.BCEWithLogitsLoss combine sigmoid and binary cross-entropy in a numerically stable operation.

Common Pitfalls

  • Incorrect loss choice: MSE punishes outliers more than MAE; classification normally uses cross-entropy.

  • Unstable momentum: values near 1 create strong inertia, while values near 0 behave like ordinary gradient descent.

  • Overshooting and stale gradients: momentum can continue in an old direction after the landscape changes.

  • Numerical instability: use logits-based BCE implementations such as BCEWithLogitsLoss.

  • Metric mismatch: training on cross-entropy does not require reporting cross-entropy; imbalanced classification should also report precision, recall, F1, ROC-AUC, and PR-AUC.

  • Data leakage: fit preprocessing only on training folds and reserve validation data for model selection.

Typical follow-up questions ask how momentum differs from weight decay, when to use Adam instead, whether it changes convergence guarantees, and how learning rate and momentum interact.

25

How do adaptive learning rate algorithms improve optimization?

Adaptive learning-rate algorithms change the effective optimization step using gradient history, allowing parameters with different gradient scales to receive appropriately scaled updates. They often accelerate early training and reduce sensitivity to the initial learning rate, but they add optimizer state and hyperparameters and do not guarantee better generalization. Their main trade-off is faster, more robust step sizing versus greater complexity and tuning requirements.

Read full answer

Applies tomachine learning optimization concepts (all versions); PyTorch 2.x for `BCEWithLogitsLoss`

How Adaptive Learning Rates Work

Standard gradient descent uses one learning rate for every parameter. Adaptive methods use accumulated gradient information to scale each parameter’s update:

  • AdaGrad: Divides updates by the square root of the accumulated squared gradient. This suits sparse features, but repeated updates to the same feature can reduce its learning rate excessively.

  • RMSProp: Uses an exponential moving average of squared gradients, so only recent gradient scales strongly influence each update.

  • Adam: Combines a moving average of gradients, which provides momentum, with a moving average of squared gradients, which provides per-parameter scaling. It also applies bias correction to its early moment estimates.

  • AdamW: Performs weight decay separately from the adaptive gradient update rather than adding an L2 penalty to the gradient.

These methods improve optimization by normalizing updates, reducing oscillation caused by inconsistent gradient magnitudes, and preserving useful learning rates for parameters that receive gradients infrequently. Learning-rate schedules are different: they usually vary one global rate over time, whereas adaptive optimizers scale rates across parameters.

Cost Functions and Differentiable Objectives

A loss measures the error for one example. A cost function or objective aggregates those losses across the data and may include regularization:

J(w) = (1/n) * sum_i(L_i) + lambda * R(w)

Here, n is the number of examples, i is an example index, L_i is its loss, w is the parameter vector, R(w) is a regularization penalty, and lambda controls the penalty’s strength. Gradient-based training requires a differentiable objective so that parameter gradients can be computed efficiently; nonsmooth objectives require subgradients or another optimization method.

Function Formula Typical use
MSE (1/n) * sum_i((y_i - y_hat_i)^2) Regression; heavily penalizes large errors
MAE (1/n) * sum_i(abs(y_i - y_hat_i)) Regression that is more robust to outliers
Huber loss H_delta(z) = z^2/2 when abs(z) <= delta; otherwise delta * (abs(z) - delta/2) Robust regression with a smooth transition
Binary cross-entropy -(1/n) * sum_i(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary classification
Categorical cross-entropy -(1/n) * sum_i sum_k(y_ik*log(p_ik)) Multiclass classification with softmax
Hinge loss (1/n) * sum_i(max(0, 1 - y_i*f(x_i))) Margin-based classifiers such as SVMs
KL divergence sum_x(P(x) * log(P(x)/Q(x))) Comparing probability distributions

Here, y_i is the target, y_hat_i is the prediction, p_i is a predicted probability, z is the residual y_i - y_hat_i, and delta is the Huber threshold. For categorical loss, k is a class index. For hinge loss, y_i is -1 or +1, x_i is an input, and f(x_i) is the classifier score. In KL divergence, x ranges over the distributions’ common support, while P and Q are the compared distributions.

Common regularizers add:

  • L2 or ridge: lambda * sum_j(w_j^2), shrinking weights continuously.

  • L1 or lasso: lambda * sum_j(abs(w_j)), which can drive some weights to zero and produce a sparse solution.

Here, j is a parameter index. Logistic regression uses cross-entropy because its objective is convex in the weights and avoids MSE’s severe gradient attenuation when sigmoid probabilities saturate near 0 or 1.

Algorithm Comparison

Algorithm How it works Strengths Weaknesses Typical use or scale
AdaGrad Scales by accumulated squared gradients Effective for sparse parameters Learning rates can decay excessively Sparse linear models and embeddings
RMSProp Scales by recent squared-gradient averages Adapts quickly to changing gradient scales Can oscillate without momentum Recurrent and noisy objectives
Adam Combines momentum and adaptive scaling Widely used and robust across many deep-learning tasks Adds hyperparameters and state Deep learning at varying model scales
AdamW Uses adaptive updates with decoupled weight decay Clearer separation of optimization and regularization Decay still requires tuning Modern neural-network training

For n examples and d features, computing a dense full-batch gradient costs O(n*d). Optimizer-state arithmetic costs O(d), and Adam, AdamW, RMSProp, and AdaGrad generally require O(d) additional state for dense parameters.

Practical Guidance

  • Scale features for gradient-based models; scaling is generally unnecessary for tree-based models.

  • Split data before fitting scalers, encoders, imputers, or feature selectors to prevent leakage.

  • Handle missing values explicitly; an adaptive optimizer does not make missing data valid.

  • For imbalanced classification, consider precision, recall, F1, PR-AUC, and ROC-AUC rather than accuracy alone.

  • The training objective and evaluation metric may differ: a model can optimize log loss but be evaluated with F1.

  • Use fixed seeds and controlled data splits for reproducibility. On GPUs, adaptive optimizers are usually practical, although their extra arithmetic can matter for very small models.

Common Pitfalls

  • Training loss is not generalization: A decreasing loss does not prove that validation or test performance improved.

  • Excessive AdaGrad decay: Updates can become nearly zero; monitor per-parameter learning rates when this occurs.

  • Poor learning-rate selection: Adam and its variants still require tuning the base learning rate.

  • Wrong objective: MSE is generally inappropriate for classification when probabilities or class margins matter.

  • Numerical instability: In PyTorch 2.x, torch.nn.BCEWithLogitsLoss computes binary cross-entropy from logits more stably than manually applying sigmoid and logarithms separately.

  • Interview follow-up: Why does Adam need bias correction, and how does AdamW differ from Adam with an L2 penalty added to the gradient?

26

How do you implement Mean Squared Error in Python with NumPy?

Mean Squared Error (MSE) is the average squared difference between predicted and actual values. It supplies a differentiable objective for regression, while its heavy penalty on large errors makes it sensitive to outliers.

Read full answer

Applies toNumPy 2.x, scikit-learn 1.5+, and PyTorch 2.x (Python 3.12)

Loss Versus Cost

A loss measures the error for one example; the squared-error loss for sample i is (y_i - y_hat_i)^2. A cost function aggregates loss across the dataset and may add regularization:

MSE = (1/n) * sum((y_i - y_hat_i)^2)

Here, n is the number of examples, i indexes them, y_i is the actual target, and y_hat_i is the prediction. Gradient-based training requires an objective differentiable with respect to the model parameters; MSE supplies gradients indicating how predictions should change to reduce error.

Common Cost Functions

Cost Formula When to use Main trade-off
MSE (1/n) * sum((y_i - y_hat_i)^2) Regression Heavily penalizes large errors
MAE (1/n) * sum(abs(y_i - y_hat_i)) Regression with outliers Robust but nondifferentiable at zero
Huber loss L_delta(e) = 0.5 * e^2 when abs(e) <= delta; otherwise delta * (abs(e) - 0.5 * delta) Regression with some outliers Combines quadratic and linear behavior
Binary cross-entropy -(1/n) * sum(y_i * log(p_i) + (1-y_i) * log(1-p_i)) Binary classification Requires numerical care around zero and one
Categorical cross-entropy -(1/n) * sum_i sum_k(y_ik * log(p_ik)) Multiclass classification Rewards high probability for the target class
Hinge loss (1/n) * sum_i max(0, 1 - y_i * f(x_i)) Linear SVM classification Focuses on maintaining a classification margin

| KL divergence | KL(P || Q) = sum_k(P_k * log(P_k / Q_k)) | Distribution matching and distillation | Can be infinite when Q lacks support where P has probability |

For Huber loss, e = y - y_hat and delta > 0 is the transition threshold. For binary cross-entropy, p_i is the predicted positive-class probability and y_i is its binary label. For categorical cross-entropy, i indexes examples, k indexes classes, y_ik is the target probability, and p_ik is the predicted probability. For hinge loss, y_i is either -1 or +1, x_i is a feature vector, and f(x_i) is a real-valued decision score. For KL divergence, P is the target distribution, Q is the approximating distribution, and k indexes their components.

Logistic regression commonly uses cross-entropy because its objective is convex in the weights. Compared with squared error on sigmoid outputs, the cross-entropy gradient does not contain the extra sigmoid-derivative factor, which becomes small for saturated predictions.

Regularized Costs

Regularization discourages an overly complex fit:

  • L2 regularization: regularized_cost = MSE + lambda * sum(w_j^2)

  • L1 regularization: regularized_cost = MSE + lambda * sum(abs(w_j))

Here, lambda controls penalty strength and w_j is weight j. L2, also called ridge regularization or weight decay, shrinks weights continuously. L1, also called lasso regularization, can set some weights exactly to zero and therefore produce sparse models.

Code Example

import numpy as np


def mean_squared_error(
    y_true: np.ndarray, y_pred: np.ndarray
) -> float:
    """Return the mean squared error between actual and predicted values."""
    y_true = np.asarray(y_true, dtype=np.float64)
    y_pred = np.asarray(y_pred, dtype=np.float64)

    if y_true.shape != y_pred.shape:
        raise ValueError("y_true and y_pred must have identical shapes")
    if y_true.size == 0:
        raise ValueError("Inputs must not be empty")

    return float(np.mean(np.square(y_true - y_pred)))


def mse_gradient(
    y_true: np.ndarray, y_pred: np.ndarray
) -> np.ndarray:
    """Return the MSE gradient with respect to predicted values."""
    y_true = np.asarray(y_true, dtype=np.float64)
    y_pred = np.asarray(y_pred, dtype=np.float64)
    error = y_pred - y_true
    return (2.0 / error.size) * error


def binary_cross_entropy(
    y_true: np.ndarray, y_probability: np.ndarray
) -> float:
    """Return mean binary cross-entropy for predicted probabilities."""
    y_true = np.asarray(y_true, dtype=np.float64)
    probability = np.clip(
        np.asarray(y_probability, dtype=np.float64),
        1e-12,
        1.0 - 1e-12,
    )
    return float(
        -np.mean(
            y_true * np.log(probability)
            + (1.0 - y_true) * np.log(1.0 - probability)
        )
    )


def sigmoid(logits: np.ndarray) -> np.ndarray:
    """Compute a numerically stable sigmoid."""
    exponent = np.exp(-np.abs(logits))
    return np.where(
        logits >= 0.0,
        1.0 / (1.0 + exponent),
        exponent / (1.0 + exponent),
    )


def bce_logits_gradient(
    y_true: np.ndarray, logits: np.ndarray
) -> np.ndarray:
    """Return the cross-entropy gradient with respect to logits."""
    probability = sigmoid(logits)
    y_true = np.asarray(y_true, dtype=np.float64)
    return (probability - y_true) / y_true.size


rng = np.random.default_rng(42)


def split_80_20(
    x: np.ndarray, y: np.ndarray
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
    """Deterministically split arrays into training and test subsets."""
    order = rng.permutation(y.size)
    cut = int(0.8 * y.size)
    return x[order[:cut]], y[order[:cut]], x[order[cut:]], y[order[cut:]]


# Synthetic regression data.
x_reg = rng.normal(size=200)
y_reg = 3.0 + 2.0 * x_reg + rng.normal(scale=0.3, size=200)
x_train, y_train, x_test, y_test = split_80_20(x_reg, y_reg)

weight, bias = 0.0, 0.0
learning_rate = 0.05

print("MSE gradient descent:")
for _ in range(5):
    predictions = weight * x_train + bias
    cost = mean_squared_error(y_train, predictions)
    prediction_gradient = mse_gradient(y_train, predictions)

    weight -= learning_rate * np.mean(prediction_gradient * x_train)
    bias -= learning_rate * np.mean(prediction_gradient)
    print(f"cost={cost:.6f}")

test_predictions = weight * x_test + bias
print(
    f"held-out MSE="
    f"{mean_squared_error(y_test, test_predictions):.6f}"
)

# Synthetic binary classification data.
x_clf = rng.normal(size=200)
y_clf = (x_clf + rng.normal(scale=0.5, size=200) > 0.0).astype(
    np.float64
)
x_train, y_train, x_test, y_test = split_80_20(x_clf, y_clf)

clf_weight = 0.0
clf_learning_rate = 0.1

print("\nBinary cross-entropy gradient descent:")
for _ in range(10):
    logits = x_train * clf_weight
    cost = binary_cross_entropy(y_train, sigmoid(logits))
    logit_gradient = bce_logits_gradient(y_train, logits)

    clf_weight -= clf_learning_rate * np.mean(
        logit_gradient * x_train
    )
    print(f"cost={cost:.6f}")

test_cost = binary_cross_entropy(y_test, sigmoid(x_test * clf_weight))
print(f"held-out cross-entropy={test_cost:.6f}")

Practical Considerations

  • For n scalar targets, computing MSE and its gradient takes O(n) time and O(n) temporary memory; with d outputs, both become O(nd).

  • The training objective may differ from the reported metric: classification models can train with cross-entropy but report F1, ROC-AUC, or PR-AUC.

  • Accuracy can mislead on imbalanced classes because the majority class can dominate the result.

  • Split data before fitting preprocessing, feature selection, or imputation to prevent leakage. Impute missing values using training data only.

  • Use held-out or cross-validation results to detect overfitting. Use StratifiedKFold for classification and TimeSeriesSplit for temporal data.

  • Scale features for gradient descent, k-NN, SVM, and PCA; scaling is generally unnecessary for tree-based models.

Common Pitfalls

  • Dividing by n and also summing over output features can produce an unintended scale.

  • Forgetting that MSE weights squared errors more heavily than MAE.

  • Computing log(0) in cross-entropy; clip probabilities or use a logits-based implementation such as torch.nn.BCEWithLogitsLoss.

  • Treating a decreasing training loss as evidence of generalization.

  • Using the wrong metric: use MAE, RMSE, or R^2 for regression and precision, recall, F1, ROC-AUC, or PR-AUC for classification.

27

How do you compute cross-entropy loss in Python?

Cross-entropy loss measures the difference between predicted probabilities and true labels by penalizing incorrect, especially confident, predictions. It provides a differentiable objective for training probabilistic classifiers such as logistic regression. Its main trade-off is strong sensitivity to mislabeled examples and numerical instability near probabilities of zero or one.

Read full answer

Applies toNumPy 2.x and scikit-learn 1.5+ (Python 3.12); PyTorch 2.x and TensorFlow 2.16+ with Keras 3 for alternative losses

How Cross-Entropy Works

  • Loss vs. cost: A loss measures the error for one example. The cost or objective averages those losses over the dataset and may add a regularization term. Gradient-based training requires an objective that is differentiable with respect to the model parameters.

  • Binary cross-entropy (BCE or log loss):
    BCE = -(1/n) * sum_i [y_i * log(p_i) + (1 - y_i) * log(1 - p_i)]

  • n is the number of examples and i indexes them.

  • y_i is the true binary label, either 0 or 1.

  • p_i is the predicted probability of class 1.

  • log is the natural logarithm.

  • Categorical cross-entropy:
    CCE = -(1/n) * sum_i sum_(c=1)^C [y_(i,c) * log(p_(i,c))] Here, C is the number of classes, y_(i,c) indicates whether class c is the true class for example i, and p_(i,c) is its predicted probability.

Related Costs and Trade-offs

Loss Formula When to use and trade-off
Mean squared error (MSE) (1/n) * sum_i (y_i - y_hat_i)^2 Regression; penalizes large errors strongly but is sensitive to outliers.
Mean absolute error (MAE) (1/n) * sum_i abs(y_i - y_hat_i) Regression when robustness to outliers matters; its derivative is less smooth at zero.
Huber loss e^2 / 2 when abs(e) <= delta; otherwise delta * (abs(e) - delta / 2) Regression combining MSE near zero error with MAE behavior for large errors; e is the residual and delta is the transition threshold.
Categorical cross-entropy -sum_c y_c * log(p_c) Multiclass classification with mutually exclusive classes, usually with softmax probabilities.
Hinge loss (1/n) * sum_i max(0, 1 - y_i * f(x_i)) Margin-based classifiers such as SVMs; y_i is -1 or 1, and f(x_i) is the signed score.

| KL divergence | D_KL(P || Q) = sum_c p_c * log(p_c / q_c) | Comparing distributions in objectives such as distillation; p_c and q_c are probability masses and the divergence is asymmetric. |

Here, y_hat_i is a numeric prediction. Logistic regression normally uses cross-entropy rather than MSE because its cross-entropy objective is convex in the weights and gives stronger gradients for confidently wrong predictions; applying MSE after a sigmoid produces a generally non-convex objective.

Regularized objectives add lambda * sum_j w_j^2 for L2 regularization or lambda * sum_j abs(w_j) for L1 regularization. Here, lambda >= 0 controls penalty strength and w_j is model weight j; L1 commonly produces sparse weights.

Code Example

import numpy as np
from sklearn.model_selection import train_test_split


def binary_cross_entropy(
    y_true: np.ndarray,
    y_prob: np.ndarray,
    eps: float = 1e-12,
) -> float:
    """Return average BCE for binary labels and probabilities."""
    # Clipping prevents log(0) and log(1 - 0).
    p = np.clip(y_prob, eps, 1.0 - eps)
    losses = -(y_true * np.log(p) + (1.0 - y_true) * np.log(1.0 - p))
    return float(np.mean(losses))


def bce_gradient(y_true: np.ndarray, y_prob: np.ndarray) -> np.ndarray:
    """Return d(mean BCE)/d(probability); probabilities must be in (0, 1)."""
    if np.any((y_prob <= 0.0) | (y_prob >= 1.0)):
        raise ValueError("BCE probability gradients require values strictly between 0 and 1.")
    return (-y_true / y_prob + (1.0 - y_true) / (1.0 - y_prob)) / y_true.shape[0]


def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """Return average MSE."""
    return float(np.mean((y_pred - y_true) ** 2))


def mse_gradient(y_true: np.ndarray, y_pred: np.ndarray) -> np.ndarray:
    """Return d(mean MSE)/d(prediction)."""
    return 2.0 * (y_pred - y_true) / y_true.shape[0]


def sigmoid(z: np.ndarray) -> np.ndarray:
    return 1.0 / (1.0 + np.exp(-z))


rng = np.random.default_rng(42)
n_samples, n_features = 200, 3
X = rng.normal(size=(n_samples, n_features))
true_w = np.array([1.5, -1.0, 0.5])
y = (sigmoid(X @ true_w) > 0.5).astype(float)

# Split before fitting the model.
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

w = np.zeros(n_features)
learning_rate = 0.5

for step in range(1, 6):
    probabilities = sigmoid(X_train @ w)
    cost = binary_cross_entropy(y_train, probabilities)
    print(f"step {step}: cost={cost:.6f}")

    # Apply the chain rule from probabilities to weights.
    grad_probabilities = bce_gradient(y_train, probabilities)
    grad_w = X_train.T @ (
        grad_probabilities * probabilities * (1.0 - probabilities)
    )
    w -= learning_rate * grad_w

test_probabilities = sigmoid(X_test @ w)
print(f"test BCE: {binary_cross_entropy(y_test, test_probabilities):.6f}")

Practical Considerations

  • Numerical stability: Prefer logits-based losses during training, such as torch.nn.BCEWithLogitsLoss or tf.keras.losses.BinaryCrossentropy(from_logits=True). They combine the activation and loss more safely than manually computing logarithms.

  • Training vs. evaluation: BCE can be the training objective while F1, ROC-AUC, or PR-AUC is reported. For imbalanced data, accuracy alone can be misleading.

  • Regularization: Add L1 or L2 penalties when overfitting is a concern and validate the penalty strength without repeatedly tuning it on the test set.

Common Pitfalls

  • Passing hard labels where probabilities are expected, using probabilities outside [0, 1], or giving a one-hot matrix to binary BCE.

  • Computing log(0), using an unstable sigmoid implementation, or treating the unclipped BCE derivative as valid at clipped boundaries.

  • Fitting preprocessing before splitting the data and causing leakage; fit preprocessing within a Pipeline or ColumnTransformer on training data only.

  • Using StratifiedKFold for classification evaluation and checking that the reported metric matches the business objective.

  • Interview follow-ups include deriving the sigmoid-plus-BCE gradient, comparing cross-entropy with MSE, explaining multiclass softmax cross-entropy, and discussing probability calibration.

28

How can gradient descent minimize a quadratic cost function in Python?

Gradient descent minimizes a cost function by repeatedly updating parameters opposite the gradient. For a quadratic objective such as mean squared error, the update is simple, but the learning rate must balance fast convergence against overshooting. Gradient-based training generally requires a differentiable objective; nonsmooth objectives require subgradients or smooth approximations.

Read full answer

Applies toNumPy 2.x and scikit-learn 1.5+ (Python 3.12)

Core Idea

For a single parameter and the quadratic cost J(w) = 0.5 * (w - 3)^2:

  • J(w) is the cost, w is the parameter, and the optimum is w = 3.

  • dJ/dw = w - 3 is the gradient.

  • w = w - learning_rate * gradient moves w toward the minimum.

In general, a loss measures the error for one example, while a cost or objective aggregates losses over the dataset and may include regularization:

J(w) = (1/n) * sum(loss_i) + regularization(w)

Here, n is the number of training examples, i identifies an example, and w contains the model parameters. Differentiability allows gradient descent to calculate the direction of steepest descent.

Common Cost Functions

Cost Formula Typical use
Mean squared error (MSE) (1/n) * sum((y_i - y_hat_i)^2) Regression; strongly penalizes large errors
Mean absolute error (MAE) (1/n) * sum(abs(y_i - y_hat_i)) Regression with outliers; less sensitive to them
Huber loss (1/n) * sum(L_delta(e_i)) Regression combining quadratic and absolute-error behavior
Binary cross-entropy -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary or multilabel classification
Categorical cross-entropy -(1/n) * sum(sum_k(y_ik*log(p_ik))) Multiclass classification with softmax
Hinge loss (1/n) * sum(max(0, 1 - y_i*f(x_i))) Linear SVMs and margin maximization
KL divergence sum_x(p(x)*log(p(x)/q(x))) Comparing probability distributions

y_i is the target, y_hat_i is the predicted value, p_i is a predicted probability, and k identifies a class. For hinge loss, x_i is the input feature vector, f(x_i) is a signed decision score, and y_i is normally -1 or +1. An error e_i is normally y_i - y_hat_i; delta is the positive Huber threshold where the loss changes from quadratic to linear. In KL divergence, x indexes possible outcomes, while p(x) and q(x) are probability distributions.

Logistic regression uses binary cross-entropy because the objective is convex in the weights and matches probabilistic classification. Using MSE on probabilities can produce a non-convex objective and saturating gradients.

Regularized Objectives

  • L2 regularization: J_L2 = loss + lambda * sum(w_j^2). It shrinks coefficients continuously and is the penalty used by ridge regression and weight decay.

  • L1 regularization: J_L1 = loss + lambda * sum(abs(w_j)). It can set coefficients exactly to zero, producing sparse models.

Here, lambda controls regularization strength and j indexes model coefficients. Training loss need not match the reporting metric: a classifier can optimize cross-entropy while being evaluated with F1, precision, recall, ROC-AUC, or PR-AUC.

Code Example

import numpy as np
from sklearn.model_selection import train_test_split


def mse(y, y_hat):
    return float(np.mean((y - y_hat) ** 2))


def mse_gradient(y, y_hat):
    """Gradient of MSE with respect to each prediction."""
    return (2.0 / len(y)) * (y_hat - y)


def binary_cross_entropy(y, probabilities):
    """Clipped binary cross-entropy loss."""
    epsilon = 1e-12
    p = np.clip(probabilities, epsilon, 1.0 - epsilon)
    return float(
        np.mean(-(y * np.log(p) + (1.0 - y) * np.log(1.0 - p)))
    )


def binary_cross_entropy_gradient(y, probabilities):
    """Gradient of the clipped BCE objective with respect to probabilities."""
    epsilon = 1e-12
    raw = np.asarray(probabilities, dtype=float)
    p = np.clip(raw, epsilon, 1.0 - epsilon)
    interior = (raw > epsilon) & (raw < 1.0 - epsilon)

    # Clipped values are constant under the objective, so their gradient is 0.
    gradient = np.zeros_like(raw)
    gradient[interior] = (
        (p[interior] - y[interior])
        / (p[interior] * (1.0 - p[interior]) * len(y))
    )
    return gradient


def sigmoid(values):
    values = np.asarray(values, dtype=float)
    result = np.empty_like(values)
    nonnegative = values >= 0.0

    result[nonnegative] = 1.0 / (
        1.0 + np.exp(-values[nonnegative])
    )
    exp_values = np.exp(values[~nonnegative])
    result[~nonnegative] = exp_values / (1.0 + exp_values)
    return result


rng = np.random.default_rng(42)

# Minimize quadratic MSE for linear regression.
x = rng.normal(size=200)
y = 2.5 * x + 1.0 + rng.normal(scale=0.5, size=200)
x_train, _, y_train, _ = train_test_split(
    x, y, test_size=0.2, random_state=42
)

weight, bias = 0.0, 0.0
learning_rate = 0.02
mse_costs = []

for _ in range(150):
    predictions = weight * x_train + bias
    mse_costs.append(mse(y_train, predictions))

    prediction_gradient = mse_gradient(y_train, predictions)
    weight -= learning_rate * np.dot(prediction_gradient, x_train)
    bias -= learning_rate * np.mean(predictions - y_train)

print("MSE cost:", round(mse_costs[0], 4), "->", round(mse_costs[-1], 4))

# Minimize binary cross-entropy for logistic regression.
features = rng.normal(size=(200, 2))
true_logits = 0.8 * features[:, 0] - 0.5 * features[:, 1]
labels = (rng.random(200) < sigmoid(true_logits)).astype(float)
X_train, _, y_train, _ = train_test_split(
    features, labels, test_size=0.2, random_state=42, stratify=labels
)

weights = np.zeros(X_train.shape[1])
bias = 0.0
bce_costs = []

for _ in range(150):
    logits = X_train @ weights + bias
    probabilities = sigmoid(logits)
    bce_costs.append(binary_cross_entropy(y_train, probabilities))

    gradient_p = binary_cross_entropy_gradient(y_train, probabilities)
    gradient_logits = gradient_p * probabilities * (1.0 - probabilities)

    weights -= 0.1 * (X_train.T @ gradient_logits)
    bias -= 0.1 * np.sum(gradient_logits)

print("BCE cost:", round(bce_costs[0], 4), "->", round(bce_costs[-1], 4))

Common Pitfalls

  • Learning rate: Too large can cause divergence; too small makes convergence slow.

  • Numerical stability: Prefer logits-based implementations such as PyTorch’s BCEWithLogitsLoss in production.

  • Scaling: Standardize features when gradient descent is sensitive to their scales.

  • Data leakage: Split data before training and fit preprocessing only on training data.

  • Missing values and imbalance: Impute missing values and use suitable class weights; accuracy can mislead on imbalanced data.

  • Validation: Use cross-validation for model selection instead of repeatedly tuning against one validation split.

  • Reproducibility: Set random seeds and confirm convergence using held-out metrics.

29

How do batch and stochastic gradient descent convergence compare in Python?

Gradient descent optimizes a cost function—the average loss over training data plus any regularization—by iteratively moving parameters in the direction that reduces that cost. It solves the problem of finding model weights that best fit data when a closed-form solution is impractical. The main trade-off is that batch gradient descent (BGD) computes stable, smooth updates using all samples but is slow per step, while stochastic gradient descent (SGD) updates after each sample for faster, noisier progress that can escape shallow local minima but may oscillate near the optimum.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12 with NumPy 2.x)

Cost Function and Gradient Descent Basics

  • Loss vs. cost: A loss measures error for one example; the cost (objective) is the average loss over the dataset, often plus a regularization term. Gradient-based training requires the cost to be differentiable so gradients can be computed.

  • Mean Squared Error (MSE): MSE = (1/n) * sum((y_i - y_hat_i)^2), where n is the number of examples, y_i is the true target for example i, y_hat_i is the predicted target, and sum(...) adds the squared errors over all examples. MSE penalizes large errors more; MAE ((1/n) * sum(|y_i - y_hat_i|)) is more robust to outliers.

  • Binary cross-entropy (log loss): BCE = -(1/n) * sum(y_i * log(p_i) + (1 - y_i) * log(1 - p_i)), where p_i is the predicted probability for class 1. Logistic regression uses cross-entropy instead of MSE because it is convex in the weights and provides better gradients. Regularized costs add lambda * sum(w^2) (L2/ridge) or lambda * sum(|w|) (L1/lasso, sparse weights).

Method Update uses Convergence behavior Per-epoch cost When to use
Batch GD All n samples Smooth, deterministic path O(n * d) Small/medium data, stable convergence
Stochastic GD 1 sample at a time Noisy, can fluctuate O(n * d) total, faster early progress Large data, online/streaming settings

d is the number of features. The training loss and the evaluation metric can differ (for example, train on log loss, report F1).

Code Example

import numpy as np

rng = np.random.default_rng(42)

# --- MSE: compare BGD vs SGD convergence on synthetic regression data ---
n_samples, n_features = 200, 1
X = rng.standard_normal((n_samples, n_features))
X_b = np.c_[np.ones(n_samples), X]  # add bias column
true_w = np.array([2.0, -3.0])
y = X_b @ true_w + rng.normal(0, 0.5, n_samples)

def mse_cost(X_b, y, w):
    errors = X_b @ w - y
    return np.mean(errors ** 2)  # (1/n) * sum((y - y_hat)^2)

def mse_gradient(X_b, y, w):
    n = X_b.shape[0]
    return (2.0 / n) * X_b.T @ (X_b @ w - y)

# Batch GD: one gradient from all samples per epoch
w_bgd = rng.standard_normal(X_b.shape[1]) * 0.1
for epoch in range(50):
    w_bgd -= 0.1 * mse_gradient(X_b, y, w_bgd)
    if epoch in (0, 49):
        print(f"BGD epoch {epoch:>2} MSE: {mse_cost(X_b, y, w_bgd):.6f}")

# SGD: one update per sample, shuffled each epoch
w_sgd = rng.standard_normal(X_b.shape[1]) * 0.1
for epoch in range(50):
    for i in rng.permutation(n_samples):
        xi, yi = X_b[i:i + 1], y[i:i + 1]
        grad = 2.0 * xi.T @ (xi @ w_sgd - yi)  # per-sample gradient
        w_sgd -= 0.01 * grad
    if epoch in (0, 49):
        print(f"SGD epoch {epoch:>2} MSE: {mse_cost(X_b, y, w_sgd):.6f}")

# --- Binary cross-entropy: clip predictions to avoid log(0), run BGD steps ---
def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

y_cls = (y > np.median(y)).astype(float)  # synthetic binary labels

def bce_cost(X_b, y, w):
    p = np.clip(sigmoid(X_b @ w), 1e-15, 1 - 1e-15)  # avoid log(0)
    return -np.mean(y * np.log(p) + (1 - y) * np.log(1 - p))

def bce_gradient(X_b, y, w):
    p = sigmoid(X_b @ w)
    return (1.0 / X_b.shape[0]) * X_b.T @ (p - y)

w_bce = rng.standard_normal(X_b.shape[1]) * 0.1
for step in range(6):
    print(f"BCE step {step} cost: {bce_cost(X_b, y_cls, w_bce):.6f}")
    w_bce -= 0.5 * bce_gradient(X_b, y_cls, w_bce)

Common Pitfalls

  • Learning rate mismatch: BGD and SGD often need different learning rates; too large diverges, too small converges slowly. Use learning-rate schedules or adaptive methods in practice.

  • No shuffling in SGD: Iterating samples in a fixed order can bias updates; shuffle each epoch.

  • Numerical instability: For binary cross-entropy, clip predictions with np.clip(p, 1e-15, 1 - 1e-15) to avoid log(0), or use logits-based losses like torch.nn.BCEWithLogitsLoss.

  • Wrong comparison: Compare full-dataset cost per epoch (as above), not raw update counts, and fix random seeds for reproducibility.

  • Interview tips: Expect follow-ups on mini-batch GD, why SGD noise can help generalization, convexity/convergence guarantees, and how feature scaling affects gradient descent.

30

How do you choose a cost function for stock prediction models?

A cost function measures the prediction error that a model optimizes, so it should reflect the trading objective, target distribution, and consequences of large mistakes. For stock prediction, the main trade-off is between objectives that strongly penalize extreme errors and robust objectives that tolerate outliers. Forecasting log returns or normalized changes is often preferable to forecasting raw, non-stationary price levels.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12 with NumPy 2.x)

Loss Versus Objective

A loss is the error or penalty for one example. The cost or objective averages losses over the training data and may add regularization:

J(w) = (1/n) * sum_i l(y_i, y_hat_i) + R(w)

Here, n is the number of examples, i is an example index, l is the per-example loss, y_i is the observed value, y_hat_i is the prediction, w is the parameter vector, and R(w) is the regularization penalty.

Gradient-based training needs a differentiable objective because optimizers use derivatives to update parameters. MAE is non-differentiable at zero but can still be optimized with subgradients or specialized solvers; Huber loss remains smooth.

Main Cost Functions

Let e_i = y_i - y_hat_i, p be a predicted probability, q a target probability, f(x) a classifier score, K the number of classes, k a class index, delta the Huber transition point, and lambda the regularization strength.

Cost Formula Appropriate use Trade-off
MSE (1/n) * sum_i(e_i^2) Regression where large errors should dominate Smooth but outlier-sensitive
MAE (1/n) * sum_i(abs(e_i)) Regression where typical absolute errors matter Robust but non-smooth at zero
Huber (1/n) * sum_i h(e_i), where h(e) = e^2 if abs(e) <= delta, otherwise delta * (abs(e) - 0.5*delta) Regression needing both smoothness and robustness delta determines the transition to linear loss
Binary cross-entropy, or log loss -(1/n) * sum_i(y_i * log(p_i) + (1-y_i) * log(1-p_i)) Binary direction or probability forecasts Strongly penalizes confident mistakes
Categorical cross-entropy -(1/n) * sum_i sum_k(q_ik * log(p_ik)) Multiclass outcomes with softmax probabilities Sensitive to incorrect confident probabilities
Hinge loss mean(max(0, 1 - y * f(x))), with y equal to -1 or 1 Margin classifiers such as SVMs Ignores correctly classified points beyond the margin
KL divergence sum_x(p(x) * log(p(x) / q(x))) Comparing distributions at outcomes indexed by x Measures distribution mismatch rather than point error

Regularized objectives use:

  • L2 penalty: lambda * sum_j(w_j^2), where w_j is one weight. It underlies ridge regression and weight decay and discourages large weights.

  • L1 penalty: lambda * sum_j(abs(w_j)). It underlies lasso and can set weights exactly to zero, producing sparse models.

Choosing for Stock Prediction

  1. Define the target: Choose next-period returns, volatility, or market direction rather than raw price levels when the business objective permits it.

  2. Match the error consequence: Use MAE for robust absolute errors, MSE when extreme moves should dominate, and Huber to combine both behaviors.

  3. Represent asymmetric costs: If bullish and bearish mistakes have different business costs, use direction-dependent weights or a quantile loss for a required return percentile.

  4. Treat probabilities correctly: For linear logistic regression, binary cross-entropy produces a convex objective in the weights, whereas MSE on sigmoid outputs can be non-convex. BCE also has the logit gradient p - y, avoiding the extra sigmoid-derivative factor in MSE that can vanish during saturation.

  5. Regularize appropriately: L1 or L2 can control overfitting in high-dimensional financial features; both preserve convexity for linear logistic regression.

  6. Separate loss from evaluation: Train with the business-aligned loss but report MAE, RMSE, directional metrics, or turnover-adjusted returns. For imbalanced direction labels, precision, recall, F1, ROC-AUC, and PR-AUC are more informative than accuracy alone.

Use chronological or walk-forward validation rather than random splits. Fit scalers, feature selectors, and encoders on each training window only to prevent temporal leakage.

Code Example

import numpy as np

rng = np.random.default_rng(42)

n_samples, n_features = 600, 5
X = rng.normal(size=(n_samples, n_features))

true_weights = np.array([0.8, -0.5, 0.3, 0.0, -0.2])
returns = X @ true_weights + rng.normal(scale=0.2, size=n_samples)

direction_logits = (
    X @ rng.normal(scale=0.5, size=n_features)
    + rng.normal(scale=0.5, size=n_samples)
)
directions = (direction_logits > 0).astype(float)

split = int(0.7 * n_samples)
X_train, X_test = X[:split], X[split:]
return_train, return_test = returns[:split], returns[split:]
direction_train = directions[:split]
direction_test = directions[split:]


def mse_loss_and_gradients(X, y, weights, intercept):
    predictions = X @ weights + intercept
    errors = y - predictions
    loss = np.mean(errors ** 2)
    grad_weights = (2.0 / len(y)) * (X.T @ errors)
    grad_intercept = 2.0 * np.mean(errors)
    return loss, grad_weights, grad_intercept


def binary_cross_entropy_and_gradients(X, y, weights, intercept):
    logits = X @ weights + intercept

    # Numerically stable sigmoid calculation.
    probabilities = np.exp(-np.logaddexp(0.0, -logits))

    # Prevent log(0) and log(1) in the loss calculation.
    clipped_probabilities = np.clip(
        probabilities, 1e-12, 1.0 - 1e-12
    )
    loss = -np.mean(
        y * np.log(clipped_probabilities)
        + (1.0 - y) * np.log(1.0 - clipped_probabilities)
    )

    # For binary targets, the BCE gradient with respect to logits is p - y.
    grad_weights = (X.T @ (probabilities - y)) / len(y)
    grad_intercept = np.mean(probabilities - y)
    return loss, grad_weights, grad_intercept


def gradient_descent(loss_and_gradients, X, y, learning_rate, epochs):
    weights = np.zeros(X.shape[1])
    intercept = 0.0
    initial_loss, _, _ = loss_and_gradients(X, y, weights, intercept)

    for _ in range(epochs):
        _, grad_weights, grad_intercept = loss_and_gradients(
            X, y, weights, intercept
        )
        weights -= learning_rate * grad_weights
        intercept -= learning_rate * grad_intercept

    final_loss, _, _ = loss_and_gradients(X, y, weights, intercept)
    return initial_loss, final_loss


mse_initial, mse_final = gradient_descent(
    mse_loss_and_gradients, X_train, return_train, 0.05, 300
)
bce_initial, bce_final = gradient_descent(
    binary_cross_entropy_and_gradients,
    X_train,
    direction_train,
    0.1,
    300,
)

mse_test, _, _ = mse_loss_and_gradients(
    X_test, return_test, np.zeros(n_features), 0.0
)
bce_test, _, _ = binary_cross_entropy_and_gradients(
    X_test, direction_test, np.zeros(n_features), 0.0
)

print(f"MSE training cost: {mse_initial:.4f} -> {mse_final:.4f}")
print(f"BCE training cost: {bce_initial:.4f} -> {bce_final:.4f}")
print("Both training costs decreased:",
      mse_final < mse_initial and bce_final < bce_initial)
print(f"Held-out MSE cost: {mse_test:.4f}")
print(f"Held-out BCE cost: {bce_test:.4f}")

Common Pitfalls

  • Optimizing raw prices: Price-level scale can dominate MSE; prefer returns or normalized prices when appropriate.

  • Temporal leakage: Random splits can expose future market information to training.

  • Ignoring trading frictions: Low loss does not guarantee profitability after costs, slippage, and turnover.

  • Using accuracy alone: Imbalanced direction labels require precision, recall, F1, and PR-AUC.

  • Numerical instability: Use logits-based BCE implementations such as torch.nn.BCEWithLogitsLoss rather than taking log of an unprotected probability.

31

How do you choose a cost function for imbalanced classification?

A cost function converts prediction errors into an objective that a model minimizes during training. For imbalanced classification, the main trade-off is penalizing minority-class errors without making the optimization unstable or excessively sensitive to noisy minority labels. Weighted cross-entropy is a strong starting point, but the selected model should also be evaluated with imbalance-aware metrics.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12 with NumPy 2.x and scikit-learn 1.5+)

Core Concept

A loss measures the error for one observation. The overall cost or objective averages those losses and may add regularization:

J(w) = (1/n) * sum(loss_i(w)) + lambda * R(w)

Here, w is the parameter vector, n is the number of training examples, lambda controls regularization strength, and R(w) is the regularization penalty. Gradient-based training requires a differentiable objective so it can compute parameter-update directions. MAE is not differentiable at zero, but optimizers can use subgradients or smooth approximations.

Common Cost Functions

Cost Formula Use and trade-off
Mean squared error MSE = (1/n) * sum((y - y_hat)^2) Regression; heavily penalizes large errors and is sensitive to outliers.
Mean absolute error MAE = (1/n) * sum(abs(y - y_hat)) Regression; more robust to outliers but less smooth around zero.
Huber loss 0.5*(y-y_hat)^2 if abs(y-y_hat) <= delta; otherwise delta*(abs(y-y_hat)-0.5*delta) Regression; combines squared-error smoothness with some outlier resistance.
Binary cross-entropy BCE = -(1/n) * sum(y*log(p) + (1-y)*log(1-p)) Binary classification; strongly penalizes confident mistakes and supports probabilistic outputs.
Categorical cross-entropy CCE = -(1/n) * sum_i sum_c(y_ic * log(p_ic)) Multiclass classification with softmax outputs.
Hinge loss (1/n) * sum(max(0, 1-y_i*f(x_i))) Binary SVM classification; encourages a margin of at least one.

| KL divergence | D_KL(P||Q) = sum(p_c * log(p_c/q_c)) | Measures distribution mismatch; not normally the primary ordinary-classification loss. |

Here, y is the target, y_hat is a predicted value, p is a predicted probability, y_ic is the one-hot target for example i and class c, p_ic is its predicted probability, f(x_i) is a decision score, and delta is the Huber transition threshold. For hinge loss, y_i is either -1 or +1.

Choosing for Imbalanced Classification

  1. Use cross-entropy for probabilistic classification. For binary classification, use weighted BCE; for multiclass classification, use class-weighted categorical cross-entropy. A weighted BCE objective is:

-(1/sum(v_i)) * sum(v_i * [y_i*log(p_i) + (1-y_i)*log(1-p_i)])

Here, v_i is the weight for example i.

  1. Calculate class weights from training data only. Giving each class equal total influence is common, so minority examples receive larger per-example weights. Do not calculate these weights using validation or test labels.

  2. Separate training loss from model selection. A model can minimize weighted log loss while being selected by F1, PR-AUC, recall at a fixed precision, or explicit false-positive and false-negative costs.

  3. Tune the decision threshold on validation data. Cross-entropy evaluates probability quality, whereas precision and recall depend on the threshold.

  4. Consider alternatives carefully. Focal loss can downweight easy examples in extreme imbalance. Custom objectives are appropriate when false positives and false negatives have different business costs.

Logistic regression generally uses cross-entropy because its negative log-likelihood is convex in the weights and its logit gradient is p - y. Squared error combined with a sigmoid is generally non-convex and can produce poor gradients for confidently incorrect predictions.

Regularization

  • L2 regularization: lambda * sum(w_j^2) continuously shrinks weights. In linear regression it is called ridge regression; in neural-network training it is commonly called weight decay.

  • L1 regularization: lambda * sum(abs(w_j)) can set some weights exactly to zero, producing sparse models.

  • Include regularization in the training objective, standardize features where appropriate, and keep validation data separate.

Code Example

import numpy as np
from sklearn.metrics import (
    average_precision_score,
    f1_score,
    precision_score,
    recall_score,
    roc_auc_score,
)
from sklearn.model_selection import train_test_split

rng = np.random.default_rng(42)

X = rng.normal(size=(1200, 2))
probabilities = 1.0 / (1.0 + np.exp(-(1.2 * X[:, 0] - 0.8 * X[:, 1] + 0.2)))
y = rng.binomial(1, probabilities)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y
)
X_train = np.column_stack([np.ones(len(X_train)), X_train])
X_test = np.column_stack([np.ones(len(X_test)), X_test])


def sigmoid(values):
    return np.exp(-np.logaddexp(0.0, -values))


def mse_objective(X, y, predictions, sample_weight=None):
    errors = predictions - y
    weights = (
        np.ones_like(y)
        if sample_weight is None
        else np.asarray(sample_weight)
    )
    loss = np.dot(weights, errors**2) / weights.sum()
    gradient = 2.0 * (X.T @ (weights * errors)) / weights.sum()
    return loss, gradient


def bce_objective(X, y, predictions, sample_weight=None):
    p = np.clip(predictions, 1e-12, 1.0 - 1e-12)
    per_example = -(y * np.log(p) + (1 - y) * np.log1p(-p))
    weights = (
        np.ones_like(y)
        if sample_weight is None
        else np.asarray(sample_weight)
    )
    loss = np.dot(weights, per_example) / weights.sum()
    gradient = X.T @ (weights * (p - y)) / weights.sum()
    return loss, gradient


def train(objective, sample_weight=None, learning_rate=0.5):
    weights = np.zeros(X_train.shape[1])
    history = []

    for _ in range(250):
        predictions = sigmoid(X_train @ weights)
        loss, gradient = objective(
            X_train, y_train, predictions, sample_weight
        )
        weights -= learning_rate * gradient
        history.append(loss)

    return weights, history


n_positive = np.sum(y_train == 1)
n_negative = np.sum(y_train == 0)
class_weights = np.where(
    y_train == 1,
    len(y_train) / (2.0 * n_positive),
    len(y_train) / (2.0 * n_negative),
)

_, mse_cost = train(mse_objective, learning_rate=0.3)
_, bce_cost = train(bce_objective)
_, weighted_cost = train(bce_objective, class_weights)

print("MSE cost:", mse_cost[0], "->", mse_cost[-1])
print("BCE cost:", bce_cost[0], "->", bce_cost[-1])
print("Weighted BCE cost:", weighted_cost[0], "->", weighted_cost[-1])

for name, model in [
    ("Unweighted BCE", train(bce_objective)[0]),
    ("Weighted BCE", train(bce_objective, class_weights)[0]),
]:
    predictions = sigmoid(X_test @ model)
    classes = predictions >= 0.5
    print(
        name,
        f"precision={precision_score(y_test, classes, zero_division=0):.3f}",
        f"recall={recall_score(y_test, classes, zero_division=0):.3f}",
        f"F1={f1_score(y_test, classes, zero_division=0):.3f}",
        f"ROC-AUC={roc_auc_score(y_test, predictions):.3f}",
        f"PR-AUC={average_precision_score(y_test, predictions):.3f}",
    )

Common Pitfalls

  • Using accuracy: it can look excellent when the majority class dominates. Report class-specific precision and recall, F1, ROC-AUC, and PR-AUC.

  • Leakage: calculate class weights, thresholds, scalers, and encoders using training data or training folds only.

  • Confusing loss and metric: training with log loss and reporting F1 is valid; it does not mean F1 was optimized directly.

  • Numerical instability: in PyTorch, prefer torch.nn.BCEWithLogitsLoss over manually taking the logarithm of sigmoid outputs.

  • Overfitting validation data: tune thresholds and hyperparameters with cross-validation or a separate validation set.

  • Ignoring data quality: missing values, label noise, duplicates, and unrepresentative sampling can outweigh the loss-function choice.

32

How can weighted binary cross-entropy reduce costly false negatives?

A weighted binary cross-entropy objective gives positive-class examples more weight, making false negatives more expensive during training and often improving recall at a fixed threshold. The main trade-off is potentially more false positives and lower precision, so both the weights and decision threshold should be validated.

Read full answer

Applies toNumPy 2.x, scikit-learn 1.5+, and PyTorch 2.x (Python 3.12)

How Weighting Works

A loss measures one example’s error; a cost function aggregates losses and may include regularization. Gradient-based training needs a differentiable objective, or one with usable subgradients.

For binary labels, define:

ell_i = -[y_i * log(p_i) + (1 - y_i) * log(1 - p_i)]

C_w = sum(w_i * ell_i) / sum(w_i)

Here, y_i is a true label in {0, 1}, p_i is its predicted positive probability, ell_i is its loss, and w_i is its weight. Set w_i = c_positive when y_i = 1 and c_negative when y_i = 0.

The weight is based on the true class, not on the prediction at runtime. It therefore raises the cost of every positive-class error, including false negatives. If a missed disease costs five times an unnecessary test, c_positive = 5 and c_negative = 1 represents that preference. The model generally assigns positives higher scores, but improved recall is not guaranteed.

Related Costs

For regression, n is the sample count, y is the target, y_hat is the prediction, and e = y - y_hat. For classification, p_k is a predicted class probability, y_k is a true one-hot indicator, and K is the class count; f(x) is a signed classifier score, and delta is a cutoff.

Cost Formula Use and trade-off
MSE (1/n) * sum((y - y_hat)^2) Regression; penalizes large errors strongly.
MAE (1/n) * sum(abs(y - y_hat)) Regression; robust to outliers, but non-differentiable at zero.
Huber 0.5*e^2 if abs(e) <= delta; otherwise delta*(abs(e) - delta/2) Smooth MSE/MAE compromise.
Binary cross-entropy -y*log(p) - (1-y)*log(1-p) Binary or multilabel classification.
Categorical cross-entropy -sum_k(y_k*log(p_k)) Multiclass softmax classification.
Hinge max(0, 1 - y*f(x)) Margin-based losses such as SVMs.

| KL divergence | D_KL(P || Q) = sum_k(P_k*log(P_k/Q_k)) | Asymmetric distribution-matching penalty. |

A regularized objective is J(w) = C(w) + lambda*R(w). L2 regularization uses R(w) = sum_j(w_j^2) and shrinks weights; L1 uses R(w) = sum_j(abs(w_j)) and can produce sparse weights. Here, lambda controls regularization strength and j indexes parameters.

With nonnegative weights, logistic-regression cross-entropy is convex in the weights. For logits z, its unweighted gradient is p - y; squared error applied to sigmoid outputs can produce saturated gradients and a non-convex objective.

Code Example

import numpy as np
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import (
    precision_score, recall_score, f1_score,
    roc_auc_score, average_precision_score,
)

rng = np.random.default_rng(42)
X = rng.normal(size=(1000, 2))
logits = X @ np.array([-1.2, 1.8]) - 1.7
probabilities = np.exp(-np.logaddexp(0.0, -logits))
y = (rng.random(len(X)) < probabilities).astype(float)


def sigmoid(values):
    return np.exp(-np.logaddexp(0.0, -values))


def mse(y_true, predictions):
    return float(np.mean((y_true - predictions) ** 2))


def mse_gradient(y_true, predictions):
    return (2.0 / len(y_true)) * (predictions - y_true)


def binary_cross_entropy(y_true, predicted_probabilities, sample_weight=None):
    p = np.clip(predicted_probabilities, 1e-12, 1.0 - 1e-12)
    losses = -(
        y_true * np.log(p)
        + (1.0 - y_true) * np.log1p(-p)
    )
    weights = (
        np.ones_like(y_true, dtype=float)
        if sample_weight is None else sample_weight
    )
    return float(np.average(losses, weights=weights))


def bce_logits_gradient(y_true, logits, sample_weight):
    p = sigmoid(logits)
    return sample_weight * (p - y_true) / sample_weight.sum()


def fit_weighted_bce(X_train, y_train, fn_weight, verbose=False):
    sample_weight = np.where(y_train == 1.0, fn_weight, 1.0)
    theta = np.zeros(X_train.shape[1])
    bias = 0.0

    for step in range(101):
        train_logits = X_train @ theta + bias
        train_probabilities = sigmoid(train_logits)

        if verbose and step % 50 == 0:
            cost = binary_cross_entropy(
                y_train, train_probabilities, sample_weight
            )
            mse_grad = mse_gradient(y_train, train_probabilities)
            print(
                f"Step {step:3d}: weighted BCE = {cost:.6f}, "
                f"MSE gradient norm = {np.linalg.norm(mse_grad):.6f}"
            )

        gradient = bce_logits_gradient(
            y_train, train_logits, sample_weight
        )
        theta -= 0.1 * (X_train.T @ gradient)
        bias -= 0.1 * gradient.sum()

    return theta, bias


splitter = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
metric_names = ["precision", "recall", "f1", "roc_auc", "pr_auc"]
results = {}

for fold, (train_index, validation_index) in enumerate(splitter.split(X, y)):
    X_train, X_validation = X[train_index], X[validation_index]
    y_train, y_validation = y[train_index], y[validation_index]

    for name, fn_weight in [("unweighted", 1.0), ("weighted", 5.0)]:
        theta, bias = fit_weighted_bce(
            X_train, y_train, fn_weight,
            verbose=(fold == 0 and name == "weighted"),
        )
        p = sigmoid(X_validation @ theta + bias)
        predictions = (p >= 0.5).astype(float)

        fold_metrics = [
            precision_score(y_validation, predictions, zero_division=0),
            recall_score(y_validation, predictions, zero_division=0),
            f1_score(y_validation, predictions, zero_division=0),
            roc_auc_score(y_validation, p),
            average_precision_score(y_validation, p),
        ]
        results.setdefault(name, []).append(fold_metrics)

for name, values in results.items():
    means = np.mean(values, axis=0).round(3)
    print(name, dict(zip(metric_names, means)))

Practical Guidance

  • Choose the weight ratio from relative business costs, then validate it with stratified or nested cross-validation.

  • For imbalanced data, report precision, recall, F1, ROC-AUC, and PR-AUC; accuracy can hide poor positive-class recall.

  • Training loss may differ from the evaluation metric: train with weighted log loss but report operational metrics such as recall or F1.

  • Fit scalers, encoders, and imputers inside each training fold, using an sklearn Pipeline when preprocessing is added.

Common Pitfalls

  • Class weighting and threshold changes both affect recall and precision; validate them jointly rather than assuming their effects combine linearly.

  • class_weight="balanced" addresses class frequency, not a specified business-cost ratio.

  • Prefer logits-based losses for numerical stability; PyTorch 2.x provides torch.nn.BCEWithLogitsLoss.

  • Choosing weights or thresholds on the test set causes leakage and optimistic evaluation.

33

When is Huber loss preferable to MSE in regression?

Huber loss is preferable when regression data contains occasional outliers or heavy-tailed errors that should not dominate training. It behaves quadratically for small residuals and linearly for large residuals, limiting the influence of corrupted observations compared with MSE. Its main trade-off is lower statistical efficiency than MSE when the noise is genuinely Gaussian and the data contain no problematic outliers.

Read full answer

Applies tomachine-learning concepts (all versions); PyTorch 2.x for the logits-based BCE reference

Loss Versus Cost

A loss measures the error for one example, while the cost function or objective aggregates those losses over the dataset and may add regularization:

J(w) = (1/n) * sum(loss(y_i, f(x_i, w))) + regularization

  • n: number of training examples.

  • i: index of an example.

  • w: model parameters.

  • y_i: true target for example i.

  • f(x_i, w): prediction for example i.

  • loss(...): error for that example.

Gradient-based training needs an objective with usable derivatives because optimization uses them to update the parameters. Huber loss is smooth and differentiable everywhere. MAE and hinge loss are nonsmooth at some points but can still be optimized with appropriate subgradient methods.

Why Huber Loss Fits Outlier-Prone Data

For prediction y_hat and true value y, let r = y_hat - y. With threshold delta, Huber loss is:

  • If |r| <= delta: L = 0.5 * r²

  • If |r| > delta: L = delta * (|r| - 0.5 * delta)

Here, r is the prediction error and delta is the residual magnitude at which quadratic behavior changes to linear behavior.

For example, suppose a model predicts house prices and a few prices contain data-entry errors of several hundred thousand dollars. MSE squares those errors, so their gradients can dominate training and pull the regression line toward the corrupted records. Huber loss limits that influence because its growth beyond delta is linear rather than quadratic.

Loss Large-error behavior Strength Limitation
MSE Quadratic Efficient under Gaussian noise Highly sensitive to outliers
MAE Linear Highly robust to outliers Nonsmooth at zero and less efficient under Gaussian noise
Huber loss Quadratic near zero, linear outside delta Combines smoothness with outlier resistance Requires choosing delta

Huber loss is therefore useful for contaminated targets, heavy-tailed noise, or situations in which a small fraction of records may be erroneous. If residuals are normally distributed and all observations are valid, MSE remains the natural choice because it is the maximum-likelihood loss for Gaussian noise.

Related Cost Functions

For n observations, other important losses and objectives include:

  • MSE: (1/n) * sum((y - y_hat)²). Penalizes large errors quadratically.

  • MAE: (1/n) * sum(|y - y_hat|). Penalizes errors linearly and is robust to outliers.

  • Huber loss: Uses quadratic loss near zero and linear growth outside delta.

  • Binary cross-entropy: -(1/n) * sum(y * log(p) + (1-y) * log(1-p)), where p is the predicted probability of the positive class.

  • Categorical cross-entropy: -(1/n) * sum_i sum_k (y_ik * log(p_ik)), where y_ik is the true indicator for class k and p_ik is its predicted probability.

  • Hinge loss: (1/n) * sum(max(0, 1 - y * f(x))), where f(x) is an SVM decision score and y is -1 or 1.

  • KL divergence: sum_x p(x) * log(p(x)/q(x)), where x indexes possible outcomes, p(x) is the target distribution, and q(x) is the approximating distribution.

Logistic regression uses cross-entropy because the objective is convex in the model weights and produces suitable gradients. MSE applied directly to sigmoid outputs is generally non-convex in the weights and can have poorly behaved optimization behavior.

Common regularized objectives include:

  • L2 regularization: lambda * sum(w_j²). This penalty is used in ridge regression and weight decay to discourage large weights.

  • L1 regularization: lambda * sum(|w_j|). This penalty is used in lasso regression and can produce sparse weights.

Here, lambda controls regularization strength, j indexes an individual coefficient, y is a true target, y_hat is a predicted target, f(x) is a decision score, and k indexes a class.

Common Pitfalls

  • Choosing delta arbitrarily: It should reflect the residual scale at which errors should stop receiving quadratic treatment.

  • Confusing training loss with evaluation metrics: A model may train with log loss but be evaluated with F1 or PR-AUC when classes are imbalanced.

  • Numerical instability: In PyTorch, prefer torch.nn.BCEWithLogitsLoss() over manually applying log to sigmoid outputs.

  • Ignoring data leakage: Fit preprocessing only on training data, using an sklearn Pipeline when appropriate.

  • Assuming robustness is always better: If residuals are normally distributed and extreme values are genuine targets, MSE may provide the better statistical fit.

34

Which recent cost functions target specialized machine learning tasks?

Specialized cost functions encode task structure—such as class imbalance, region overlap, or distribution differences—into the training objective. They can improve performance on the target metric, but may be less convex, harder to tune, or less interpretable than standard losses. “Recent” is time-sensitive, so the following are representative rather than exhaustive.

Read full answer

Applies tomachine learning concepts (all versions)

Loss and Objective

A regularized training objective is commonly written as:

J(θ) = (1/n) * sum(L(y_i, f_θ(x_i))) + R(θ)

  • n is the number of training examples.

  • x_i is example i’s input, y_i is its target, and f_θ(x_i) is the prediction from parameters θ.

  • L is the loss for one example; R(θ) is an optional regularization penalty.

Gradient-based training requires an objective that is differentiable, or whose subgradients or smooth approximations can be optimized. A differentiable objective allows parameter updates to follow the direction in which prediction error decreases.

Main Cost Functions

  • Mean squared error (MSE): MSE = (1/n) * sum((y_i - y_hat_i)^2). It is common for regression and penalizes large errors more strongly, but is sensitive to outliers.

  • Mean absolute error (MAE): MAE = (1/n) * sum(|y_i - y_hat_i|). It is more robust to outliers, although it is non-smooth at zero.

  • Huber loss: With e = y - y_hat, it is 0.5*e^2 when |e| <= delta and delta*(|e| - 0.5*delta) otherwise. The positive threshold delta determines the transition from squared to absolute error, balancing smoothness and outlier robustness.

  • Binary cross-entropy: BCE = -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)). Here y_i is a binary target and p_i is a predicted probability. It is standard for binary and multilabel classification.

  • Categorical cross-entropy: CCE = -(1/n) * sum_i sum_k(y_ik*log(p_ik)). Here y_ik is the target probability for class k of example i, and p_ik is the predicted probability.

  • Hinge loss: hinge = (1/n) * sum(max(0, 1 - y_i*f_i)), where y_i is +1 or -1 and f_i is a signed model score. It supports the margin-based objectives used by SVMs.

  • KL divergence: KL(q || p) = sum(q*log(q/p)), where q and p are compared distributions. It is nonnegative but asymmetric and is often used for distribution matching or as a regularizer.

Specialized Functions in the Literature

Cost function Specialized task Main trade-off
Focal loss, -(1-p_t)^gamma*log(p_t) Classification and dense detection with severe imbalance Downweights easy examples, but gamma requires tuning; p_t is the true-class probability
Dice loss, 1 - (2*sum(y*p))/(sum(y)+sum(p)) Medical-image segmentation Rewards mask overlap but may underweight boundaries; y is the target mask and p the predicted mask
Hausdorff-distance loss Boundary-sensitive segmentation Emphasizes worst local boundary error, but is computationally heavier and non-convex
Cramér-von Mises loss Learning from aggregate label proportions Compares empirical cumulative distribution functions rather than individual labels
Wasserstein loss GANs and distribution matching Measures distribution distance through a learned critic but requires careful critic training
Focal frequency loss Image restoration and reconstruction Emphasizes difficult frequency components rather than weighting all pixels uniformly

Practical implementations often smooth Dice denominators to avoid division by zero. Specialized objectives should be selected using the actual validation metric because improved training loss does not guarantee better deployment performance.

Why Logistic Regression Uses Cross-Entropy

Binary cross-entropy is convex in the weights for standard logistic regression, whereas squared error on sigmoid outputs is generally non-convex. Its main advantage is gradient behavior: with p = sigmoid(w*x), MSE contains an additional sigmoid-derivative factor that can vanish when the model is confidently wrong. Binary cross-entropy has a gradient proportional to (p-y)*x, so it continues to strongly penalize extreme incorrect probabilities.

Regularized Costs

  • L2 regularization adds lambda * sum(w_j^2) to the objective. Ridge regression and conventional weight decay use this penalty; it discourages large weights without normally making them exactly zero.

  • L1 regularization adds lambda * sum(|w_j|). Lasso regression uses this penalty, and some weights can become exactly zero, producing sparse models.

Here lambda is a nonnegative regularization strength and w_j is parameter j.

Common Pitfalls

  • The training loss and deployment metric may differ: optimize log loss for stable learning but report F1, precision, recall, ROC-AUC, or PR-AUC when appropriate. Accuracy can mislead with imbalanced classes.

  • Fit preprocessing only on training data, using a scikit-learn Pipeline; use stratified cross-validation for classification and time-ordered splits for time series.

  • Implement binary cross-entropy with stable probability clipping or use torch.nn.BCEWithLogitsLoss, which combines sigmoid and log-loss calculations safely.

  • Scale features for distance-based methods and gradient optimization, but generally not for tree models. Handle missing values explicitly, monitor overfitting, and fix random seeds for reproducibility.

35

How does loss function shaping affect machine learning models?

Loss function shaping means deliberately choosing or modifying a training objective to favor behaviors such as outlier robustness, sparse predictions, or attention to rare classes. It aligns training with the deployment goal, but the main trade-off is added inductive bias, which can improve one priority while hurting optimization smoothness or performance on ordinary cases.

Read full answer

Applies tomachine learning concepts; NumPy 2.x and scikit-learn 1.5+ examples in Python 3.12; PyTorch 2.x note

Loss, Cost, and Objective

A loss measures the error for one example, while a cost function usually aggregates those losses across the dataset. An objective function may also add regularization or other constraints.

For example:

MSE = (1/n) * Σ (y_i - y_hat_i)^2

Here, n is the number of examples, y_i is the true target for example i, and y_hat_i is its prediction. The sum adds squared errors, and division by n produces their average.

Ordinary gradient descent requires an objective with a useful derivative so the gradient can indicate how parameters should change. Nonsmooth objectives such as MAE and hinge loss can instead be optimized using subgradients.

Common Cost Functions

Cost function Formula When to use it and main trade-off
Mean squared error MSE = (1/n) * Σ (y_i - y_hat_i)^2 Regression; penalizes large errors heavily and is sensitive to outliers.
Mean absolute error MAE = (1/n) * Σ abs(y_i - y_hat_i) Regression with outliers; more robust but less smooth for optimization.
Huber loss 0.5*e_i^2 if abs(e_i) <= delta; otherwise delta*(abs(e_i) - 0.5*delta) Regression combining MSE-like smoothness with MAE-like robustness. e_i = y_i - y_hat_i, and delta is the transition threshold.
Binary cross-entropy BCE = -(1/n) * Σ [y_i log(p_i) + (1-y_i) log(1-p_i)] Binary classification; p_i is the predicted positive probability and y_i is the binary label. Confident wrong predictions receive very large penalties.
Categorical cross-entropy CE = -(1/K) * Σ_i Σ_k y_ik log(p_ik) Multiclass classification with softmax. K is the number of classes, y_ik is the true-class indicator, and p_ik is the predicted probability.
Hinge loss L = (1/n) * Σ max(0, 1 - y_i f(x_i)) SVM classification. x_i is an input, y_i is either -1 or +1, and f(x_i) is the decision score.

| KL divergence | KL(P || Q) = Σ_k P_k log(P_k / Q_k) | Comparing distributions, including distillation. k indexes components, P is the target distribution, and Q is the predicted distribution. |

Regularization and Logistic Regression

A regularized objective commonly has the form:

J = average_loss + λ * R(w)

Here, J is the complete objective, λ controls regularization strength, w contains model weights, and R(w) is the penalty.

  • L2 regularization: R(w) = Σ_j w_j^2. Ridge regression and weight decay discourage large weights and often improve stability.

  • L1 regularization: R(w) = Σ_j abs(w_j). Lasso regression can set some weights exactly to zero, producing sparse models.

In both penalties, j indexes individual weights. Logistic regression uses binary cross-entropy because, with the sigmoid model, it produces a convex objective in the weights and provides stronger, more useful gradients for confident incorrect predictions than squared error applied to probabilities.

Advantages and Trade-offs

Loss shaping can reduce outlier influence with MAE or Huber loss, emphasize rare classes with class-weighted cross-entropy, encourage sparsity with L1, or encode domain-specific costs. However, it can complicate optimization or optimize a proxy rather than the deployment metric. Training with log loss while reporting F1 is valid, provided model selection uses the relevant validation metric.

Code Example

import numpy as np
from sklearn.model_selection import train_test_split

rng = np.random.default_rng(42)


def mse(y, y_hat):
    return float(np.mean((y - y_hat) ** 2))


def mse_gradients(X, y, w, b):
    errors = X @ w + b - y
    return X.T @ errors * (2.0 / len(y)), 2.0 * np.mean(errors)


def sigmoid(values):
    values = np.asarray(values, dtype=float)
    result = np.empty_like(values)
    positive = values >= 0
    result[positive] = 1.0 / (1.0 + np.exp(-values[positive]))
    exp_values = np.exp(values[~positive])
    result[~positive] = exp_values / (1.0 + exp_values)
    return result


def binary_cross_entropy(y, probabilities, epsilon=1e-12):
    probabilities = np.clip(probabilities, epsilon, 1.0 - epsilon)
    return float(-np.mean(
        y * np.log(probabilities)
        + (1.0 - y) * np.log(1.0 - probabilities)
    ))


def bce_gradients(X, y, probabilities):
    return X.T @ (probabilities - y) / len(y), np.mean(probabilities - y)


# Regression: minimize MSE on the training split.
X_reg = rng.normal(size=(200, 1))
y_reg = 3.0 * X_reg[:, 0] + 2.0 + rng.normal(scale=0.2, size=200)
X_reg_train, X_reg_test, y_reg_train, y_reg_test = train_test_split(
    X_reg, y_reg, test_size=0.2, random_state=42
)

w_reg, b_reg = 0.0, 0.0
initial_mse = mse(y_reg_train, X_reg_train @ w_reg + b_reg)

for _ in range(1000):
    predictions = X_reg_train @ w_reg + b_reg
    grad_w, grad_b = mse_gradients(X_reg_train, y_reg_train, w_reg, b_reg)
    w_reg -= 0.05 * grad_w
    b_reg -= 0.05 * grad_b

final_mse = mse(y_reg_train, X_reg_train @ w_reg + b_reg)
test_mse = mse(y_reg_test, X_reg_test @ w_reg + b_reg)
print(f"MSE training cost: {initial_mse:.4f} -> {final_mse:.4f}")
print(f"MSE test cost: {test_mse:.4f}")

# Classification: minimize binary cross-entropy on the training split.
X_clf = rng.normal(size=(200, 2))
probabilities = sigmoid(X_clf @ np.array([2.0, -1.0]) + 0.2)
y_clf = rng.binomial(1, probabilities)
X_clf_train, X_clf_test, y_clf_train, y_clf_test = train_test_split(
    X_clf, y_clf, test_size=0.2, random_state=42, stratify=y_clf
)

w_clf = np.zeros(2)
b_clf = 0.0
initial_bce = binary_cross_entropy(
    y_clf_train, sigmoid(X_clf_train @ w_clf + b_clf)
)

for _ in range(1000):
    probabilities = sigmoid(X_clf_train @ w_clf + b_clf)
    grad_w, grad_b = bce_gradients(X_clf_train, y_clf_train, probabilities)
    w_clf -= 0.1 * grad_w
    b_clf -= 0.1 * grad_b

final_bce = binary_cross_entropy(
    y_clf_train, sigmoid(X_clf_train @ w_clf + b_clf)
)
test_bce = binary_cross_entropy(
    y_clf_test, sigmoid(X_clf_test @ w_clf + b_clf)
)
print(f"BCE training cost: {initial_bce:.4f} -> {final_bce:.4f}")
print(f"BCE test cost: {test_bce:.4f}")

Common Pitfalls

  • Metric mismatch: Training loss may differ from the reported metric. Accuracy can hide poor rare-class performance, so use precision, recall, F1, ROC-AUC, or PR-AUC when class imbalance matters.

  • Numerical instability: Never calculate log(0). In PyTorch, use torch.nn.BCEWithLogitsLoss, which combines sigmoid and binary cross-entropy stably.

  • Poor scaling: Standardize features for gradient descent, SVMs, k-NN, and PCA; tree models generally do not require it.

  • Weak validation: Compare candidate losses using the same cross-validation procedure, such as StratifiedKFold for classification, and keep final evaluation separate from tuning.

  • Overfitting the validation set: Use validation data for model selection rather than repeated manual experimentation.

  • Outlier or imbalance sensitivity: Squared error may let extreme values dominate, while aggregate cross-entropy may favor frequent classes over rare ones.

36

How could quantum computing change cost functions in machine learning?

A cost function (or objective function) measures how well a model’s predictions match target data, typically as an average loss over examples plus any regularization term, and gradient-based training minimizes it to learn parameters. Quantum computing may influence future cost functions by enabling new quantum-native objectives evaluated on quantum hardware, changing optimization landscapes, and introducing noise-aware formulations. The main trade-off is that potential expressivity and speedups come with challenges such as barren plateaus, hardware noise, and limited near-term scalability.

Read full answer

Applies tomachine learning and quantum computing concepts (examples in Python 3.12 context where noted)

How Quantum Computing Interacts with Cost Functions

  • Variational quantum algorithms (VQAs): Near-term quantum machine learning often uses parameterized quantum circuits. A classical optimizer updates circuit parameters θ to minimize a cost function C(θ).

  • Expectation-value objectives: A common form is C(θ) = <ψ(θ)|H|ψ(θ)>, where |ψ(θ)> is the quantum state produced by the circuit, H is a Hermitian operator (Hamiltonian) encoding the task, and <...> denotes the expectation value. The optimizer seeks parameters that minimize this measured value.

  • Hybrid loop: Quantum hardware (or a simulator) estimates C(θ) and sometimes gradients; a classical algorithm (e.g., gradient descent variants) performs the parameter update.

Potential Changes to Cost Function Design

  • Quantum-native objectives: Tasks in chemistry, optimization, or kernel methods may use cost functions derived directly from quantum measurements rather than classical losses like MSE = (1/n) * sum((y_i - ŷ_i)^2) (where n is the number of examples, y_i is the true value, and ŷ_i is the prediction).

  • Barren plateaus: Some quantum cost landscapes become exponentially flat as system size grows, making gradients near zero and training difficult. This motivates local cost functions, layer-wise training, and problem-inspired ansätze.

  • Noise-aware costs: Hardware noise can bias cost estimates. Future formulations may incorporate error mitigation, regularization for robustness, or stochastic objectives that account for measurement uncertainty.

  • Regularization parallels: As in classical learning, constraints or penalty terms may be added to control circuit complexity, e.g., C_total(θ) = C_task(θ) + λ * R(θ), where λ controls regularization strength and R(θ) penalizes undesirable parameter or circuit properties.

Classical vs Quantum Cost Function Considerations

Aspect Classical ML cost functions Quantum-influenced cost functions
Evaluation Computed on classical data and model outputs Often estimated from quantum measurements (expectation values)
Typical examples MSE, MAE, cross-entropy, hinge loss Hamiltonian expectation, fidelity-based objectives, kernel-target alignment
Optimization challenges Non-convexity, local minima, vanishing gradients Barren plateaus, shot noise, hardware noise, non-convex landscapes
Differentiability Required for gradient methods; automatic differentiation common Gradients via parameter-shift or finite differences; estimator variance matters
When to use Standard supervised/unsupervised learning on classical data Quantum-native problems or hybrid models where quantum features may help

Practical Challenges and Research Directions

  • Scalability: Current hardware limits circuit depth and qubit counts; many results rely on simulators for small problems.

  • Data encoding: Mapping classical data into quantum states affects the effective cost landscape and trainability.

  • Verification: Comparing quantum and classical baselines requires careful experimental design to avoid overstating advantages.

Interview Tips

  • Typical mistakes: Claiming quantum computing will simply “speed up” all cost minimization; ignoring barren plateaus and noise; assuming quantum models replace classical losses like cross-entropy in all settings.

  • Key distinctions: Emphasize that cost functions still define the learning objective; quantum computing changes how some objectives are represented and evaluated, especially in hybrid VQA workflows.

  • Follow-up questions interviewers ask: What is a barren plateau and how does it affect training? How do variational quantum algorithms use a cost function? Why might measurement noise change cost function design? When would you still prefer classical objectives like cross-entropy or MSE?

37

How are cost functions applied in reinforcement learning reward shaping?

In reinforcement learning, a cost function (or objective) measures how far an agent’s behavior is from the desired outcome; minimizing it drives learning, while reward shaping adds carefully designed feedback to make that objective easier to optimize. It solves the problem of sparse or delayed rewards by providing denser learning signals. The main trade-off is that useful shaping can speed up training, but poorly designed shaping can change the optimal policy and cause reward hacking.

Read full answer

Applies toreinforcement learning concepts (Python 3.12)

Loss, Cost, and Objectives in RL

  • Loss: the error for one example or transition, e.g. a temporal-difference (TD) error for a single step.

  • Cost/objective: the average loss over data plus any regularization term, e.g. J(w) = (1/n) * sum(L_i) + lambda * R(w). For gradient-based training, the cost must be differentiable (or have usable subgradients) with respect to the parameters w.

  • Value-based objective (TD/MSE form): MSE = (1/n) * sum((y_i - y_hat_i)^2), where n is the number of samples, y_i is the target (for example, r + gamma * max_a' Q(s',a')), y_hat_i is the predicted value, and gamma is the discount factor.

  • Policy-gradient objective: methods often maximize expected return J(theta) = E[sum_t gamma^t * r_t] (or minimize -J(theta)), where theta are policy parameters and r_t is the reward at time t.

  • Reward shaping: replaces r with r' = r + F(s,a,s'), where F is the shaping signal, s is the current state, a the action, and s' the next state. Potential-based reward shaping uses F(s,a,s') = gamma * Phi(s') - Phi(s), with potential function Phi; this form preserves the optimal policy while changing the cost landscape.

Key Cost Functions and Regularization

Cost function Formula (plain notation) Use when Notes
MSE (1/n) * sum((y - y_hat)^2) Regression, value targets Penalizes large errors more
MAE ‘(1/n) * sum( y - y_hat )‘ Regression with outliers Robust, non-smooth at 0
Huber Quadratic for small ‘ e , linear for large e (e = y - y_hat`) Outliers + smooth optimization Combines MSE/MAE behavior
Binary cross-entropy -(1/n) * sum(y*log(p) + (1-y)*log(1-p)) Binary labels/probabilities y in {0,1}, p = y_hat
Categorical cross-entropy -(1/n) * sum_i sum_c y_{i,c}*log(p_{i,c}) Multi-class with softmax c indexes classes
Hinge (1/n) * sum(max(0, 1 - y*y_hat)) Margin-based classifiers (SVM) y in {-1,1}
KL divergence sum_x P(x)*log(P(x)/Q(x)) Matching distributions Used in policy regularization
  • Cross-entropy vs MSE for logistic regression: cross-entropy is convex in the weights with logistic outputs and gives stronger gradients when predictions are confidently wrong; MSE with a sigmoid can produce poor gradients and non-convex optimization.

  • Regularized cost: L2/ridge/weight decay adds lambda * sum(w^2); L1/lasso adds lambda * sum(|w|) and can produce sparse weights. lambda >= 0 controls the strength.

Common Pitfalls

  • Reward hacking/misaligned shaping: dense rewards can teach shortcuts; prefer potential-based shaping when preserving the optimal policy matters.

  • Numerical instability: log(0) breaks BCE; clip probabilities, or use logits-based losses such as torch.nn.BCEWithLogitsLoss in PyTorch 2.x.

  • Objective/metric mismatch: you may train on log loss or TD/MSE but report success rate, return, F1, or RMSE; choose metrics that match the real goal.

  • Non-differentiable pieces: environment rewards are sampled, but the learned objective (value loss, policy surrogate) must support gradient-based updates.

  • Interview follow-ups: potential-based reward shaping, sparse vs dense rewards, TD error vs Monte Carlo targets, entropy regularization, and when to use Huber for robust value learning.

38

How do you troubleshoot cost function optimization in neural networks?

Diagnosing and fixing cost function optimization issues in a neural network means finding why the training objective (average loss over examples, plus any regularization) fails to decrease or generalize, then correcting the data, loss, or training procedure. It solves the problem of stalled or unstable learning, with the main trade-off between optimization speed and stability versus generalization: aggressive settings can drop training loss quickly but often overfit or diverge.

Read full answer

Applies toneural networks in PyTorch 2.x (Python 3.12); NumPy 2.x for gradient examples

Diagnosing Optimization Problems

  • Monitor curves: Plot training and validation loss every epoch. A flat loss suggests a learning-rate, gradient, or loss-definition issue; a diverging loss suggests instability; a large train-validation gap suggests overfitting.

  • Check gradients and activations: Inspect gradient norms and activation distributions. Vanishing gradients keep early layers from learning; exploding gradients cause NaNs or spikes.

  • Verify the objective: The loss is the error for one example; the cost/objective is the average loss plus regularization, e.g. J(w) = (1/n) * sum(L_i) + lambda * R(w). Cost functions must be differentiable for gradient-based training.

  • Sanity checks: Overfit a tiny batch, confirm labels and preprocessing match, and compare the training loss with a separate evaluation metric (for example, train on log loss, report F1).

Common Causes and Fixes

Symptom Likely cause Fix
Loss is NaN or explodes Large learning rate, overflow, log(0) Lower learning rate, gradient clipping, use stable logits losses such as torch.nn.BCEWithLogitsLoss
Loss stays high/flat Bad learning rate, poor initialization, wrong loss scale Tune learning rate (scheduler/warmup), use suitable initialization, standardize inputs
Learns then plateaus/overfits Capacity too high, weak regularization Early stopping, dropout, L2 lambda * sum(w^2) (weight decay), L1 lambda * sum(abs(w)) for sparsity
Good loss, poor task metric Metric mismatch or class imbalance Use task metrics (F1, ROC-AUC, PR-AUC) and class weighting/resampling

Key cost functions (plain notation): MSE = (1/n) * sum((y_i - y_hat_i)^2), where y_i is the true value and y_hat_i the prediction, penalizes large errors; MAE = (1/n) * sum(abs(y_i - y_hat_i)) is robust to outliers; Huber blends MSE near zero and MAE for large errors. Binary cross-entropy = -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)) (p_i is predicted probability) is preferred over MSE for logistic/sigmoid outputs because it is convex in the weights and gives stronger gradients. Categorical cross-entropy with softmax fits multi-class labels; hinge loss fits SVM margins; KL divergence measures distribution mismatch.

Code Example

import numpy as np

rng = np.random.default_rng(42)
X = rng.normal(size=(200, 3))
true_w = np.array([1.5, -2.0, 0.5])
y_reg = X @ true_w + rng.normal(scale=0.1, size=200)          # regression targets
y_cls = (1 / (1 + np.exp(-(X @ true_w))) > 0.5).astype(float) # binary labels

def mse(y: np.ndarray, y_hat: np.ndarray) -> float:
    return float(np.mean((y - y_hat) ** 2))                    # MSE = (1/n)*sum((y-y_hat)^2)

def bce(y: np.ndarray, p: np.ndarray) -> float:
    p = np.clip(p, 1e-7, 1 - 1e-7)                            # avoid log(0)
    return float(-np.mean(y * np.log(p) + (1 - y) * np.log(1 - p)))

def sigmoid(z: np.ndarray) -> np.ndarray:
    return 1.0 / (1.0 + np.exp(-z))

# Gradient descent on MSE (linear model)
w_mse = rng.normal(scale=0.01, size=X.shape[1])
for step in range(5):
    pred = X @ w_mse
    grad = (2 / len(y_reg)) * X.T @ (pred - y_reg)             # d/dw MSE
    w_mse -= 0.1 * grad
    print(f"mse step {step}: {mse(y_reg, pred):.6f}")

# Gradient descent on binary cross-entropy (logistic model)
w_bce = rng.normal(scale=0.01, size=X.shape[1])
for step in range(5):
    prob = sigmoid(X @ w_bce)
    grad = (1 / len(y_cls)) * X.T @ (prob - y_cls)            # d/dw BCE with sigmoid
    w_bce -= 0.5 * grad
    print(f"bce step {step}: {bce(y_cls, prob):.6f}")
# Expected: both printed costs decrease over the 5 steps (exact values vary by platform).

Common Pitfalls

  • Numerical instability: Passing probabilities of 0 or 1 into log breaks training; clip probabilities or use logits-based losses (BCEWithLogitsLoss, CrossEntropyLoss).

  • Data leakage and bad evaluation: Fit preprocessing on training data only (Pipeline), and use metrics suited to the task; accuracy can mislead on imbalanced classes.

  • Wrong objective for the problem: MSE on sigmoid outputs can produce weak gradients; cross-entropy is usually the right choice for classification.

  • Interview follow-ups: Why is cross-entropy convex for logistic regression but MSE is not with a sigmoid? How do L1/L2 change the objective and weights? How would you debug a loss that becomes NaN?

39

How do you validate a cost function against business goals?

A cost-function validation checks that optimizing the training objective improves the intended business KPI, not merely that the objective’s numeric value decreases. Translate business outcomes and error costs into an offline metric and decision policy, test them on representative data and against baselines, and confirm causal value with an online experiment when feasible. The main trade-off is that business KPIs are often non-differentiable, so models usually optimize a differentiable surrogate while thresholds and policies are validated separately.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12 with NumPy 2.x)

Validate Against Business Outcomes

  • Define the KPI: Specify the measurable outcome, such as incremental profit, prevented fraud, retained revenue, or reduced response time.

  • Map errors to economics: For example, use Net value = N_TP * V_TP - N_FP * C_FP - N_FN * C_FN. N_TP, N_FP, and N_FN are true-positive, false-positive, and false-negative counts; V_TP is the value of a correct positive; C_FP and C_FN are the costs of incorrect predictions.

  • Separate training and evaluation: Train with a differentiable loss such as log loss, but report business-aligned metrics such as net value, F1, precision at a target recall, or constrained recall.

  • Use representative validation: Compare against the current rule and existing model using cross-validation, confidence intervals, and an untouched final holdout. Evaluate important customer and time segments separately.

  • Test sensitivity and thresholds: Vary error weights and thresholds on validation data. If changing the intended error costs does not improve the business metric in the expected direction, revise the objective or policy.

  • Confirm causally when possible: Run a controlled A/B test and monitor the KPI, guardrail metrics, and model drift after deployment.

Loss, Objective, and Cost Functions

The loss measures one example’s error. The cost function or training objective aggregates those losses and may add regularization:

J(w) = (1/n) * sum_i(loss_i(y_i, prediction_i)) + lambda * R(w)

Here n is the number of examples, i is an example index, loss_i is its loss, y_i is its target, prediction_i is its prediction, w is the model parameters, lambda is regularization strength, and R(w) is the penalty.

Cost function Formula Appropriate use
Mean squared error MSE = (1/n) * sum((y - y_hat)^2) Regression; strongly penalizes large errors
Mean absolute error MAE = (1/n) * sum(abs(y - y_hat)) Regression where outliers should have limited influence
Huber loss 0.5 * error^2 when abs(error) <= delta; otherwise delta * (abs(error) - 0.5 * delta) Regression balancing smoothness and robustness
Binary cross-entropy BCE = -(1/n) * sum(y * log(p) + (1-y) * log(1-p)) Probabilistic binary classification
Categorical cross-entropy CCE = -(1/n) * sum_i(sum_c(y_ic * log(p_ic))) Multiclass classification with softmax
Hinge loss max(0, 1 - y * f) Margin classifiers such as SVMs; here y is -1 or +1
KL divergence sum(p * log(p/q)) Comparing probability distributions

y is the target, y_hat is the regression prediction, error = y - y_hat, and delta is the Huber cutoff. p is a predicted probability, c is a class index, y_ic is a one-hot class indicator, p_ic is its predicted probability, f is a signed margin, and q is a reference distribution.

Gradient-based training requires a differentiable objective. L2 regularization adds lambda * sum(w^2), while L1 adds lambda * sum(abs(w)) and can produce sparse weights. Logistic regression commonly uses cross-entropy because it is convex in the weights and gives stronger gradients than MSE on sigmoid outputs.

Code Example

import numpy as np

rng = np.random.default_rng(42)
X = rng.uniform(-1.0, 1.0, size=(200, 2))
w_true = np.array([1.5, -1.0])
y_reg = X @ w_true + rng.normal(0.0, 0.25, size=200)
prob_true = 1.0 / (1.0 + np.exp(-(X @ w_true)))
y_cls = (prob_true >= 0.5).astype(float)

indices = rng.permutation(len(X))
train_idx, test_idx = indices[:160], indices[160:]
X_train, X_test = X[train_idx], X[test_idx]
y_reg_train, y_reg_test = y_reg[train_idx], y_reg[test_idx]
y_cls_train, y_cls_test = y_cls[train_idx], y_cls[test_idx]

def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

def mse(y, y_hat):
    return float(np.mean((y - y_hat) ** 2))

def mse_gradient(X, y, w, b):
    error = X @ w + b - y
    return X.T @ error / len(y), float(np.mean(error))

def binary_cross_entropy(y, probabilities):
    p = np.clip(probabilities, 1e-12, 1.0 - 1e-12)
    return float(-np.mean(y * np.log(p) + (1 - y) * np.log1p(-p)))

def bce_gradient(X, y, w, b):
    p = sigmoid(X @ w + b)
    inside = (p > 1e-12) & (p < 1.0 - 1e-12)
    derivative = np.where(inside, p - y, 0.0)
    return X.T @ derivative / len(y), float(np.mean(derivative))

reg_w, reg_b = np.zeros(X_train.shape[1]), 0.0
for step in range(201):
    grad_w, grad_b = mse_gradient(
        X_train, y_reg_train, reg_w, reg_b
    )
    reg_w -= 0.05 * grad_w
    reg_b -= 0.05 * grad_b
    if step % 50 == 0:
        prediction = X_train @ reg_w + reg_b
        print(f"MSE step {step}: {mse(y_reg_train, prediction):.4f}")

print(
    "Regression test RMSE:",
    np.sqrt(mse(y_reg_test, X_test @ reg_w + reg_b)),
)

cls_w, cls_b = np.zeros(X_train.shape[1]), 0.0
for step in range(201):
    grad_w, grad_b = bce_gradient(
        X_train, y_cls_train, cls_w, cls_b
    )
    cls_w -= 0.1 * grad_w
    cls_b -= 0.1 * grad_b
    if step % 50 == 0:
        probability = sigmoid(X_train @ cls_w + cls_b)
        print(
            f"BCE step {step}: "
            f"{binary_cross_entropy(y_cls_train, probability):.4f}"
        )

print(
    "Classification test log loss:",
    binary_cross_entropy(
        y_cls_test, sigmoid(X_test @ cls_w + cls_b)
    ),
)

Common Pitfalls

  • Proxy mismatch: A lower training loss does not prove better revenue or customer outcomes. Test whether the proxy ranks candidates consistently with the business metric.

  • Data leakage and overfitting: Select thresholds and tune parameters using cross-validation or validation folds, not the final test set.

  • Wrong metrics: Accuracy can mislead with imbalanced classes; use precision, recall, F1, PR-AUC, and cost-weighted metrics.

  • Numerical and operational issues: Use logits-based implementations such as torch.nn.BCEWithLogitsLoss; handle missing values, scale features when gradient optimization or regularization requires it, preserve random seeds, and monitor drift.

40

How does A/B testing compare cost functions?

A/B testing compares cost functions indirectly by deploying model variants trained with different objectives and randomly assigning eligible users to each variant. It does not compare MSE, MAE, and cross-entropy values directly because they have different scales and meanings. Its main trade-off is causal evidence from real users versus the need for sufficient traffic, experiment time, and careful control of confounding factors.

Read full answer

Applies tomachine learning concepts (APIs mentioned: scikit-learn 1.5+ and PyTorch 2.x)

Loss, Cost, and Differentiability

A loss measures the error for one example, while a cost function or training objective aggregates losses across the dataset and may include regularization:

J(w) = (1/n) * sum(L(y_i, f(x_i; w))) + R(w)

Here, n is the number of examples, i indexes an example, y_i is its true target, f(x_i; w) is the prediction, w represents model parameters, and R(w) is the regularization term.

Gradient-based training requires an objective whose gradient can be computed with respect to the parameters. MAE is not differentiable exactly when the prediction equals the target, so implementations use a subgradient or a smooth approximation such as Huber loss.

How A/B Testing Compares Objectives

A/B testing does not assign a cost function directly to a user. It assigns users to deployed model variants or policies that use models trained with different objectives:

  1. Define the causal contrast: Specify which training-objective change is being tested and the expected downstream effect.

  2. Train comparable variants: Keep architecture, training data, preprocessing, feature engineering, tuning budget, and serving pipeline as consistent as practical. Changing several factors prevents attribution to the cost function.

  3. Randomize exposure: Assign eligible users or accounts to variants using stable randomization, and keep each user’s assignment consistent throughout the experiment.

  4. Predefine metrics: Choose one primary metric aligned with the business goal, such as conversion rate, revenue, retention, or task success. Add guardrail metrics for latency, safety, fairness, and error rates.

  5. Determine sample size and duration: Run for the planned period so the test can detect the minimum practically important effect.

  6. Analyze by assignment: Use confidence intervals and intention-to-treat analysis, considering both statistical significance and practical significance.

The result applies to the tested population, treatment implementations, and time period; it does not establish that one cost function is universally better. Offline evaluation is useful for screening and debugging, while A/B testing estimates the causal effect of the resulting policy in actual use.

Common Cost Functions

Cost function Formula Typical use and trade-off
Mean squared error MSE = (1/n) * sum((y_i - y_hat_i)^2) Regression; strongly penalizes large errors but is sensitive to outliers.
Mean absolute error MAE = (1/n) * sum(abs(y_i - y_hat_i)) Regression with outliers; more robust but less smooth around zero error.
Huber loss Huber_i = 0.5*e_i^2 if abs(e_i) <= delta, otherwise delta*(abs(e_i) - 0.5*delta) Regression with occasional outliers; delta controls the transition between squared and absolute error.
Binary cross-entropy BCE = -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary classification; strongly penalizes confident incorrect predictions.
Categorical cross-entropy CCE = -(1/n) * sum(log(p_i,y_i)) Multiclass classification with softmax; p_i,y_i is the probability assigned to the true class.
Hinge loss Hinge = (1/n) * sum(max(0, 1 - y_i*f_i)) SVM classification; maximizes classification margin. Here y_i is 1 or -1, and f_i is the margin.

| KL divergence | KL(q || r) = sum_c q_c*log(q_c/r_c) | Comparing distributions; asymmetric, so KL(q || r) generally differs from KL(r || q). |

For regression, y_i and y_hat_i are observed and predicted values, while e_i = y_i - y_hat_i. For cross-entropy, p_i is a predicted probability. For KL divergence, q_c and r_c are probabilities for class or outcome c, and the sum ranges over those outcomes.

Logistic Regression and Regularization

Logistic regression uses cross-entropy because the objective is convex in the weights, while applying MSE to sigmoid outputs can produce saturating, poorly scaled gradients. The loss used for training also need not equal the evaluation metric: a model may be trained with binary cross-entropy but evaluated with F1, PR-AUC, revenue, or another business metric.

Regularization modifies the objective:

  • L2 or ridge: J_L2(w) = J(w) + lambda * sum(w_j^2); this discourages large weights.

  • L1 or lasso: J_L1(w) = J(w) + lambda * sum(abs(w_j)); this can produce sparse weights.

Here, lambda is the nonnegative regularization strength, and j indexes model weights.

Common Pitfalls

  • Comparing unlike losses: Lower MSE does not directly imply better cross-entropy, better F1, or better business performance.

  • Changing multiple factors: Altering the objective, architecture, data, and hyperparameters together prevents attribution to the cost function.

  • Optimizing the wrong objective: F1 is nondifferentiable, so training usually uses a differentiable surrogate or a threshold-selection procedure.

  • Data leakage: Fit preprocessing only on training data or training folds, using scikit-learn’s Pipeline or ColumnTransformer where appropriate.

  • Ignoring class imbalance: Accuracy can be misleading; examine precision, recall, F1, ROC-AUC, and especially PR-AUC for imbalanced data.

  • Numerical instability: Use logits-based losses such as PyTorch 2.x’s torch.nn.BCEWithLogitsLoss instead of applying log manually to sigmoid probabilities.

  • Undersized or repeatedly inspected experiments: Perform power analysis, avoid peeking before the planned endpoint, and report uncertainty rather than only a point estimate.

41

How to implement Adam or AdaGrad from scratch in Python?

Adaptive optimizers such as AdaGrad and Adam minimize a cost function (objective) by adjusting each parameter’s learning rate from past gradients. A loss is the error for one example, while the cost/objective is the average loss over the data plus any regularization term; it must be differentiable for gradient-based training. AdaGrad shrinks updates for frequently updated features, and Adam adds momentum with bias-corrected variance estimates. The main trade-off is extra memory/state and hyperparameters, and adaptive methods can generalize worse than well-tuned SGD with momentum.

Read full answer

Applies toNumPy 2.x (Python 3.12); mentions PyTorch 2.x for the logits-based loss note

Cost Functions and Regularization

  • MSE = (1/n) * Σ(y_i - ŷ_i)^2 (regression; penalizes large errors). MAE = (1/n) * Σ|y_i - ŷ_i| (robust to outliers). Huber is quadratic near 0 and linear for large errors.

  • Binary cross-entropy / log loss = -(1/n) * Σ[y_i*log(ŷ_i) + (1-y_i)*log(1-ŷ_i)] (ŷ_i is predicted probability). Categorical cross-entropy with softmax is used for multi-class targets. Hinge loss is used for SVMs. KL divergence measures difference between distributions.

  • Why not MSE for logistic regression: with a sigmoid, MSE is non-convex in the weights and gives poor gradients; cross-entropy is convex in the weights and yields better gradients.

  • Regularized cost: L2 (ridge, weight decay) adds λ * Σ(w_j^2); L1 (lasso) adds λ * Σ|w_j| and produces sparse weights (λ controls strength). The training loss and evaluation metric can differ (e.g., train on log loss, report F1).

AdaGrad and Adam

For gradient g_t = ∇_θ J(θ) at step t (⊙ element-wise): - AdaGrad: G_t = G_{t-1} + g_t ⊙ g_t, θ_t = θ_{t-1} - (η / (sqrt(G_t) + ε)) ⊙ g_t. Accumulated squared gradients G_t make rates only decrease (can stall). - Adam: m_t = β1*m_{t-1} + (1-β1)*g_t, v_t = β2*v_{t-1} + (1-β2)*(g_t ⊙ g_t), bias-corrected m̂_t = m_t/(1-β1^t), v̂_t = v_t/(1-β2^t), θ_t = θ_{t-1} - η * m̂_t/(sqrt(v̂_t)+ε). Defaults: η=0.001, β1=0.9, β2=0.999, ε=1e-8.

Code Example

import numpy as np

def mse_cost(y, y_hat):
    err = y_hat - y
    return np.mean(err ** 2), (2.0 / len(y)) * err  # (cost, dcost/dy_hat)

def bce_cost(y, p):
    p = np.clip(p, 1e-7, 1.0 - 1e-7)  # avoid log(0)
    cost = -np.mean(y * np.log(p) + (1.0 - y) * np.log(1.0 - p))
    grad = (p - y) / (len(y) * p * (1.0 - p))  # dcost/dp
    return cost, grad

def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

class Adam:
    def __init__(self, lr=0.001, beta1=0.9, beta2=0.999, eps=1e-8):
        self.lr, self.beta1, self.beta2, self.eps = lr, beta1, beta2, eps
        self.t, self.m, self.v = 0, None, None

    def step(self, params, grads):
        if self.m is None:  # initialize state on first step
            self.m = np.zeros_like(params)
            self.v = np.zeros_like(params)
        self.t += 1
        self.m = self.beta1 * self.m + (1.0 - self.beta1) * grads
        self.v = self.beta2 * self.v + (1.0 - self.beta2) * (grads ** 2)
        m_hat = self.m / (1.0 - self.beta1 ** self.t)
        v_hat = self.v / (1.0 - self.beta2 ** self.t)
        params -= self.lr * m_hat / (np.sqrt(v_hat) + self.eps)
        return params

class AdaGrad:
    def __init__(self, lr=0.1, eps=1e-8):
        self.lr, self.eps, self.G = lr, eps, None

    def step(self, params, grads):
        if self.G is None:
            self.G = np.zeros_like(params)
        self.G += grads ** 2
        params -= self.lr * grads / (np.sqrt(self.G) + self.eps)
        return params

rng = np.random.default_rng(42)
# MSE demo (linear regression) with Adam
X = rng.normal(size=(200, 3))
y_reg = X @ np.array([2.0, -1.0, 0.5]) + rng.normal(scale=0.1, size=200)
w = np.zeros(3)
opt = Adam(lr=0.05)
for epoch in range(300):
    cost, dcost_dy = mse_cost(y_reg, X @ w)
    w = opt.step(w, X.T @ dcost_dy)
    if epoch % 100 == 0:
        print(f"MSE epoch={epoch:3d} cost={cost:.4f}")  # cost decreases
print("learned w:", np.round(w, 3))  # close to [2. -1.  0.5]

# BCE demo (logistic regression) with AdaGrad
y_bin = (rng.normal(size=200) > 0).astype(float)
w2 = np.zeros(3)
opt2 = AdaGrad(lr=0.5)
for epoch in range(300):
    p = sigmoid(X @ w2)
    cost, dcost_dp = bce_cost(y_bin, p)
    w2 = opt2.step(w2, X.T @ (dcost_dp * p * (1.0 - p)))  # chain rule through sigmoid
    if epoch % 100 == 0:
        print(f"BCE epoch={epoch:3d} cost={cost:.4f}")  # cost decreases

Common Pitfalls

  • Numerical instability: always clip probabilities for log loss (np.clip); prefer logits-based losses such as torch.nn.BCEWithLogitsLoss over sigmoid + BCE.

  • No bias correction / stale state: skipping Adam’s m̂_t, v̂_t makes early steps too small; reinitialize optimizer state for unrelated runs.

  • AdaGrad decay: G_t only grows, so learning rates can shrink to near zero; Adam/RMSProp address this.

  • Feature scaling and leakage: scale features for gradient-based models (fit scalers on training data only). Interview follow-ups: Adam vs. SGD+momentum generalization, AdamW vs. L2 in Adam, and learning-rate schedules with adaptive methods.

42

How do you minimize a cost function with simulated annealing in Python?

A cost function (or objective) measures how wrong a model is by averaging a per-example loss over the data, often plus a regularization term, and optimization minimizes it to fit parameters. It solves the problem of turning prediction errors into a single scalar to guide learning. The main trade-off is fit versus complexity: lowering training cost too far can increase variance and hurt generalization.

Read full answer

Applies tomachine learning concepts (examples in Python 3.12, NumPy 2.x)

Cost Functions and Formulas

  • Loss vs. cost: Loss L(y, ŷ) is the error for one example with true value y and prediction ŷ; cost J = (1/n) * Σ L(y_i, ŷ_i) + λ*R(w) averages loss over n examples and adds regularization R(w) on weights w with strength λ.

  • Differentiability: Gradient-based training (gradient descent) needs J to be differentiable in w so gradients ∂J/∂w exist. Derivative-free methods like simulated annealing do not require this.

  • MSE: MSE = (1/n) * Σ (y_i - ŷ_i)^2; squares errors, so it penalizes large errors more and is sensitive to outliers.

  • MAE: MAE = (1/n) * Σ |y_i - ŷ_i|; uses absolute errors and is more robust to outliers.

  • Huber loss: Behaves like MSE for small errors and MAE for large ones (controlled by a threshold δ), balancing smooth gradients and outlier robustness.

  • Binary cross-entropy (log loss): BCE = -(1/n) * Σ [y_i*log(p_i) + (1-y_i)*log(1-p_i)], where p_i is the predicted probability for class 1. Logistic regression uses cross-entropy instead of MSE because BCE is convex in the weights for a linear log-odds model and gives stronger, well-behaved gradients when probabilities saturate.

  • Categorical cross-entropy: CCE = -(1/n) * Σ_i Σ_c y_{i,c}*log(p_{i,c}) over classes c with softmax probabilities p_{i,c} for multiclass classification.

  • Hinge loss: max(0, 1 - y_i*f(x_i)) with labels y_i ∈ {-1,+1} and score f(x_i), used by SVMs to maximize margin.

  • KL divergence: Σ p(x)*log(p(x)/q(x)) measures how distribution q diverges from p.

  • Regularized costs: L2 (ridge/weight decay) adds λ * Σ w_j^2 to shrink weights; L1 (lasso) adds λ * Σ |w_j| and can drive some weights to zero for sparse models.

Simulated Annealing

  • How it works: Starts from a candidate solution, proposes random neighbors, always accepts improvements, and accepts worse moves with probability exp(-ΔJ / T) where ΔJ is the cost increase and T is temperature.

  • Cooling schedule: T decreases over iterations (e.g., T ← α*T), reducing random uphill moves so the search settles near a minimum.

  • When to use: Useful for non-differentiable, discontinuous, or multimodal costs where gradients are unavailable or local minima trap gradient descent. It is slower and stochastic, so fix a random seed for reproducibility.

Code Example

import numpy as np

rng = np.random.default_rng(42)

# --- Cost functions (NumPy) ---
def mse(y_true, y_pred):
    return np.mean((y_true - y_pred) ** 2)  # (1/n) * sum((y - y_hat)^2)

def binary_cross_entropy(y_true, p_pred):
    p = np.clip(p_pred, 1e-15, 1 - 1e-15)  # clip to avoid log(0)
    return -np.mean(y_true * np.log(p) + (1 - y_true) * np.log(1 - p))

def mse_grad(y_true, y_pred):
    return 2 * (y_pred - y_true) / y_true.size  # dMSE/dy_hat

def bce_grad(y_true, p_pred):
    p = np.clip(p_pred, 1e-15, 1 - 1e-15)  # clip for numerical stability
    return (p - y_true) / (p * (1 - p) * y_true.size)  # dBCE/dp

# --- Gradient descent steps on synthetic data (costs decrease) ---
y_cls = rng.integers(0, 2, size=50).astype(float)
p = rng.uniform(0.2, 0.8, size=50)
print("BCE gradient descent:")
for step in range(5):
    p = np.clip(p - 0.1 * bce_grad(y_cls, p), 1e-15, 1 - 1e-15)
    print(f"  step {step + 1}, BCE: {binary_cross_entropy(y_cls, p):.4f}")

y_reg = rng.normal(size=50)
y_hat = rng.normal(size=50)
print("MSE gradient descent:")
for step in range(5):
    y_hat = y_hat - 0.5 * mse_grad(y_reg, y_hat)
    print(f"  step {step + 1}, MSE: {mse(y_reg, y_hat):.4f}")

# --- Simulated annealing to minimize MSE for a linear fit ---
X = rng.normal(size=(100, 1))
y_lin = 3.0 * X.ravel() + 1.0 + rng.normal(scale=0.5, size=100)  # true w=[3,1]

def cost_mse(w):
    return mse(y_lin, w[0] * X.ravel() + w[1])

def simulated_annealing(cost_fn, bounds, n_iter=5000, T0=1.0, alpha=0.995, seed=42):
    rng_sa = np.random.default_rng(seed)
    current = np.array([(lo + hi) / 2 for lo, hi in bounds], dtype=float)
    best = current.copy()
    cur_cost = best_cost = cost_fn(current)
    T = T0
    for _ in range(n_iter):
        candidate = np.clip(
            current + rng_sa.normal(scale=0.1, size=current.shape),
            [b[0] for b in bounds], [b[1] for b in bounds],
        )
        cand_cost = cost_fn(candidate)
        delta = cand_cost - cur_cost
        if delta < 0 or rng_sa.random() < np.exp(-delta / max(T, 1e-12)):
            current, cur_cost = candidate, cand_cost
            if cand_cost < best_cost:
                best, best_cost = candidate.copy(), cand_cost
        T *= alpha  # cooling
    return best, best_cost

w_best, c_best = simulated_annealing(cost_mse, bounds=[(-10, 10), (-10, 10)])
print(f"SA weights: {w_best.round(3)}, MSE: {c_best:.4f}")  # near [3.0, 1.0]

Common Pitfalls

  • Numerical instability: log(0) in cross-entropy produces -inf; clip probabilities as shown, or prefer logits-based losses such as torch.nn.BCEWithLogitsLoss in PyTorch 2.x.

  • Wrong cost for the task: Using MSE for classification gives poor gradients and calibration; use cross-entropy for probabilities and hinge for margin-based classifiers.

  • Training loss vs. metric: You may train on log loss but report F1, precision/recall, or ROC-AUC; the optimized objective and the evaluation metric can differ.

  • Annealing schedule sensitivity: Cooling too fast traps simulated annealing in local minima; cooling too slowly wastes iterations. Tune T0, alpha, step scale, and iteration count.

  • Interview follow-ups: Why is cross-entropy convex for logistic regression but MSE is not? When choose MAE or Huber over MSE? How do L1 and L2 change the solution geometry? How do you pick a cooling schedule?

43

How do I implement RMSprop optimization in Python?

RMSprop is a gradient-based optimizer that adjusts each parameter’s learning rate using the recent size of its gradients. It solves the problem of applying one learning rate to parameters with very different gradient scales, but it requires extra state and still depends on careful learning-rate and decay settings.

Read full answer

Applies toPython 3.12 with NumPy 2.x and scikit-learn 1.5+

How RMSprop Works

For gradient g_t at step t, RMSprop maintains an exponentially weighted average of squared gradients:

v_t = beta * v_(t-1) + (1 - beta) * g_t^2

It then updates the parameters:

theta_(t+1) = theta_t - learning_rate * g_t / (sqrt(v_t) + epsilon)

Here:

  • theta_t is the parameter vector at step t.

  • g_t is the gradient of the loss or cost function.

  • v_t is the accumulated squared-gradient value for each parameter.

  • beta controls how quickly old gradient information is forgotten.

  • learning_rate controls the update magnitude.

  • epsilon prevents division by zero.

Unlike ordinary gradient descent, parameters receiving small gradients can receive larger effective updates. The main trade-off is adaptive convergence versus additional optimizer state and sensitivity to learning_rate, beta, and epsilon.

Loss, Cost, and Common Objectives

A loss measures the error for one example. A cost or objective aggregates losses across the dataset and may include regularization:

cost = average_loss + regularization_term

Gradient-based training requires an objective whose derivative can be calculated. This is why training commonly uses differentiable losses or appropriate surrogate losses.

Objective Formula Typical use
Mean Squared Error (MSE) (1/n) * sum((y_i - y_hat_i)^2) Regression; penalizes large errors more strongly
Mean Absolute Error (MAE) (1/n) * sum(abs(y_i - y_hat_i)) Regression with outliers
Huber loss (1/n) * sum(huber(y_i - y_hat_i)) Regression balancing MSE smoothness and MAE robustness
Binary cross-entropy -(1/n) * sum(y_i*log(p_i) + (1-y_i)*log(1-p_i)) Binary classification
Categorical cross-entropy -(1/n) * sum(y_ik*log(p_ik)) Multiclass classification
Hinge loss (1/n) * sum(max(0, 1 - y_i*f_i)) Margin-based classifiers such as SVMs
KL divergence sum(P_i * log(P_i / Q_i)) Comparing probability distributions

Here, n is the number of examples, i identifies an example, y_i is its target, y_hat_i is a predicted value, p_i is a predicted probability, k identifies a class, and f_i is a signed classification score. Huber loss uses squared error near zero and linear error beyond a threshold delta.

For logistic regression, binary cross-entropy is preferred over MSE because BCE is convex in the weights with linear logits. Applying MSE to sigmoid probabilities can create a non-convex objective, while sigmoid saturation can also make gradients very small.

Common regularizers modify the objective:

  • L2 regularization: loss + lambda * sum(w_j^2); used by ridge regression and weight decay.

  • L1 regularization: loss + lambda * sum(abs(w_j)); used by lasso and can produce sparse weights.

Code Example

import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import (
    StratifiedKFold,
    cross_validate,
    train_test_split,
)
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.base import BaseEstimator, ClassifierMixin


def sigmoid(z):
    """Compute sigmoid without overflowing for large positive or negative z."""
    result = np.empty_like(z, dtype=np.float64)
    positive = z >= 0
    result[positive] = 1.0 / (1.0 + np.exp(-z[positive]))
    exp_z = np.exp(z[~positive])
    result[~positive] = exp_z / (1.0 + exp_z)
    return result


def mse(y, y_hat):
    """Mean squared error."""
    return np.mean((y - y_hat) ** 2)


def mse_gradient(y, y_hat):
    """Gradient of MSE with respect to predicted values."""
    return (2.0 / len(y)) * (y_hat - y)


def binary_cross_entropy(y, probabilities):
    """Mean binary cross-entropy with protected logarithms."""
    probabilities = np.clip(probabilities, 1e-15, 1.0 - 1e-15)
    return -np.mean(
        y * np.log(probabilities)
        + (1.0 - y) * np.log(1.0 - probabilities)
    )


def bce_gradient(y, probabilities):
    """Gradient of mean BCE with respect to the logits (sigmoid inputs)."""
    return (probabilities - y) / len(y)


def rmsprop(
    initial_theta,
    gradient_fn,
    objective_fn,
    learning_rate=0.01,
    beta=0.9,
    epsilon=1e-8,
    steps=200,
):
    """Optimize a vector of parameters with RMSprop."""
    theta = np.asarray(initial_theta, dtype=np.float64).copy()
    velocity = np.zeros_like(theta)
    costs = []

    for _ in range(steps):
        gradient = gradient_fn(theta)
        velocity = beta * velocity + (1.0 - beta) * gradient**2
        theta -= learning_rate * gradient / (np.sqrt(velocity) + epsilon)
        costs.append(objective_fn(theta))

    return theta, np.asarray(costs)


class RMSpropClassifier(BaseEstimator, ClassifierMixin):
    """Binary logistic regression trained with RMSprop."""

    def __init__(
        self,
        learning_rate=0.01,
        beta=0.9,
        epsilon=1e-8,
        steps=200,
    ):
        self.learning_rate = learning_rate
        self.beta = beta
        self.epsilon = epsilon
        self.steps = steps

    def fit(self, X, y):
        X = np.asarray(X, dtype=np.float64)
        y = np.asarray(y, dtype=np.float64)
        n_features = X.shape[1]

        def probabilities(theta):
            return sigmoid(X @ theta[:-1] + theta[-1])

        def objective_fn(theta):
            return binary_cross_entropy(y, probabilities(theta))

        def gradient_fn(theta):
            predictions = probabilities(theta)
            error = bce_gradient(y, predictions)
            return np.concatenate((X.T @ error, [np.sum(error)]))

        self.theta_, self.cost_history_ = rmsprop(
            initial_theta=np.zeros(n_features + 1),
            gradient_fn=gradient_fn,
            objective_fn=objective_fn,
            learning_rate=self.learning_rate,
            beta=self.beta,
            epsilon=self.epsilon,
            steps=self.steps,
        )
        self.classes_ = np.array([0, 1])
        return self

    def predict_proba(self, X):
        X = np.asarray(X, dtype=np.float64)
        positive = sigmoid(X @ self.theta_[:-1] + self.theta_[-1])
        return np.column_stack((1.0 - positive, positive))

    def predict(self, X):
        positive = self.predict_proba(X)[:, 1]
        return np.where(positive >= 0.5, self.classes_[1], self.classes_[0])


# Synthetic classification data with a fixed seed.
X, y = make_classification(
    n_samples=600,
    n_features=5,
    n_informative=3,
    n_redundant=0,
    class_sep=1.5,
    random_state=42,
)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# The scaler is fitted only on training data, preventing leakage.
pipeline = Pipeline(
    [
        ("scaler", StandardScaler()),
        ("classifier", RMSpropClassifier()),
    ]
)

pipeline.fit(X_train, y_train)
predictions = pipeline.predict(X_test)

print(f"Initial BCE: {pipeline.named_steps['classifier'].cost_history_[0]:.4f}")
print(f"Final BCE:   {pipeline.named_steps['classifier'].cost_history_[-1]:.4f}")
print(
    "Cost decreased:",
    pipeline.named_steps["classifier"].cost_history_[-1]
    < pipeline.named_steps["classifier"].cost_history_[0],
)
print(f"Test accuracy: {np.mean(predictions == y_test):.3f}")

scoring = {
    "accuracy": "accuracy",
    "precision": "precision",
    "recall": "recall",
    "f1": "f1",
    "roc_auc": "roc_auc",
    "pr_auc": "average_precision",
}

results = cross_validate(
    pipeline,
    X,
    y,
    cv=StratifiedKFold(n_splits=5, shuffle=True, random_state=42),
    scoring=scoring,
)

for metric, scores in results.items():
    print(f"{metric}: {scores.mean():.3f} +/- {scores.std():.3f}")

Practical Guidance

  • Scale features when using gradient-based optimization. Trees generally do not require scaling.

  • Put scaling and other learned preprocessing inside a Pipeline to prevent data leakage.

  • Each RMSprop step costs approximately O(n * d) for n examples and d parameters. Optimizer state requires O(d) memory.

  • Training loss and reported metrics need not match. For example, optimize BCE but report F1 when class imbalance makes accuracy misleading.

  • Use cross-validation for model selection and reserve the test set for final evaluation.

Common Pitfalls

  • Dividing by sqrt(v) without epsilon can cause division by zero.

  • Applying MSE to sigmoid probabilities can produce weak or vanishing gradients.

  • Using log(0) causes numerical instability; clip probabilities or use logits-based losses.

  • Reporting training accuracy can hide overfitting.

  • Interpreting an imbalanced dataset with accuracy alone can hide poor minority-class performance.

  • Tuning hyperparameters repeatedly against the test set turns it into an implicit validation set.

Interview Tips

Common follow-up questions include:

  • How does RMSprop differ from momentum and Adam?

  • Why are squared gradients accumulated instead of gradients?

  • What happens when beta is close to 1 or 0?

  • How would you implement Adam?

  • Which loss is more appropriate for outliers, and why?

  • How would you prevent preprocessing leakage during cross-validation?

Sign in to keep reading

You've opened 2 full answers. Sign in to read all 43 Cost Function answers, mark what you know and pick up where you left off.

Report a mistake

What's wrong?

All Machine Learning & Data Science subjects