Keep values within a valid numeric range
Preprocessing image or signal tensors
TensorFlow · Function
Clips tensor values to an inclusive minimum and maximum. It preserves the input's shape and type while replacing out-of-range elements with the relevant bound.
The function compares each value in t with the supplied lower and upper bounds. Values below the minimum become the minimum, values above the maximum become the maximum, and values already inside the interval remain unchanged; the bounds may be scalars or broadcastable tensors.
tf.clip_by_value(t, clip_value_min, clip_value_max, name=None)| Name | Type | Description |
|---|---|---|
t | Tensor or IndexedSlices | A Tensor or IndexedSlices to process. |
clip_value_min | Tensor | Lower clipping limit; scalar or broadcastable to t's shape. |
clip_value_max | Tensor | Upper clipping limit; scalar or broadcastable to t's shape. |
name | string or None | Optional operation name. |
A clipped Tensor or IndexedSlices.
import tensorflow as tf
t = tf.constant([[-10., -1., 0.], [0., 2., 10.]])
clipped = tf.clip_by_value(t, clip_value_min=-1., clip_value_max=1.)
print(clipped.numpy())
# [[-1. -1. 0.]
# [ 0. 1. 1.]]This limits every element of the tensor to the inclusive interval from -1.0 through 1.0.
Preprocessing image or signal tensors
Numerically bounded model computations
Element-wise constraints
It returns a tensor with the same type and shape as t, replacing values below the minimum or above the maximum with the corresponding bound. Values within the interval are preserved.
Follow-up: What shape can a bound have? A bound can be scalar or broadcastable to the shape of t.
clip_value_min and clip_value_max specify the lower and upper limits. Each may be a scalar tensor or a tensor broadcastable to t's shape.
Follow-up: Can the two bounds vary across tensor elements? Yes; broadcastable tensors allow element-wise or partially shared limits.
The operation accepts a Tensor or IndexedSlices as t and returns a clipped Tensor or IndexedSlices. The optional name argument gives the operation a name.
Follow-up: What type issue should you check when using numeric bounds? Do not clip an integer tensor with floating-point bounds; cast the input first.
tf.clip_by_normtf.clip_by_global_normtf.compat.v1.clip_by_valuetf.raw_ops.ClipByValue