_clip_by_value
clip_by_value
¶
Clip a tensor such that min_value
<= tensor <= max_value
.
Given an interval, values outside the interval are clipped. If min_value
or max_value
is not provided then
clipping is not performed on lower or upper interval edge respectively.
This method can be used with Numpy data:
n = np.array([-5, 4, 2, 0, 9, -2])
b = fe.backend.clip_by_value(n, min_value=-2, max_value=3) # [-2, 3, 2, 0, 3, -2]
b = fe.backend.clip_by_value(n, min_value=-2) # [-2, 4, 2, 0, 9, -2]
This method can be used with TensorFlow tensors:
t = tf.constant([-5, 4, 2, 0, 9, -2])
b = fe.backend.clip_by_value(t, min_value=-2, max_value=3) # [-2, 3, 2, 0, 3, -2]
b = fe.backend.clip_by_value(t, min_value=-2) # [-2, 4, 2, 0, 9, -2]
This method can be used with PyTorch tensors:
p = torch.tensor([-5, 4, 2, 0, 9, -2])
b = fe.backend.clip_by_value(p, min_value=-2, max_value=3) # [-2, 3, 2, 0, 3, -2]
b = fe.backend.clip_by_value(p, min_value=-2) # [-2, 4, 2, 0, 9, -2]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
Tensor
|
The input value. |
required |
min_value |
Union[int, float, Tensor, None]
|
The minimum value to clip to. |
None
|
max_value |
Union[int, float, Tensor, None]
|
The maximum value to clip to. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
The |
Raises:
Type | Description |
---|---|
ValueError
|
If |