ValueError: Target and input must have the same number of elements. target nelement (20) != input nelement (1)

This post gives you a quick overview for the different approaches for a binary classification use case.
Basically you can treat it as a “real” binary classification with a single output unit (and nn.BCEWithLogitsLoss) or as a mutli-class classification using 2 classes (and nn.CrossEntropyLoss).

For the first use case, pass the raw logits to nn.BCEWithLogitsLoss for more numerical stability compared to sigmoid + nn.BCELoss.

1 Like