keras "You must compile your model before using it." 即使在编译之后

keras "You must compile your model before using it." even after compilation

我正在使用以下代码使我的 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.

x_train.shape = (340, 5110, 12).y_train.shape = (3400, 511, 1) 如何修复此错误?感觉输入输出维度搞乱了,但是又不确定

您的代码存在的问题是您放错了输入形状,因此您的模型无法正确编译。尽管如此,我认为您的输入形状不正确。

你应该使用

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

而不是

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

这将解决您当前的错误,因为模型将得到编译。而设置正确的输入形状是一个应该单独讨论的话题。

希望对您有所帮助