1

I have a problem applying LayerNormalization in keras Sequential model in the following code:

from keras import Sequential
from keras.layers import Dense
import keras
import tensorflow as tf

def create_classifier(dim):
    model = Sequential()
    model.add(Dense(neurons, activation='relu', input_dim=dim, trainable=True))
    model.add(tf.keras.layers.LayerNormalization())
    model.add(Dense(int(neurons / 2), activation='relu', trainable=True))
    model.add(Dense(neurons, activation='relu',  trainable=True))
    model.add(Dense(1, activation='sigmoid', trainable=True))
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[keras.metrics.Recall()])
    model.summary()
    return model

I get this error:

TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.normalization.LayerNormalization object at 0x123b1ff90>

I wonder why it is not acceptable to mix up between keras and tensorflow (I have keras 2.3.1 and tensorflow 2.2.0). Is there a workaround for that?

Dave
  • 554
  • 1
  • 6
  • 13

1 Answers1

1

The workaround is to change

from keras import Sequential
from keras.layers import Dense

into

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

This answer provides a good explanation of why that is.

Bas Krahmer
  • 489
  • 5
  • 11