Using pytorch reduction in custom loss

Hi, I am trying to write custom loss function by creating a nn.module subclass with forward method.
Is it possible to use pytorch reduction mode handling in my loss?
Sure I can do:

if reduction == 'mean':
  return torch.mean(my_loss_value)
elif ...

But I would rather just call something like:

return torch_reduction_handling_function(my_loss_value, reduction=reduction)

However, I don’t know where to find this reduction handling function and if it is exposed to me at all.

Thanks for your help!

If I understand the use case correctly, you are looking for a generic “reduction” method, which would take the desired reduction type as an argument?
If so, then unfortunately PyTorch doesn’t provide this generic method.

You could pass a reduction argument to the functional calls of some criteria, e.g. F.mse_loss(..., reduction=reduction), but this would of course only work for this particular criterion.

ok, then I will have to re-implement. Thanks!