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


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

<p> <a href=”https://www.geeksforgeeks.org/introduction-to-tensorflow/”> TensorFlow </a>是Google設計的開源python庫,用於開發機器學習模型和深度學習神經網絡。

clip_by_global_norm()用於按範數之和與它們的範數之和來裁剪多個張量的值。

Syntax: tensorflow.clip_by_global_norm( t_list, clip_norm, use_norm, name)

參數:

  • t_list:It is tuple or list of mixed Tensors, IndexedSlices.
  • clip_norm:It is 0-D scalar tensor. It defines the clipping ratio and must be greater than 0.
  • use_norm(optional):It is 0-D scalar tensor. It defines the norm to be used. If none is passed global_norm() is used to compute the norm.
  • name(optional):It defines the name for the operation.

返回:



  • list_clipped:It is list of clipped tensor of same type as t_list.
  • global_norm:It is 0-D tensor which represent the global_norm.

範例1:

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
t_list = [tf.constant([1, 2, 3, 4], dtype = tf.float64), tf.constant([5, 6, 7, 8], dtype = tf.float64)] 
clip_norm = .8
use_norm = tf.constant(1.0, dtype = tf.float64) 
  
# Printing the input tensor 
print('t_lis:', t_list) 
print('clip_norm:', clip_norm) 
print('use_norm:', use_norm) 
  
# Calculating tangent 
res = tf.clip_by_global_norm(t_list, clip_norm, use_norm) 
  
# Printing the result 
print('Result:', res)

輸出:

t_lis: [<tf.Tensor:shape=(4, ), dtype=float64, numpy=array([1., 2., 3., 4.])>, <tf.Tensor:shape=(4, ), dtype=float64, numpy=array([5., 6., 7., 8.])>]
clip_norm: 0.8
use_norm: tf.Tensor(1.0, shape=(), dtype=float64)
Result: ([<tf.Tensor:shape=(4, ), dtype=float64, numpy=array([0.8, 1.6, 2.4, 3.2])>, <tf.Tensor:shape=(4, ), dtype=float64, numpy=array([4., 4.8, 5.6, 6.4])>], <tf.Tensor:shape=(), dtype=float64, numpy=1.0>)




範例2:在此示例中,沒有任何內容傳遞給use_norm,因此將使用global_norm()查找規範。

Python3

# Importing the library 
import tensorflow as tf 
  
# Initializing the input tensor 
t_list = [tf.constant([1, 2, 3, 4], dtype = tf.float64), tf.constant([5, 6, 7, 8], dtype = tf.float64)] 
clip_norm = .8
  
# Printing the input tensor 
print('t_lis:', t_list) 
print('clip_norm:', clip_norm) 
  
# Calculating tangent 
res = tf.clip_by_global_norm(t_list, clip_norm) 
  
# Printing the result 
print('Result:', res)

輸出:

t_lis: [<tf.Tensor:shape=(4, ), dtype=float64, numpy=array([1., 2., 3., 4.])>, <tf.Tensor:shape=(4, ), dtype=float64, numpy=array([5., 6., 7., 8.])>]
clip_norm: 0.8
Result: ([<tf.Tensor:shape=(4, ), dtype=float64, numpy=array([0.0560112, 0.11202241, 0.16803361, 0.22404481])>, <tf.Tensor:shape=(4, ), dtype=float64, numpy=array([0.28005602, 0.33606722, 0.39207842, 0.44808963])>], <tf.Tensor:shape=(), dtype=float64, numpy=14.2828568570857>)





相關用法


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