Search code examples
pythontensorflowkerasdeep-learningsequential

Keras Sequential Compile Function TypeError : compile() missing 1 required positional argument: 'self'


This is my Keras model

model = keras.Sequential
(
    [
        Dense(2, activation="relu", name="L1"),
        Dense(3, activation="relu", name="L2"),
        Dense(4,name="L3")
    ]
)

and in the next line i use compile() function on my model

model.compile(optimizer="sgd", loss="mse", metrics=[MeanSquaredError()])

but i get this TypeError when i run it

TypeError: compile() missing 1 required positional argument: 'self'

Solution

  • first of all you need to understand how many input and outputs you have. for example you want to detect the 5 class than your last layer should have 5 as dense attributes. so add this as model

    model=tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape=(224,224,3)),
        tf.keras.layers.MaxPool2D(2,2),
        #1
        tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
        tf.keras.layers.MaxPool2D(2,2),
        #2
        tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
        tf.keras.layers.MaxPool2D(2,2),
        #
        #
        tf.keras.layers.Flatten(),
        #
        tf.keras.layers.Dense(512,activation='relu'),
        #
        tf.keras.layers.Dense(4,activation='softmax')
    
        ]
    )
    

    than compile your model

    model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.01),
                  loss='categorical_crossentropy',metrics=['accuracy'])
    
    

    this will work.