Assertion error of nn.BCELoss

When I use nn.BCELoss() for the criterion, I have the assertion error:

anaconda3/lib/python3.6/site-packages/torch/nn/_functions/thnn/ in forward(self, input, target)
     21     def forward(self, input, target):
---> 22         assert input.nelement() == target.nelement()
     23         self._resize_weight(target)
     24         result = super(BCELoss, self).forward(input, target)

But when I change the loss into nn.CrossEntropy(), it is OK to run.
What is the problem here?

BCELoss expects input and target to have the same number of elements. CrossEntropyLoss requires targets to be nBatch, input to be nBatch x nClasses. Read the documentation.