tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None)
上式:
x为输入的向量;
dim为l2范化的维数,dim取值为0或0或1;取0为按列范化,取1为按行范化,也可以取其他数值
epsilon的范化的最小值边界;
举例:
import tensorflow as tf
input_data = tf.constant([[1.0,2,3],[4.0,5,6],[7.0,8,9]])
output = tf.nn.l2_normalize(input_data, dim = 0)
with tf.Session() as sess:
print sess.run(output)
输出结果为:
若dim=1则输出:
[[0.26726124 0.5345225 0.8017837 ]
[0.45584232 0.5698029 0.6837635 ]
[0.5025707 0.5743665 0.64616233]]
举例二:
import tensorflow as tf
x=tf.constant([[[[1.,2.,3],[4.,5.,6.],[4.,5.,6.]],
[[1.,2.,3],[4.,5.,6.],[4.,5.,6.]],
[[1.,2.,3],[4.,5.,6.],[4.,5.,6.]]]])
l2=tf.nn.l2_normalize(x, [3],epsilon=1e-12)
with tf.Session() as sess:
print(sess.run(l2))
输出结果:
[[[[0.26726124 0.5345225 0.8017837 ]
[0.45584232 0.5698029 0.6837635 ]
[0.45584232 0.5698029 0.6837635 ]]
[[0.26726124 0.5345225 0.8017837 ]
[0.45584232 0.5698029 0.6837635 ]
[0.45584232 0.5698029 0.6837635 ]]
[[0.26726124 0.5345225 0.8017837 ]
[0.45584232 0.5698029 0.6837635 ]
[0.45584232 0.5698029 0.6837635 ]]]]