How to apply a element wise cross_entropy/nll loss to a 3D matrix?

The answer has been provided here.