Adapt() gives error while using Normalization Layer in Sequential Models?

While using Normalization layer in Sequential Model, while adapt(), I am getting Unbound Error:

normalizer = Normalization()

normalizer.adapt(X_train)


UnboundLocalError Traceback (most recent call last)
Cell In[198], line 2
1 normalizer = Normalization()
----> 2 normalizer.adapt(X_train)

File /usr/local/lib/python3.10/site-packages/keras/src/layers/preprocessing/normalization.py:228, in Normalization.adapt(self, data)
225 input_shape = tuple(data.element_spec.shape)
227 if not self.built:
→ 228 self.build(input_shape)
229 else:
230 for d in self._keep_axis:

UnboundLocalError: local variable ‘input_shape’ referenced before assignment

I’ve only used a Normalization() layer once before. I recall it being a little bit tricky.

I am not certain, but you might need to pass X_train as part of the Normalization() layer, and then not include it in normalizer.adapt().

Hi @TMosh thanks for the response but Normalization layer doesn’t take training data as input. adapt() is only way to make it learn. But out of curiosity, if you don’t use Normalization layer, then which way do you use to normalize data in neural networks?

I’ll see if I can find my experimental notebook where I investigated how the Normalizaion() layer works.

Typically I just write my own function for normalizing the data, it’s not difficult or complicated.

@TMosh yeah sure, thank you :slight_smile: