Keras双向"RuntimeError:您必须在使用模型之前对其进行编译."编译完成后 [英] Keras Bidirectional "RuntimeError: You must compile your model before using it." after compilation completed

查看:1192
本文介绍了Keras双向"RuntimeError:您必须在使用模型之前对其进行编译."编译完成后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个小的双向递归神经网络.模型本身编译时没有错误,但是在尝试拟合模型时出现错误,指出应该首先编译.请参见下面的代码段:

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:您必须先编译模型,然后再使用它.

RuntimeError: You must compile your model before using it.

我使用相同的设置来训练单向RNN,效果很好.任何帮助解决运行时错误的技巧都将受到赞赏. (重新启动内核没有帮助)
也许我没有正确实例化双向"?

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?

请注意:此问题与不同在"X"之前类型的问题
注意2:可以找到这里

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

推荐答案

找到了,
使用双向时,应将其视为一个层,将input_shape包含在Bidirectional()中而不是GRU()对象中即可解决此问题

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

如此

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

成为

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

这篇关于Keras双向"RuntimeError:您必须在使用模型之前对其进行编译."编译完成后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆