为什么Keras fit_generator会引发此“模型需要编译”错误?

时间:2019-02-04 04:35:46

标签: keras

我正在尝试创建一个模型并将其适合Keras,但我收到一条消息,要求我编译它,尽管调用了compile()却没有错误

大部分代码来自在线教程,我对其进行了一些调整以适合我的设置(图像文件夹),但似乎无法对其进行调试。

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)

预期结果没有错误,但是我得到了:

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

0 个答案:

没有答案