Torch.clamp() where min and max are tensors of the same shape is input tensor?

The function torch.clamp() requires min and max to be scalars. Is there a quick way to replicate the tf function clip_by_value where min and max can be tensors of the same size as the input tensor? I just want to avoid nesting a few 'for’s.


You can use 2 torch.where operations, similarly to an answer here Clipping a tensor between two corresponding tensor values. It’s not ideal in terms of performance, but it should work.

1 Like