Solved by eqy in post #2 
              
              
                Calling mean should just work, although it seems surprising that the loss function cannot work on a batch of data. There might be some slowdown because of for loop overhead rather than computing the loss in a batched way.