Binary cross entropy weights

From the torch.nn.BCELoss documentation, it says that the weight parameter, if given, has to be a Tensor of size nbatch. However, when I use weight of size nbatch, I got the following error:

The size of tensor a (200) must match the size of tensor b (36) at non-singleton dimension 3.

My tensors sizes are:

input = (36, 1, 200, 200)
target = (36, 1, 200, 200)
weight = (36,)

What I want to do is to have weights 0 or 1 in the weight array in order to ignore samples (since we don’t have ìgnore_index` parameter in the binary cross entropy loss) from the batch where we have 0 and include them where we have 1. What am I missing ?

Side note: I know I can use NLLLoss and build a one-hot encoding for it, however, my problem is binary and for some other reasons isn’t feasible to do that.

That’s strange indeed.
It seems that a call to _infer_size() in binary_cross_entropy() is throwing this error.

For a workaround you could try creating the weight tensor with dims [36, 1, 1, 1].

1 Like

Exactly that, the _infer_size() is calling a C function infer_size from ATen and then this function is throwing this error. I did what you suggested as a workaround and it is working fine, thanks !