The native PyTorch loss functions, such as nn.CrossEntropyLoss, allow you to use reduction='none', which will calculate the loss for each sample/pixel etc. without calculating the sum or mean (default) of it. The result would thus not be a scalar value but a tensor which you could reduce afterwards (e.g. by calling mean on it). This allows for more flexibility as you would have the chance to add an elementwise weighting etc. before the final reduction.