当前位置:   article > 正文

tf.clip_norm_clipnorm

clipnorm

t= [ x 1 x_{1} x1, x 2 x_{2} x2,… x 2 x_{2} x2]

l2= ∑ i = 0 n x i 2 \sqrt{\displaystyle\sum\limits_{i=0}^n x_i^2} i=0nxi2
norm = 2.0

clip_norm = t ∗ n o r m l 2 =\frac{t*norm}{l2} =l2tnorm

import numpy as np

t=np.array([[1, 2, 3, 4, 5]])
l2norm4t = np.linalg.norm(t)
clip_norm = 2.0
transformed_t = t *clip_norm/l2norm4t
print(transformed_t)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
[[0.26967996 0.5393599  0.80903983 1.0787199  1.3483998 ]]
  • 1
some_nums = tf.constant([[1, 2, 3, 4, 5]], dtype=tf.float32)
print(tf.clip_by_norm(some_nums, 2.0).numpy())

  • 1
  • 2
  • 3
[[0.26967996 0.5393599  0.80903983 1.0787199  1.3483998 ]]
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/311554
推荐阅读
相关标签
  

闽ICP备14008679号