I am trying to get better intuition for what is happening and how to use the axis parameter in the Normalization layer and I think I understood it but I am not sure why the programs giving errors for the above code.
The axis parameter is basically selecting the axis in which normalization happens, which means with axis = -1(for below data structure in code) it is normalizing the columns which in Machine Learning represents normalizing along all the samples of individual features/inputs.
From the error what I understood is that it is not liking the shape of the matrix it needs to normalize, right? It wants 2-D maybe but I dont get why it is not getting 2-D matrix or is it something else.
What I expected to happen was that it will normalize each row for example if first row is [2 4] then it will become [0 1]
Input = np.random.choice([1, 2, 3, 4, 5], p=[0.1, 0.35, 0.25, 0.1, 0.2], size=(20, 2))
Observed = np.random.choice([0, 1], size=(20, 1))
norm_In = tf.keras.layers.Normalization(axis=-1)
norm_In_ax = tf.keras.layers.Normalization(axis=0)
norm_In.adapt(Input) #learns mean and variance
norm_In_ax.adapt(Input)
norm_Input = norm_In(Input)
norm_Input_ax = norm_In_ax(Input)
print(norm_Input[0:5,:])
print(norm_Input_ax[0:5,:])
Error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[51], line 5
2 norm_In_ax = tf.keras.layers.Normalization(axis=0)
4 norm_In.adapt(Input) #learns mean and variance
----> 5 norm_In_ax.adapt(Input)
7 norm_Input = norm_In(Input)
8 norm_Input_ax = norm_In_ax(Input)
File c:\Users\tinnt\Documents\Learning\Machine Learning Specialization\Supervised Machine Learning Regression and Classification\.venv\Lib\site-packages\keras\src\layers\preprocessing\normalization.py:287, in Normalization.adapt(self, data, batch_size, steps)
241 def adapt(self, data, batch_size=None, steps=None):
242 """Computes the mean and variance of values in a dataset.
243
244 Calling `adapt()` on a `Normalization` layer is an alternative to
(...)
285 argument is not supported with array inputs.
286 """
--> 287 super().adapt(data, batch_size=batch_size, steps=steps)
File c:\Users\tinnt\Documents\Learning\Machine Learning Specialization\Supervised Machine Learning Regression and Classification\.venv\Lib\site-packages\keras\src\engine\base_preprocessing_layer.py:258, in PreprocessingLayer.adapt(self, data, batch_size, steps)
256 with data_handler.catch_stop_iteration():
257 for _ in data_handler.steps():
--> 258 self._adapt_function(iterator)
259 if data_handler.should_sync:
260 context.async_wait()
...
File "c:\Users\tinnt\Documents\Learning\Machine Learning Specialization\Supervised Machine Learning Regression and Classification\.venv\Lib\site-packages\keras\src\layers\preprocessing\normalization.py", line 188, in build
raise ValueError(
ValueError: All `axis` values to be kept must have known shape. Got axis: (0,), input shape: [None, 2], with unknown axis at index: 0