An edge case: do we need to consider empty partition here (`reduce` complains about empty collection)? We can use `reduceOption` together with default values for `gradientSum` and `lossSum`.