Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras Bidirectional "RuntimeError: You must compile your model before using it." after compilation completed

I'm trying to create a small Bidirectional recurrent NN. The model itself compiles without error, but when trying to fit the model I get the error stating I should compile first. Please see the code snippet below:

# fourth recurrent model, bidirectional
bidirectional_recurrent = Sequential()
bidirectional_recurrent.add(Bidirectional(GRU(32, input_shape=(int(lookback/steps), data_scaled.shape[-1]))))
bidirectional_recurrent.add(Dense(1))

bidirectional_recurrent.compile(optimizer='rmsprop', loss='mae')

bidirectional_recurrent_history = bidirectional_recurrent.fit_generator(train_gen, steps_per_epoch=500, epochs=40,
                                               validation_data=val_gen, validation_steps=val_steps)

RuntimeError: You must compile your model before using it.

I've used the same setup to train unidirectional RNN's which worked fine. Any tips to help solve the run-time error are appreciated. (restarting the kernel did not help)
Maybe I did not instantiate 'Bidirectional' correctly?

Please note: This question is different from Do I need to compile before 'X' type of questions
Note2: R examples of the same code can be found here

like image 691
Koen Avatar asked Mar 05 '23 23:03

Koen


1 Answers

Found it,
When using Bidirectional, it should be treated as a layer, shifting the input_shape to be contained in Bidirectional() instead of in the GRU() object solved the problem

so

bidirectional_recurrent.add(Bidirectional(GRU(32, input_shape=(int(lookback/steps),
                            data_scaled.shape[-1]))))

becomes

bidirectional_recurrent.add(Bidirectional(GRU(32), input_shape=(int(lookback/steps),
                            data_scaled.shape[-1])))
like image 196
Koen Avatar answered May 11 '23 14:05

Koen