batch and l2 normalization example

code example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import tensorflow as tf

_1d = tf.constant([0.1, 0.3, 0.6, 0.9, 1.2, 1.5])
_2d = tf.constant([[0.1, 0.3, 0.6, 0.9, 1.2, 1.5], [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]])
_3d = tf.constant([[[0.1, 0.3, 0.6, 0.9, 1.2, 1.5], [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]],
                  [[1.1, 1.3, 1.6, 1.9, 2.2, 2.5], [1.1, 1.2, 1.3, 1.4, 1.5, 1.6]]])

batch_norm_1d = tf.nn.batch_normalization(_1d, tf.reduce_mean(_1d), tf.reduce_mean(_1d), offset=0, scale=1, variance_epsilon=.1)
batch_norm_2d = tf.nn.batch_normalization(_2d, tf.reduce_mean(_2d), tf.reduce_mean(_2d), offset=0, scale=1, variance_epsilon=.1)
batch_norm_3d = tf.nn.batch_normalization(_3d, tf.reduce_mean(_3d), tf.reduce_mean(_3d), offset=0, scale=1, variance_epsilon=.1)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    init.run()
    print('\n l2_norm 1d axes:0: \n', sess.run(tf.nn.l2_normalize(_1d, 0)))
    print('\n l2_norm 2d axes:0: \n', sess.run(tf.nn.l2_normalize(_2d, 0)))
    print('\n l2_norm 2d axes:1: \n', sess.run(tf.nn.l2_normalize(_2d, 1)))
    print('\n l2_norm 3d axes:0: \n', sess.run(tf.nn.l2_normalize(_3d, 0)))
    print('\n l2_norm 3d axes:1: \n', sess.run(tf.nn.l2_normalize(_3d, 1)))
    print('\n l2_norm 3d axes:2: \n', sess.run(tf.nn.l2_normalize(_3d, 2)))

    print('\n batch_norm 1d: \n', sess.run(batch_norm_1d))
    print('\n batch_norm 2d: \n', sess.run(batch_norm_2d))
    print('\n batch_norm 3d: \n', sess.run(batch_norm_3d))
l2_norm 1d axes:0:
[0.04490132 0.13470398 0.26940796 0.4041119  0.5388159  0.67351985]

l2_norm 2d axes:0:
[[0.70710677 0.8320503  0.8944272  0.9138115  0.9230769  0.9284767 ]
[0.70710677 0.5547002  0.4472136  0.40613848 0.38461536 0.3713907 ]]

l2_norm 2d axes:1:
[[0.04490132 0.13470398 0.26940796 0.4041119  0.5388159  0.67351985]
[0.10482849 0.20965698 0.3144855  0.41931397 0.52414244 0.628971  ]]

l2_norm 3d axes:0:
[[[0.09053575 0.22485952 0.35112342 0.42808634 0.47885215 0.51449573]
[0.09053575 0.16439898 0.22485952 0.27472115 0.31622776 0.35112345]]

[[0.9958932  0.97439116 0.93632907 0.90373784 0.8778956  0.8574928 ]
[0.9958932  0.9863939  0.97439116 0.961524   0.94868326 0.9363292 ]]]

l2_norm 3d axes:1:
[[[0.70710677 0.8320503  0.8944272  0.9138115  0.923077   0.9284766 ]
[0.70710677 0.5547002  0.4472136  0.40613848 0.3846154  0.37139064]]

[[0.7071067  0.7348034  0.77611405 0.8050558  0.8262273  0.8422714 ]
[0.7071067  0.6782801  0.63059264 0.593199   0.5633368  0.5390537 ]]]

l2_norm 3d axes:2:
[[[0.04490132 0.13470398 0.26940796 0.4041119  0.5388159  0.67351985]
[0.10482849 0.20965698 0.3144855  0.41931397 0.52414244 0.628971  ]]

[[0.24498948 0.289533   0.35634834 0.42316362 0.48997897 0.5567943 ]
[0.33001646 0.360018   0.39001945 0.42002097 0.45002246 0.48002395]]]

batch_norm 1d:
[-0.6356417  -0.44494918 -0.1589104   0.12712836  0.41316718  0.69920594]

batch_norm 2d:
[[-0.43700367 -0.24631116  0.03972763  0.32576638  0.6118052  0.89784396]
[-0.43700367 -0.3416574  -0.24631116 -0.15096489 -0.05561864  0.03972763]]

batch_norm 3d:
[[[-0.913735   -0.7230425  -0.43700367 -0.15096492  0.1350739  0.42111266]
[-0.913735   -0.8183887  -0.7230425  -0.62769616 -0.53234994  -0.43700367]]
[[ 0.03972769  0.23042011  0.516459    0.8024976   1.0885366  1.3745753 ]
[ 0.03972769  0.1350739   0.23042011  0.32576632  0.42111266  0.516459  ]]]
Manuel Cuevas

Manuel Cuevas

Hello, I'm Manuel Cuevas a Software Engineer with background in machine learning and artificial intelligence.

Recent post