The error was avoided by making sure:

targets are float
the network outputs one value per sample
The output is flattened using view
For example:


Blockquote
loss_fn = nn.BCEWithLogitsLoss()
output = net(data)
loss = loss_fn(output.view(-1), target.float())

Hope this helps