Python – tensorflow.clip_by_value()
Last Updated :
15 Mar, 2023
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
import tensorflow as tf
t = tf.constant([ 1 , 2 , 3 , 4 ], dtype = tf.float64)
clip_value_min = 2
clip_value_max = 5
print ( 't: ' , t)
print ( 'clip_min: ' , clip_value_min)
print ( 'clip_max: ' , clip_value_max)
res = tf.clip_by_value(t, clip_min, clip_max)
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
import tensorflow as tf
t = tf.constant([[ 1 , 2 ], [ 3 , 4 ]], dtype = tf.float64)
clip_value_min = [ 2 , 3 ]
clip_value_max = [ 5 , 7 ]
print ( 't: ' , t)
print ( 'clip_min: ' , clip_value_min)
print ( 'clip_max: ' , clip_value_max)
res = tf.clip_by_value(t, clip_value_min, clip_value_max)
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)
Share your thoughts in the comments
Please Login to comment...