By the way, if someone is passing-by here, there could be a typo on the torch.nn.functional.nll_loss documentation.
In case of a K-dimensional loss it should be a target shape of (N,d1,d2,…,dK) instead of (N,C,d1,d2,…,dK) (which is only applying to the input shape) ?