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