1、l2_normalize函數
tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None)
解釋:這個函數的作用是利用 L2 范數對指定維度 dim 進行標准化。
比如,對於一個一維的張量,指定維度 dim = 0,那么計算結果為:
output = x / sqrt( max( sum( x ** 2 ) , epsilon ) )
假設 x 是多維度的,那么標准化只會獨立的對維度 dim 進行,不會影響到別的維度。
2、tensorflow實現
import tensorflow as tf a=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32) with tf.Session() as sess: print(sess.run(tf.nn.l2_normalize(a, [0]))) sess.close()
輸出結果:
[[ 0.26726124 0.26726124]
[ 0.53452247 0.53452247]
[ 0.80178368 0.80178368]]