[英]Error “You must compile your model before using it” in case of LSTM and fit_generator in Keras
I create my own class which create a Keras model inside one of its methods. 我创建了自己的类,在其中一个方法中创建了一个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'])
In other method i try to train this model using python generator as data provider. 在其他方法中,我尝试使用python生成器作为数据提供者来训练此模型。
self.model.fit_generator(my_gen(), steps=10, epochs=1, verbose=1)
This causes an error: 这会导致错误:
raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.
Error does not rises if i change LSTM layer to Dense layer. 如果我将LSTM层更改为Dense层,则错误不会上升。 What am i doing wrong?
我究竟做错了什么?
Keras version 2.2.0 with Tensorflow 1.8.0 backend. Keras版本2.2.0,带有Tensorflow 1.8.0后端。
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. 似乎第一个
input_shape
LSTM层在使用input_shape
时仍然需要fit_generator
,这在fit_generator
中似乎缺失,并导致“你必须在使用之前编译你的模型”错误。
To solve make sure you have an input_shape
parameter in your first LSTM layer as shown by the example below: 要解决此问题,请确保在第一个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')
I experience a similar problem. 我遇到了类似的问题。 I could resolve it by using:
我可以使用以下方法解决它:
self.model.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['acc'])
before : 之前:
self.model.fit_generator(my_gen(), steps=10, epochs=1, verbose=1)
in the function where fit_generator()
was called. 在调用
fit_generator()
的函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.