Solved by pascal_notsawo in post #2 
              
              
                One solution, inspired from How to efficiently normalize a batch of tensor to [0, 1], is as follows. 
import torch
batch_size, height, width = 2, 2, 2

torch.manual_seed(0)
loss = torch.randn((batch_size, height, width))
print(loss)

# loss.size(0) or batch_size
loss = loss.view(loss.size(0), -1) # …