According to my understanding, the following nloss.sum() and sloss should be equal, but the actual is different. Why is this?
import torch
import torch.nn as nn

mean_loss = nn.BCELoss(reduction='mean')
sum_loss = nn.BCELoss(reduction='sum')
none_loss = nn.BCELoss(reduction='none')

prediction = torch.rand((4, 1, 256, 256))
target = torch.randint(0, 2, (4, 1, 256, 256), dtype=torch.float32)

mloss = mean_loss(prediction, target)
sloss = sum_loss(prediction, target)
nloss = none_loss(prediction, target)

print(f"none_loss: {nloss}")
print(f"none_loss size: {nloss.size()}")

print(f"none_loss sum: {nloss.sum()}")
print(f"sum_loss: {sloss}")

print(f"none_loss mean: {nloss.mean()}")
print(f"sum_loss/(4*1*256*256): {sloss / (4 * 1 * 256 * 256)}")
print(f"mean_loss: {mloss}")

output:
none_loss: tensor([[[[1.3495, 0.2165, 0.1645,  ..., 0.9105, 1.3246, 2.3588],
          [0.2977, 1.2160, 0.2346,  ..., 0.2156, 0.0112, 0.0647],
          [2.3740, 0.0570, 1.7437,  ..., 1.9400, 0.1285, 2.8168],
          ...,
          [0.6525, 0.3042, 0.9111,  ..., 1.0126, 1.0627, 6.1224],
          [4.6391, 0.6456, 0.9346,  ..., 0.9919, 0.0441, 0.0186],
          [1.5025, 0.8117, 0.3026,  ..., 1.2144, 0.8634, 0.0161]]],


        [[[1.8923, 1.4190, 1.1883,  ..., 0.4101, 1.8231, 0.7425],
          [0.0231, 0.7847, 2.2190,  ..., 0.5121, 0.1161, 1.3471],
          [0.9289, 3.2961, 0.2482,  ..., 2.1655, 0.3986, 0.3489],
          ...,
          [0.0197, 2.0553, 0.0988,  ..., 0.0907, 0.8908, 0.6585],
          [0.7845, 4.9424, 1.9141,  ..., 2.5432, 2.2620, 1.1243],
          [0.6280, 0.2520, 1.0300,  ..., 0.4113, 1.2322, 0.4129]]],


        [[[3.9760, 1.3871, 0.5002,  ..., 0.0077, 0.3474, 0.0095],
          [3.5548, 2.1170, 1.0698,  ..., 0.3680, 2.2655, 0.0573],
          [1.6277, 2.4666, 0.0724,  ..., 0.6252, 0.1662, 0.3559],
          ...,
          [1.9241, 0.0977, 1.0205,  ..., 1.4400, 0.4261, 1.4168],
          [0.3839, 1.5772, 0.2207,  ..., 0.4982, 1.3771, 0.0717],
          [0.6420, 0.4036, 0.5927,  ..., 1.3667, 2.6873, 0.0586]]],


        [[[1.5006, 0.1055, 0.5871,  ..., 0.2185, 0.3801, 0.0206],
          [0.0636, 0.2080, 0.5735,  ..., 0.3822, 0.7734, 0.7849],
          [0.8799, 1.9971, 1.4169,  ..., 0.0417, 0.4123, 1.9111],
          ...,
          [0.9491, 0.8655, 0.8146,  ..., 0.0889, 1.1045, 0.0404],
          [2.3005, 0.9741, 1.6709,  ..., 0.3271, 0.4325, 1.3791],
          [1.7653, 6.5351, 1.3232,  ..., 0.4984, 0.6124, 1.4612]]]])
none_loss size: torch.Size([4, 1, 256, 256])
none_loss sum: 261970.9375
sum_loss: 261969.8125
none_loss mean: 0.9993398189544678
sum_loss/(4*1*256*256): 0.999335527420044
mean_loss: 0.999335527420044