torch.nn.SmoothL1Loss returns only average/sum not individual losses

I am trying to use torch.nn.SmoothL1Loss to calculate the loss for a batch. In older versions, I could use parameter reduce to return one value per element. In version 0.2.0.post3, however, the only parameter of SmoothL1Loss is size_average. It can only be used to return sum of the losses or the average loss. How can I make this class return one loss per batch element?

The reduce keyword was implemented after 0.2. To use it, you should build from source or wait for the upcoming 0.3 release.

Alternatively, you can write your own SmoothL1Loss in Python. It’s not too bad, you can try something like this: https://github.com/zou3519/pytorch/blob/ff4612605e0c311c1f6ad2d6a67a298587ea8eae/test/common_nn.py#L299