Python – tensorflow.clip_by_global_norm()
TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络.
clip_by_global_norm()用于通过它们的范数之和的比率来裁剪多个张量的值。
Syntax: tensorflow.clip_by_global_norm( t_list, clip_norm, use_norm, name)
Parameters:
- 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.
Returns:
- 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)
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: [, ]
clip_norm: 0.8
use_norm: tf.Tensor(1.0, shape=(), dtype=float64)
Result: ([, ], )
示例 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: [, ]
clip_norm: 0.8
Result: ([, ], )