Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AttributeError: 'function' object has no attribute 'compile'

I’ve been facing this attribute error. Any ideas how I can solve it please. I can share the whole code

def model(input_shape):
   model = keras.Sequential()
   model.add(keras.layers.LSTM(64, input_shape=(1,9), return_sequences=True))
   model.add(keras.layers.LSTM(64))

   model.add(keras.layers.Dense(64, activation='relu'))
   model.add(keras.layers.Dropout(0.3))
   
   model.add(keras.layers.Dense(10, activation='softmax'))

return model

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()

ttributeError Traceback (most recent call last)
Input In [67], in <cell line: 1>()
—-> 1 model.compile(loss=’binary_crossentropy’, optimizer=’adam’, metrics=[‘accuracy’])
2 model.summary()

AttributeError: ‘function’ object has no attribute ‘compile’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The function and variable have the same name, causing the issue. You can either rename the variable or the function.

def model(input_shape):
   model = keras.Sequential()
   model.add(keras.layers.LSTM(64, input_shape=(1,9), return_sequences=True))
   model.add(keras.layers.LSTM(64))

   model.add(keras.layers.Dense(64, activation='relu'))
   model.add(keras.layers.Dropout(0.3))
   
   model.add(keras.layers.Dense(10, activation='softmax'))

return model

my_model = model() # your initializer
my_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
my_model.summary()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading