Check out piwise 167. For context my target have shape batch x height x width with pixel values equal the class indices of the corresponding segmentation class. The output passed to NLLLoss2d()(output, target) have shape batch x classes x height x width where the classes dimension contains the probability map.
My model does work with cuda and cpu which should also fix your problem however it does not converge (need to find out why but this is not necessarily relevant for you).