Keras: “must compile model before using it” despite compile() is used

前端 未结 4 502
遥遥无期
遥遥无期 2021-01-12 08:15

I want to create and train a CNN model in Keras for classification of banknotes. Creating models works fine with simple tutorials but not with the architecture I adopt from

相关标签:
4条回答
  • 2021-01-12 08:30

    If ever someone ends up with the same error code here is maybe a way to fix it. So I was using a generator and was getting the "Must compile" error even tho everything was fine. I was able to fix it by doing a model.fit(x,y) on a single batch before launching my fit_generator and everything worked fine after that. I don't know if this helps anyone but yeah!

    0 讨论(0)
  • 2021-01-12 08:30

    Try This:

    from keras.optimizers import Adam
    opt = keras.optimizers.Adam(use your own learning rate)
    model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=['accuracy']))
    
    0 讨论(0)
  • 2021-01-12 08:33

    You can run evaluate on a small set and that would fix it.

    0 讨论(0)
  • 2021-01-12 08:40

    Found my mistake - explanation for future reference.

    The error origniates back in compile() where the first if-statement says:

    if not self.built:
        # Model is not compilable because
        # it does not know its number of inputs
        # and outputs, nor their shapes and names.
        # We will compile after the first
        # time the model gets called on training data.
    return
    

    So I specified input_shape= and input_format=in the first Conv2D layer and everything works fine.

    0 讨论(0)
提交回复
热议问题