How to get pixel-wise BCELoss of an end to end framework?

I want to get the pixel-wise BCELoss because some pixels don’t have ground truth. So I want to set the loss of those unlabeled pixels to 0.
But it seems like the current BCELoss function can just provide the sum or average?

class torch.nn.BCELoss(weight=None, size_average=True)
weight (Tensor, optional) – a manual rescaling weight given to the loss of each batch element. If given, has to be a Tensor of size “nbatch”.
size_average (bool, optional) – By default, the losses are averaged over observations for each minibatch. However, if the field size_average is set to False, the losses are instead summed for each minibatch. Default: True

Which PyTorch version are you using?
There should be a reduce argument in the current version.
Setting reduce=False will give your the desired loss.

1 Like

What I am using is 0.3.0. I can’t install the latest version on our server with several days’ trials.:rofl:

Why not? Do you get an error?

Yes! Thank you for reminding me about my failed installation.


I thought even though setting reduce=False in bce loss, I sum all losses of pixels but just do not average or not sum of loss of “batch”.

If I am using reduce=False, is it right that I don’t make summation of loss of pixels?

Thank you in advance.

If you are using reduction='none' (which is the new keyword), the loss won’t be reduced in any dimension:

x = torch.randn(2, 3, 4, 5, requires_grad=True)
y = torch.randn(2, 3, 4, 5)
criterion = nn.BCEWithLogitsLoss(reduction='none')
loss = criterion(x, y)
> torch.Size([2, 3, 4, 5])