NoLand'sMan
NoLand'sMan

Reputation: 574

keras "You must compile your model before using it." even after compilation

I am using the following code to fit my LSTM network to a time series generator:

data_gen = TimeseriesGenerator(x_train,y_train,length=10, sampling_rate=2,batch_size=5110)

def rmsle_K(y, y0):
    return K.sqrt(K.mean(K.square(tf.log1p(y) - tf.log1p(y0))))

regressor = Sequential()
regressor.add(Bidirectional(LSTM(units=100,return_sequences= True),input_shape=(x_train.shape[1],12)))
regressor.add(Dropout(0.1))
regressor.add(Bidirectional(LSTM(units=100,return_sequences= True)))
regressor.add(Dropout(0.1))
regressor.add(Bidirectional(LSTM(units=100,return_sequences= True)))
regressor.add(Dropout(0.1))
regressor.add(Dense(units=1))
regressor.compile(loss=keras.losses.mean_squared_logarithmic_error, optimizer='adam', metrics=[rmsle_K])

regressor.fit_generator(data_gen)

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

x_train.shape = (340, 5110, 12).y_train.shape = (3400, 511, 1) How can I fix this error? I feel that I am messsing up in the input and output dimensions, but I am not sure.

Upvotes: 0

Views: 357

Answers (1)

ArunJose
ArunJose

Reputation: 2174

The issue with your code is that you are misplacing the input shape so your model is not compiling right. Even with this I dont think your input shape is correct.

You should use

regressor.add(Bidirectional(LSTM(units=100,return_sequences= True),input_shape=(x_train.shape[1],1)))

instead of

regressor.add(Bidirectional(LSTM(units=100,return_sequences= True,input_shape=(x_train.shape[1],1))))

This would solve your current error as the model will get compiled. And setting the right input shape is a topic that should be discussed separately.

Hope this helps

Upvotes: 2

Related Questions