📜  Python – tensorflow.clip_by_value()

📅  最后修改于: 2022-05-13 01:54:25.692000             🧑  作者: Mango

Python – tensorflow.clip_by_value()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

clip_by_value()用于将张量值裁剪为指定的最小值和最大值。

示例 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_vlaue(t, clip_min, clip_max)
  
# Printing the result
print('Result: ', res)


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)


输出:

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)



示例 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)

输出:

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)