當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tensorflow.clip_by_value()用法及代碼示例

TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。

clip_by_value()用於將張量值裁剪為指定的最小值和最大值。

用法:tensorflow.clip_by_value( t, clip_value_min, clip_value_max, name )

參數:

  • t:它是輸入張量。
  • clip_value_min:它定義了最小限幅值。
  • clip_value_max:它定義了最大剪輯值。
  • name(optional):它定義了操作的名稱。

返回值:



它返回裁剪的張量。

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

輸出:

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)



相關用法


注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.clip_by_value()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。