赞
踩
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=0∑nxi2
norm = 2.0
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)
[[0.26967996 0.5393599 0.80903983 1.0787199 1.3483998 ]]
some_nums = tf.constant([[1, 2, 3, 4, 5]], dtype=tf.float32)
print(tf.clip_by_norm(some_nums, 2.0).numpy())
[[0.26967996 0.5393599 0.80903983 1.0787199 1.3483998 ]]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。