PyTorch - BCELoss: ValueError: Target and input must have the same number of elements
When I use BCELoss as the loss function of my neural network, getting the ValueError: Target and input must have the same number of elements.

Here is my code for test phase (which is a quite typical test phase code):

network.eval()
test_loss = 0
correct = 0
with torch.no_grad():
    for data, target in test_loader:
        data, target = data.to(device), target.to(device)

    output = network(data)
    output = output.to(device)
    test_loss += loss_function(output, target).item() # error happens here
    _, predicted = torch.max(output.data, 1)
    correct += (predicted == target).sum().item()


The shape of the variable output is [1000, 10] as there are 10 target classes (in MNIST dataset), and the shape of the variable target is [1000] as it contains the target classes of the tested batch (the batch size for test is set to 10). So, the question is how can I apply BCELoss as the loss function of a CNN network?

p.s. The dataset I use is the MNIST dataset which is provided by the torchvision library.

p.s. The answer provided to a similar question here does not propose a solution for my case.

The answer you claim does not propose a solution, does in fact solves your problem:


  your targets are incomplete! If there are multiple classes, you should work with torch.nn.CrossEntropyLoss instead of torch.nn.BCELoss()


To recap, torch.nn.BCELoss() is intended to be used for a task of classifying c independant binary attributes per input example. You, on the other hand, have the task of classifying each output into one of c mutually exclusive classes. For this task you need a different loss, torch.nn.CrossEntropyLoss().
The different tasks, represented by the different loss functions call for different supervision (labels). If you want to classify each example to one of c mutually exclusive classes, you only need one integer label for each example (as you have in your mnist example). However, if you want to classify each example into c independent binary attributes, you need, for each example c binary labels - and this is why pytorch gives you an error. 
