I spent a lot of time trying to understand how the axis argument works in the keras.utils.normalization() function. Can someone please explain it to me by using the concept of np.array by making a random (2,2) np array and explain how the normalization actually works for different axes.
Asked
Active
Viewed 212 times
1 Answers
0
Let's consider a 2*2
Numpy Array
,
x = np.array([[1,2],
[3,4]])
Axis = 0
indicates that the Operation is done Row-wise
. Code with Axis = 0
:
x_norm_rows_axis = tf.keras.utils.normalize(x, axis= 0)
print(x_norm_rows_axis)
Output of the above code is:
[[0.31622777 0.4472136 ]
[0.9486833 0.89442719]]
The output of Axis = 0
can be elaborated as shown below:
print('x_norm_rows_axis[0][0] = {}'.format(1/np.sqrt(1 ** 2 + 3 ** 2)))
print('x_norm_rows_axis[0][1] = {}'.format(2/np.sqrt(2 ** 2 + 4 ** 2)))
print('x_norm_rows_axis[1][0] = {}'.format(3/np.sqrt(1 ** 2 + 3 ** 2)))
print('x_norm_rows_axis[1][1] = {}'.format(4/np.sqrt(2 ** 2 + 4 ** 2)))
Output of the above Print
Statements is shown below:
x_norm_rows_axis[0][0] = 0.31622776601683794
x_norm_rows_axis[0][1] = 0.4472135954999579
x_norm_rows_axis[1][0] = 0.9486832980505138
x_norm_rows_axis[1][1] = 0.8944271909999159
Axis = 1
indicates that the Operation is done Column-wise
. Code with axis = 1
. In this case, since we have only 2 Dimensions, we can consider this as axis = -1
as well:
x_norm_col_axis = tf.keras.utils.normalize(x, axis= 1)
print(x_norm_col_axis)
Output of the above code is:
[[0.4472136 0.89442719]
[0.6 0.8 ]]
The output of axis = 1
or axis = -1
(in this case) can be elaborated as shown below:
print('x_norm_col_axis[0][0] = {}'.format(1/np.sqrt(1 ** 2 + 2 ** 2)))
print('x_norm_col_axis[0][1] = {}'.format(2/np.sqrt(2 ** 2 + 1 ** 2)))
print('x_norm_col_axis[1][0] = {}'.format(3/np.sqrt(4 ** 2 + 3 ** 2)))
print('x_norm_col_axis[1][1] = {}'.format(4/np.sqrt(3 ** 2 + 4 ** 2)))
Output of the above Print
Statements is shown below:
x_norm_col_axis[0][0] = 0.4472135954999579
x_norm_col_axis[0][1] = 0.8944271909999159
x_norm_col_axis[1][0] = 0.6
x_norm_col_axis[1][1] = 0.8
To understand how the Order
argument works, refer this Stack Overflow Answer.
Hope this helps. Happy Learning!
-
Thank you so much ! That was a perfect explanation . Thanks again :D – Jun 13 '20 at 14:24