# tensorflow--神经网络--5--tf.nn.l2_normalize标准化数据

tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None)

• x为输入的向量；
• dim为l2范化的维数，dim取值为0或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(input_data)
print sess.run(output)


[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
[[0.12309149 0.20739034 0.26726127]
[0.49236596 0.51847583 0.53452253]
[0.86164045 0.82956135 0.80178374]]


dim = 0, 为按列进行l2范化

$$norm(1)= \sqrt{1^{2}+4^{2}+7^{2}} = \sqrt{66} \\ norm(2)= \sqrt{2^{2} +5^{2}+8^{2} }= \sqrt{93} \\ norm(3) = \sqrt{ 3^{2} + 6^{2} + 9^{2}} = \sqrt{126}$$

[[1./norm(1), 2./norm(2) , 3./norm(3) ]
[4./norm(1) , 5./norm(2) , 6./norm(3) ]    =
[7./norm(1) , 8./norm(2) , 9./norm(3) ]]

[[0.12309149 0.20739034 0.26726127]
[0.49236596 0.51847583 0.53452253]
[0.86164045 0.82956135 0.80178374]]


## 二、按行计算

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 = 1)
with tf.Session() as sess:
print sess.run(input_data)
print sess.run(output)


[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
[[0.26726124 0.5345225 0.8017837 ]
[0.45584232 0.5698029 0.6837635 ]
[0.5025707 0.5743665 0.64616233]]


dim = 1, 为按行进行l2范化

$$norm(1)= \sqrt{1^{2}+4^{2}+7^{3}} = \sqrt{14} \\ norm(2)= \sqrt{4^{2} +5^{2}+6^{2} }= \sqrt{77} \\ norm(3) = \sqrt{ 7^{2} + 8^{2} + 9^{2}} = \sqrt{194}$$

[[1./norm(1), 2./norm(1) , 3./norm(1) ]
[4./norm(2) , 5./norm(2) , 6./norm(2) ]    =
[7./norm(3) , 8..norm(3) , 9./norm(3) ]]

[[0.12309149 0.20739034 0.26726127]
[0.49236596 0.51847583 0.53452253]
[0.86164045 0.82956135 0.80178374]]