📜  Python – tensorflow.clip_by_global_norm()

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

Python – tensorflow.clip_by_global_norm()

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

clip_by_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:  ([, ], )