bugger.
All challenges/Pythoneasygsoc-ignite-weighted-loss-v1
THE INVESTIGATION

The last batch that changed the loss

A training report gives a small final batch as much influence as a full batch.

MLPyTorch-IgniteMetric aggregation

What’s going wrong

Original ML practice for NumFOCUS / PyTorch-Ignite contribution preparation. This is not an upstream bug reproduction.

An evaluation report averages the mean losses reported by its batches. A short final batch changes the reported dataset loss even though each individual sample should carry equal weight. Repair summarize so the result is the mean loss per sample.

Read one JSON object {"batches":[{"meanLoss":1.0,"samples":3},{"meanLoss":9.0,"samples":1}]} from stdin. There are 0 to 128 batches. Each meanLoss is a finite number from 0 to 1000000, already the mean over that batch; samples is an integer from 0 to 10000. A batch with zero samples has no influence, regardless of its meanLoss. No other weights or loss reductions are used.

Print {"loss":3.0,"samples":4}. The samples field is the sum of actual sample counts. The loss field is the per-sample mean rounded with Python round(value, 6), or null when the total sample count is zero. Keep the returned numeric value; trailing zeros need not be displayed. The input is valid and within these bounds. Use only the Python standard library.

Expected behavior

Aggregate batch mean losses by their actual sample counts and report null when no observations were seen.

Reproduction

01 A short final batch must not get equal weight
{"batches":[{"meanLoss":1.0,"samples":3},{"meanLoss":9.0,"samples":1}]}
{"loss":3.0,"samples":4}
02 Equal batches remain a passing control
{"batches":[{"meanLoss":2.0,"samples":2},{"meanLoss":4.0,"samples":2}]}
{"loss":3.0,"samples":4}
03 No batches have no computable loss
{"batches":[]}
{"loss":null,"samples":0}

Before you begin

  • Python dictionaries, loops and sums
  • The difference between a total and a mean
  • A regression test that changes batch sizes while keeping sample losses fixed

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.