Implement an Uncertainty-Weighted Multi-Task Loss and Its Analytic Gradients

Quick Overview

Implement a multi-task loss that weights softmax cross-entropy and mean squared error by learnable log-variances, plus its analytic gradients. Tests numerically stable log-softmax for extreme logits, careful gradient derivation, and how uncertainty weighting balances tasks.

Implement an Uncertainty-Weighted Multi-Task Loss and Its Analytic Gradients

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `multitask_loss(logits, labels, reg_pred, reg_target, log_vars)` to combine softmax cross-entropy and MSE using learnable log-variances `log_vars = [s_cls, s_reg]`, returning a Python `float`. Implement `multitask_loss_grad(logits, labels, reg_pred, reg_target, log_vars)` to return the analytic gradients `(grad_logits, grad_reg_pred, grad_log_vars)`, shaped like `logits`, `reg_pred` and `(2,)`. ```python L_cls = mean_i( -log softmax(logits[i])[labels[i]] ) L_reg = mean( (reg_pred - reg_target) ** 2 ) loss = exp(-s_cls) * L_cls + 0.5 * exp(-s_reg) * L_reg + 0.5 * s_cls + 0.5 * s_reg ``` Example: ```python logits = np.zeros((4, 2)) labels = [0, 1, 0, 1] reg_pred = [1.0, 2.0, 3.0, 4.0] reg_target = [0.0, 0.0, 0.0, 0.0] log_vars = [0.0, np.log(2.0)] multitask_loss(logits, labels, reg_pred, reg_target, log_vars) # 2.9147 (ln 2 + 0.25 * 7.5 + 0.5 * ln 2) grad_logits, grad_reg_pred, grad_log_vars = multitask_loss_grad( logits, labels, reg_pred, reg_target, log_vars) # grad_reg_pred -> [0.125, 0.25, 0.375, 0.5] # grad_log_vars -> [-0.1931, -1.375] ``` ```hint Extreme logits `np.exp(1000)` overflows. Look for a way to compute the log-probabilities without ever exponentiating a large positive number. ``` ```hint Gradient bookkeeping Differentiate each term of the loss on its own. Watch the 0.5 in front of the regression term, and note that each log-variance appears in exactly two terms. ``` ### Constraints & Assumptions - `logits` has shape `(N, C)`, and `labels` are integers in `[0, C)`. - `reg_pred` and `reg_target` are 1-D arrays of equal length. - Logits may be as large as ±1000. Loss and gradients must stay finite and exact. - Inputs may be lists or arrays. Use NumPy only. ### What a Strong Answer Covers - A cross-entropy that stays finite and exact for logits near ±1000. - Correct gradients for the logits and the regression predictions, including the task weight and the division by each task's own sample count. - Correct gradients for both log-variances, checked against the example. - What the `0.5 * s` terms do to the task weights, and the time and memory cost. ### Follow-up Questions - What is the optimal `log_vars` for fixed task losses, and what does it imply? - Why parameterize with log-variance instead of $\sigma^2$ directly? - How would you generalize this to K tasks with mixed loss types?

Overview: Implement a multi-task loss that weights softmax cross-entropy and mean squared error by learnable log-variances, plus its analytic gradients. Tests numerically stable log-softmax for extreme logits, careful gradient derivation, and how uncertainty weighting balances tasks.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement multitask_loss(logits, labels, reg_pred, reg_target, log_vars) to combine softmax cross-entropy and MSE using learnable log-variances log_vars = [s_cls, s_reg], returning a Python float. Implement multitask_loss_grad(logits, labels, reg_pred, reg_target, log_vars) to return the analytic gradients (grad_logits, grad_reg_pred, grad_log_vars), shaped like logits, reg_pred and (2,).

L_cls = mean_i( -log softmax(logits[i])[labels[i]] )
L_reg = mean( (reg_pred - reg_target) ** 2 )
loss  = exp(-s_cls) * L_cls + 0.5 * exp(-s_reg) * L_reg + 0.5 * s_cls + 0.5 * s_reg

Example:

logits = np.zeros((4, 2))
labels = [0, 1, 0, 1]
reg_pred = [1.0, 2.0, 3.0, 4.0]
reg_target = [0.0, 0.0, 0.0, 0.0]
log_vars = [0.0, np.log(2.0)]

multitask_loss(logits, labels, reg_pred, reg_target, log_vars)
# 2.9147  (ln 2 + 0.25 * 7.5 + 0.5 * ln 2)

grad_logits, grad_reg_pred, grad_log_vars = multitask_loss_grad(
    logits, labels, reg_pred, reg_target, log_vars)
# grad_reg_pred -> [0.125, 0.25, 0.375, 0.5]
# grad_log_vars -> [-0.1931, -1.375]

Constraints & Assumptions

  • logits has shape (N, C) , and labels are integers in [0, C) .
  • reg_pred and reg_target are 1-D arrays of equal length.
  • Logits may be as large as ±1000. Loss and gradients must stay finite and exact.
  • Inputs may be lists or arrays. Use NumPy only.

What a Strong Answer Covers Guidance

  • A cross-entropy that stays finite and exact for logits near ±1000.
  • Correct gradients for the logits and the regression predictions, including the task weight and the division by each task's own sample count.
  • Correct gradients for both log-variances, checked against the example.
  • What the 0.5 * s terms do to the task weights, and the time and memory cost.

Follow-up Questions Guidance

  • What is the optimal log_vars for fixed task losses, and what does it imply?
  • Why parameterize with log-variance instead of σ2\sigma^2 directly?
  • How would you generalize this to K tasks with mixed loss types?