Linked Questions

Popular Questions

I'm trying to create a model and fit it in Keras, but I'm getting a message saying I need to compile it, despite having called compile() with no error

Most of this code is from online tutorials, and I've tweaked it a bit to suit my setup (folders of images), but can't seem to debug this.

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
        'data/train',
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
        'data/test',
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')
model = Sequential()
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fine-tune the model
model.fit_generator(
        train_generator,
        steps_per_epoch=50,
        epochs=50,
        validation_data=validation_generator,
        validation_steps=50)

Expected results would be no error, but I get:

"RuntimeError: You must compile your model before using it."

Related Questions