![](/img/trans.png)
[英]Adding Bidirectional in a Keras LSTM results in a RuntimeError('You must compile your model before using it.')
[英]Error “You must compile your model before using it” in case of LSTM and fit_generator in Keras
我创建了自己的类,在其中一个方法中创建了一个Keras模型。
self.model = Sequential()
self.model.add(LSTM(32))
self.model.add(Dense(2, activation='relu'))
self.model.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['acc'])
在其他方法中,我尝试使用python生成器作为数据提供者来训练此模型。
self.model.fit_generator(my_gen(), steps=10, epochs=1, verbose=1)
这会导致错误:
raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.
如果我将LSTM层更改为Dense层,则错误不会上升。 我究竟做错了什么?
Keras版本2.2.0,带有Tensorflow 1.8.0后端。
似乎第一个input_shape
LSTM层在使用input_shape
时仍然需要fit_generator
,这在fit_generator
中似乎缺失,并导致“你必须在使用之前编译你的模型”错误。
要解决此问题,请确保在第一个LSTM图层中有一个input_shape
参数,如下例所示:
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')
我遇到了类似的问题。 我可以使用以下方法解决它:
self.model.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['acc'])
之前:
self.model.fit_generator(my_gen(), steps=10, epochs=1, verbose=1)
在调用fit_generator()
的函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.