Assign torch.clamp out-of-range values to zero

torch.clamp( input, min, max, out=None)

Clamp all elements in input into the range [min, max] and return a resulting tensor.

Can clamp() assign out-of-range:

value < min


value > max

to zero instead to assigning it to min and max respectively?

Possible solution:

zero_tensor = torch.zeros(input.size())
torch.where(input > min, input, zero_tensor)
torch.where(input < max, input, zero_tensor)

where() is only available in 0.4 or higher versions.

You could also do something like

tensor[tensor!=torch.clamp(tensor, min, max)] = 0