Who Is Carrying the Loss?

Loss FunctionsEasy+10 XP+10

Who Is Carrying the Loss?

A loss is one number, but it is assembled from every example in the batch, and the examples do not chip in equally. The examples holding most of the loss are the ones training works hardest to fix, because shrinking their errors is the quickest way to shrink the total.

Here is a week of a model's temperature forecasts, in °C:

MonTueWedThuFriSatSun
forecast20202122202420
recorded18211923312217

How many of those seven days hold most of the loss may depend on which loss is doing the counting.

Task: write loss_carriers(predictions, targets, fraction), returning [mse_count, mae_count]: the smallest number of examples that together hold at least fraction of the loss, under MSE and under MAE respectively.

  • predictions and targets are equal-length lists with one entry per example. An example's error is its prediction minus its target.
  • An example's contribution is its squared error under MSE, and its absolute error (the error's size, with the sign dropped) under MAE.
  • The share held by a group of examples is the sum of their contributions divided by the sum of every example's contribution. Both losses go on to divide by the number of examples, but that divides the top and the bottom of every share by the same count, so it never changes a share.
  • A group qualifies when its share is at least fraction. Landing exactly on fraction counts.
  • fraction is strictly between 000 and 111. At least one prediction misses its target, so the loss is never zero.
  • Return the two counts as whole numbers.

The forecast week above, with fraction = 0.75, is the first test.

Both counts describe the same predictions, so any gap between them comes from the ruler alone. A wide gap means the choice of loss decides how many examples get to steer the model, which is fine when those few examples are real and a problem when they are broken readings.

1
-2
2
-1
1
MSE
2.2
MAE
1.4

Examples

predictions = [20, 20, 21, 22, 20, 24, 20]
targets = [18, 21, 19, 23, 31, 22, 17]
fraction = 0.75
returns [1, 4]
predictions = [150, 204, 330, 262, 188, 240]
targets = [146, 208, 332, 260, 190, 238]
fraction = 0.5
returns [2, 2]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run