Implement batch MSE and MAE losses and their gradients in NumPy

Quick Overview

Implement mean squared error and mean absolute error losses and their gradients in NumPy for inputs of any shape. Tests averaging over every element, the MAE subgradient at a zero residual, shape checking before broadcasting, and why the two losses react differently to outliers.

Implement batch MSE and MAE losses and their gradients in NumPy

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `mse_loss`, `mae_loss`, `mse_grad` and `mae_grad` taking array-likes `y_true` and `y_pred` of any shape. Losses average over every element (N = total element count) and return a Python `float`. Gradients are with respect to `y_pred` and return a float array of the same shape as `y_pred`. - `mse_grad` = `2 * (y_pred - y_true) / N` - `mae_grad` = `sign(y_pred - y_true) / N` (the subgradient is `0` where `y_pred == y_true`) For example: ```text y_true = [1.0, 2.0, 3.0, 4.0] y_pred = [1.5, 2.0, 2.0, 6.0] mse_loss -> 1.3125 mae_loss -> 0.875 mse_grad -> [0.25, 0.0, -0.5, 1.0] mae_grad -> [0.25, 0.0, -0.25, 0.25] ``` ```hint Watch the shapes Try subtracting a `(4,)` array from a `(4, 1)` array in NumPy and look at the shape of the result. Decide where your shape check has to run. ``` ### Constraints & Assumptions - Inputs may be 1-D or multi-dimensional, with at least one element. - Mismatched shapes raise `ValueError`. - Values are finite. Use NumPy only. ### What a Strong Answer Covers - Converting both inputs to float arrays and rejecting mismatched shapes before any arithmetic, so broadcasting cannot hide a bug. - Using the total element count N in both the loss and the gradient, so each gradient is the true derivative of the loss reported. - The MAE subgradient at a zero residual, and gradients that keep the shape of `y_pred`. - Returning a plain Python `float` from the two losses. - Why the MSE and MAE gradients react so differently to one large residual. ### Follow-up Questions - Why does an outlier affect MSE far more than MAE? - How would you implement Huber loss and its gradient? - How would you check these gradient functions numerically, and what happens to the check for MAE when a residual is exactly zero?

Overview: Implement mean squared error and mean absolute error losses and their gradients in NumPy for inputs of any shape. Tests averaging over every element, the MAE subgradient at a zero residual, shape checking before broadcasting, and why the two losses react differently to outliers.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement mse_loss, mae_loss, mse_grad and mae_grad taking array-likes y_true and y_pred of any shape. Losses average over every element (N = total element count) and return a Python float. Gradients are with respect to y_pred and return a float array of the same shape as y_pred.

  • mse_grad = 2 * (y_pred - y_true) / N
  • mae_grad = sign(y_pred - y_true) / N (the subgradient is 0 where y_pred == y_true )

For example:

y_true = [1.0, 2.0, 3.0, 4.0]
y_pred = [1.5, 2.0, 2.0, 6.0]

mse_loss -> 1.3125
mae_loss -> 0.875
mse_grad -> [0.25, 0.0, -0.5, 1.0]
mae_grad -> [0.25, 0.0, -0.25, 0.25]

Constraints & Assumptions

  • Inputs may be 1-D or multi-dimensional, with at least one element.
  • Mismatched shapes raise ValueError .
  • Values are finite. Use NumPy only.

What a Strong Answer Covers Guidance

  • Converting both inputs to float arrays and rejecting mismatched shapes before any arithmetic, so broadcasting cannot hide a bug.
  • Using the total element count N in both the loss and the gradient, so each gradient is the true derivative of the loss reported.
  • The MAE subgradient at a zero residual, and gradients that keep the shape of y_pred .
  • Returning a plain Python float from the two losses.
  • Why the MSE and MAE gradients react so differently to one large residual.

Follow-up Questions Guidance

  • Why does an outlier affect MSE far more than MAE?
  • How would you implement Huber loss and its gradient?
  • How would you check these gradient functions numerically, and what happens to the check for MAE when a residual is exactly zero?