简体   繁体   English

错误“在使用它之前必须编译模型”,如果是Keras中的LSTM和fit_generator

[英]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.

相关问题 在 Keras LSTM 中添加双向会导致 RuntimeError('您必须在使用之前编译模型。') - Adding Bidirectional in a Keras LSTM results in a RuntimeError('You must compile your model before using it.') Keras模型fit_generator奇数错误 - Keras model fit_generator odd error keras模型中的fit_generator() - fit_generator() in keras model 为什么keras模型编译但fit_generator命令抛出“模型未编译的运行时错误”? - Why does the keras model compile but the fit_generator command throws a 'model not compiled runtime error'? 在 Keras 中使用 fit_generator - Using fit_generator in Keras Keras编码器 - 解码器模型RuntimeError:您必须在使用之前编译模型 - Keras encoder-decoder model RuntimeError: You must compile your model before using it 使用fit_generator时,Keras模型的批量大小为1 - Keras model is doing batch size of 1 when using fit_generator keras “您必须在使用前编译您的 model。” 即使在编译之后 - keras “You must compile your model before using it.” even after compilation 使用ImageDataGenerator(Keras)时,fit_generator输入尺寸错误 - fit_generator input dimensions error when using ImageDataGenerator (Keras) 使用Keras fit_generator会产生错误的形状错误 - Using Keras fit_generator gives an error of wrong shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM