Solved by ptrblck in post #4 
              
              
                I’ve implemented a weighted pixelwise NLLLoss a while ago in this topic. The code is quite old (e.g. don’t use Variables anymore), but might be a good starter for your custom loss.