Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.clip_by_value

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.

How it works

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.

Syntax

tf.clip_by_value(t, clip_value_min, clip_value_max, name=None)

Parameters

NameTypeDescription
tTensor or IndexedSlicesA Tensor or IndexedSlices to process.
clip_value_minTensorLower clipping limit; scalar or broadcastable to t's shape.
clip_value_maxTensorUpper clipping limit; scalar or broadcastable to t's shape.
namestring or NoneOptional operation name.

Returns

A clipped Tensor or IndexedSlices.

Key points

  • Use this function to constrain tensor values to a defined lower and upper range.
  • The result preserves the input tensor's type and shape.
  • Elements below the lower bound are replaced by that lower bound.
  • Elements above the upper bound are replaced by that upper bound.
  • Bounds may match the input shape exactly or broadcast across it.

Common mistakes

  • Ensure the lower bound is no greater than the upper bound; reversing them does not produce correct results.
  • Keep the tensor and clipping bounds type-compatible; cast an integer tensor to floating point before using float bounds.

Example: Clip tensor values to a range

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.

When to use it

Keep values within a valid numeric range

Preprocessing image or signal tensors

Limit activations or intermediate values

Numerically bounded model computations

Apply broadcastable lower and upper limits

Element-wise constraints

Interview questions

QWhat does tf.clip_by_value do?

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.

QWhat forms can the clipping bounds take?

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.

QWhat are the accepted input and output types?

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_norm
  • tf.clip_by_global_norm
  • tf.compat.v1.clip_by_value
  • tf.raw_ops.ClipByValue

Source: tf.clip_by_value | TensorFlow v2.16.1. Checked 2026-10-15.