Error “You must compile your model before using it” in case of LSTM and fit_generator in Keras

前端 未结 1 2137
再見小時候
再見小時候 2021-02-20 11:59

I create my own class which create a Keras model inside one of its methods.

self.model = Sequential()
self.model.add(LSTM(32))
self.model.add(Dense(2, activation         


        
相关标签:
1条回答
  • 2021-02-20 12:46

    It seems the first Keras LSTM layer still requires an input_shape when using fit_generator which seems to be missing in the Keras documentation and results in the "You must compile your model before using it" error.

    To solve make sure you have an input_shape parameter in your first LSTM layer as shown by the example below:

    model.add(LSTM(100, input_shape=(n_timesteps, n_dimensions), return_sequences=True))
    model.add(Dropout(0.2))
    model.add(LSTM(100, return_sequences=False))
    model.add(Dropout(0.2))
    model.add(Dense(10, activation='tanh'))
    
    model.compile(loss='mse', optimizer='adam')
    
    0 讨论(0)
提交回复
热议问题