Saturday, July 4, 2026
HomeLanguagesPython – tensorflow.clip_by_value()

Python – tensorflow.clip_by_value()

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks.

clip_by_value()  is used to clip a Tensor values to a specified min and max.

Syntax: tensorflow.clip_by_value( t, clip_value_min, clip_value_max, name )

Parameter:

  • t: It is input Tensor.
  • clip_value_min: It defines the minimum clip value.
  • clip_value_max: It defines the maximum clip value.
  • name(optional): It defines the name for the operation.

Returns:

It returns a clipped Tensor.

Example 1:

Python3




# Importing the library
import tensorflow as tf
 
# Initializing the input tensor
t = tf.constant([1, 2, 3, 4], dtype = tf.float64)
clip_value_min = 2
clip_value_max = 5
 
# Printing the input tensor
print('t: ', t)
print('clip_min: ', clip_value_min)
print('clip_max: ', clip_value_max)
 
# Calculating result
res = tf.clip_by_value(t, clip_min, clip_max)
 
# Printing the result
print('Result: ', res)


Output:

t:  tf.Tensor([1. 2. 3. 4.], shape=(4, ), dtype=float64)
clip_min:  2
clip_max:  5
Result:  tf.Tensor([2. 2. 3. 4.], shape=(4, ), dtype=float64)

Example 2:

Python3




# Importing the library
import tensorflow as tf
 
# Initializing the input tensor
t = tf.constant([[1, 2], [ 3, 4]], dtype = tf.float64)
clip_value_min = [2, 3]
clip_value_max = [5, 7]
 
# Printing the input tensor
print('t: ', t)
print('clip_min: ', clip_value_min)
print('clip_max: ', clip_value_max)
 
# Calculating result
res = tf.clip_by_value(t, clip_value_min, clip_value_max)
 
# Printing the result
print('Result: ', res)


Output:

t:  tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float64)
clip_min:  [2, 3]
clip_max:  [5, 7]
Result:  tf.Tensor(
[[2. 3.]
 [3. 4.]], shape=(2, 2), dtype=float64)
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12015 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6967 POSTS0 COMMENTS