How do I clamp the magnitude of a PyTorch tensor?
I know I can use torch.clamp to clamp a tensor's values within some min / max, but how can I do this if I want to clamp by the magnitude (absolute value)? Example:

import torch
t = torch.tensor([-5.0, -250, -1, 0.003, 7, 1238])
min_mag = 1 / 10
max_mag = 100


# desired output:
tensor([  -5.0000, -100.0000,   -1.0000,    0.1000,    7.0000,  100.0000])


Here's one method:

sign = t.sign()
t = t.abs_().clamp_(min_mag, max_mag)
t *= sign


(note: this is using in-place operations)
