Clipping a tensor between two corresponding tensor values

Hi
I have ywo tensors T and I with the same size and a scalar a, and I want to clip each item in T between the corresponding I-a and I+a. in numpy it is doable using np.clip(T,I-a,I+a), whereas when I use torch.clamp(T,I-a,I+a) it raises an error “argument ‘min’ (position 2) must be Number, not Tensor”

Maybe try something like this:

T = torch.where(T > I + a, I + a, T)
T = torch.where(T < I - a, I - a, T)

https://pytorch.org/docs/stable/torch.html#torch.where

5 Likes

Thanks a lot, it solved my problem!