简体   繁体   English

keras “您必须在使用前编译您的 model。” 即使在编译之后

[英]keras “You must compile your model before using it.” even after compilation

I am using the following code to fit my LSTM network to a time series generator:我正在使用以下代码将我的 LSTM 网络适合时间序列生成器:

data_gen = TimeseriesGenerator(x_train,y_train,length=10, sampling_rate=2,batch_size=5110)

def rmsle_K(y, y0):
    return K.sqrt(K.mean(K.square(tf.log1p(y) - tf.log1p(y0))))

regressor = Sequential()
regressor.add(Bidirectional(LSTM(units=100,return_sequences= True),input_shape=(x_train.shape[1],12)))
regressor.add(Dropout(0.1))
regressor.add(Bidirectional(LSTM(units=100,return_sequences= True)))
regressor.add(Dropout(0.1))
regressor.add(Bidirectional(LSTM(units=100,return_sequences= True)))
regressor.add(Dropout(0.1))
regressor.add(Dense(units=1))
regressor.compile(loss=keras.losses.mean_squared_logarithmic_error, optimizer='adam', metrics=[rmsle_K])

regressor.fit_generator(data_gen)

Error: RuntimeError: You must compile your model before using it.错误:RuntimeError:您必须在使用前编译您的 model。

x_train.shape = (340, 5110, 12).y_train.shape = (3400, 511, 1) How can I fix this error? x_train.shape = (340, 5110, 12).y_train.shape = (3400, 511, 1) 我该如何解决这个错误? I feel that I am messsing up in the input and output dimensions, but I am not sure.我觉得我在输入和 output 尺寸方面搞砸了,但我不确定。

The issue with your code is that you are misplacing the input shape so your model is not compiling right.您的代码的问题是您放错了输入形状,因此您的 model 编译不正确。 Even with this I dont think your input shape is correct.即使这样,我认为您的输入形状也不正确。

You should use你应该使用

regressor.add(Bidirectional(LSTM(units=100,return_sequences= True),input_shape=(x_train.shape[1],1)))

instead of代替

regressor.add(Bidirectional(LSTM(units=100,return_sequences= True,input_shape=(x_train.shape[1],1))))

This would solve your current error as the model will get compiled.这将解决您当前的错误,因为 model 将被编译。 And setting the right input shape is a topic that should be discussed separately.而设置正确的输入形状是一个应该单独讨论的话题。

Hope this helps希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循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编码器 - 解码器模型RuntimeError:您必须在使用之前编译模型 - Keras encoder-decoder model RuntimeError: You must compile your model before using it 错误“在使用它之前必须编译模型”,如果是Keras中的LSTM和fit_generator - Error “You must compile your model before using it” in case of LSTM and fit_generator in Keras 合并Conv2D和Dense模型会导致“ RuntimeError:您必须在使用模型之前对其进行编译。”,尽管已编译了合并模型 - Merging Conv2D and Dense models results in “RuntimeError: You must compile your model before using it.”, despite having compiled the merged model Keras:尽管使用了 compile(),但“使用前必须编译模型” - Keras: “must compile model before using it” despite compile() is used Keras Model甚至在编译调用后仍要求进行编译 - Keras Model asks for compiling even after compile call Keras 顺序 Model 编译成功后不拟合 - Keras Sequential Model is not fitting after successful compilation 使用Keras模型时出现“您必须输入占位符张量的值”错误 - Getting “You must feed a value for placeholder tensor” error when using Keras model 编辑keras模型后,“无”是什么意思? - What does a “None” mean after compilation of keras model? 为什么 keras model 在编译后预测会变慢? - Why does keras model predict slower after compile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM