Path: tensorflow/tensorflow/python/ops/clip_ops.py
@tf_export("clip_by_value")
@dispatch.register_unary_elementwise_api
@dispatch.add_dispatch_support
def clip_by_value(t, clip_value_min, clip_value_max,
name=None):
with ops.name_scope(name, "clip_by_value",
[t, clip_value_min, clip_value_max]) as name:
values = ops.convert_to_tensor(
t.values if isinstance(t, indexed_slices.IndexedSlices) else t,
name="t")
# Go through list of tensors, for each value in each tensor clip
t_min = math_ops.minimum(values, clip_value_max)
# Assert that the shape is compatible with the initial shape,
# to prevent unintentional broadcasting.
values.shape.assert_is_compatible_with(t_min.shape)
t_max = math_ops.maximum(t_min, clip_value_min, name=name)
values.shape.assert_is_compatible_with(t_max.shape)
if isinstance(t, indexed_slices.IndexedSlices):
t_max = indexed_slices.IndexedSlices(t_max, t.indices, t.dense_shape)
return t_max
# TODO(scottzhu): switch to use new implementation in 2 weeks.
# return gen_math_ops.clip_by_value(
# t, clip_value_min, clip_value_max, name=name)