If all you want is to limit the values to be in a range, why not using the torch.clamp?
I just want to make sure you use the right tools here, tanh is an activation function, and it transforms your input to be in that range, depending on the task it might not give you what you desire.
So if you know what you are doing then fine, in case you didn’t know, check torch.clamp out, maybe this is what you were looking for?