Torch cut range [0,1]

Hello all. my prediction tensor (output of a CNN size of BxCxHxW) has to in range [0,1]. However, the prediction tensor sometime in the range smaller than 0 or bigger than 1. Do we have any torch function to cutting the negative value and bigger than one, so that the range of tensor will be in [0,1]

if x< 0 --> x=0
if x>1 -->x=1

This is my solution

x = torch.clamp(x, min=0, max=1)

clamp or sigmoid should work depending if you want a hard “cut” or want to use a smooth function.

1 Like