当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。