Search code examples
tensorflowmachine-learningkeraspython-3.7tensorboard

AttributeError: 'Sequential' object has no attribute 'run_eagerly'


I'm trying to try using this model to train on rock, paper, scissor pictures. However, it was trained on 1800 pictures and only has an accuracy of 30-40%. I was then trying to use TensorBoard to see whats going on, but the error in the title appears.

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from tensorflow.python.keras.callbacks import TensorBoard

model = Sequential()
model.add(Conv2D(256, kernel_size=(4, 4),
            activation='relu',
            input_shape=(64,64,3)))
model.add(Conv2D(196, (4, 4), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(196, (4, 4), activation='relu'))
model.add(Conv2D(196, (4, 4), activation='relu'))
model.add(Dropout(0.25))

model.add(Conv2D(128, (4, 4), activation='relu'))
model.add(Conv2D(128, (4, 4), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(96, (4, 4), activation='relu'))
model.add(Conv2D(96, (4, 4), activation='relu'))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(3, activation='softmax'))

''' here it instantiates the tensorboard '''
tensorboard = TensorBoard(log_dir="C:/Users/bamla/Desktop/RPS project/Logs")

model.compile(loss="sparse_categorical_crossentropy",
        optimizer="SGD",
        metrics=['accuracy'])

model.summary()

''' Here its fitting the model '''
model.fit(x_train, y_train, batch_size=50, epochs = 3, callbacks= 
[tensorboard])

This outputs:

Traceback (most recent call last):

File "c:/Users/bamla/Desktop/RPS project/Testing.py", line 82, in <module>
model.fit(x_train, y_train, batch_size=50, epochs = 3, callbacks= 
[tensorboard])

File "C:\Users\bamla\AppData\Local\Programs\Python\Python37\lib\site- 
packages\keras\engine\training.py", line 1178, in fit
validation_freq=validation_freq)

File "C:\Users\bamla\AppData\Local\Programs\Python\Python37\lib\site- 
 packages\keras\engine\training_arrays.py", line 125, in fit_loop
callbacks.set_model(callback_model)

 File "C:\Users\bamla\AppData\Local\Programs\Python\Python37\lib\site- 
packages\keras\callbacks.py", line 68, in set_model
callback.set_model(model)

File "C:\Users\bamla\AppData\Local\Programs\Python\Python37\lib\site- 
packages\tensorflow\python\keras\callbacks.py", line 1509, in set_model
if not model.run_eagerly:

AttributeError: 'Sequential' object has no attribute 'run_eagerly'

Also, if you have any tips on how to improve the accuracy it would be appreciated!


Solution

  • The problem is here:

    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Flatten
    from keras.layers import Conv2D, MaxPooling2D
    from tensorflow.python.keras.callbacks import TensorBoard
    

    Do not mix keras and tf.keras imports, these are not compatible with each other, and produce weird errors as the ones you are seeing.