def clip_(in_tensor, max_or_min, min_or_max):
    """limit the values in in_tensor to be within [min,max].
    values larger than max will be cut to max, respectively for mins.
    the order of max/min doesn't matter.
    the operation is not in-place, that saves you alot troubles.
    Notice: this clip is not inplace.
            But, for clipped values, the gradients will always be passed backwards.
            This is useful when you want `clip` as a value-normalizer, but don not want the gradient to vanish.
    """
    minv, maxv = sorted((max_or_min, min_or_max))
    greater_pos = (in_tensor.detach() > maxv).type_as(in_tensor)
    diff = (maxv - in_tensor.detach()) * greater_pos
    in_tensor = in_tensor + diff
    less_pos = (in_tensor.detach() < minv).type_as(in_tensor)
    diff = (minv - in_tensor.detach()) * less_pos
    in_tensor = in_tensor + diff
    return in_tensor
