我建立了一个1层LSTM模型,该模型可以预测未来1小时内的乘车需求。
为了测试鲁棒性,我想多次重复预测并求平均RSME得分。在此example之后,我想使用一个for循环来实现这一点。
这段代码包括一个适合LSTM模型的函数和一个进行预测的函数:
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from statsmodels.tools.eval_measures import rmse
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from math import sqrt
def fit_lstm(x_train_scaled, y_train_scaled, batch, ep, neurons_lstm, neuron_dense):
model = Sequential()
model.add(LSTM(units=neurons_lstm, input_shape=(x_train_scaled.shape[1], x_train_scaled.shape[2])))
model.add(Dropout(rate=0.2))
model.add(Dense(units=neuron_dense))
model.compile(loss="mean_squared_error",
optimizer="adam",
)
history = model.fit(x_train_scaled,
y_train_scaled,
epochs=ep,
batch_size=batch,
validation_split=0.1,
verbose=2,
shuffle=False)
return model, history
def predictions_lstm(x_test_scaled, y_test):
# get predictions
test_pred = model.predict(x_test_scaled)
# reverse transform predictions
test_pred_inv = y_scaler.inverse_transform(test_pred)
# reverse differencing predictions
inverted_pred = list()
for i in range(len(test_pred_inv)):
value = inverse_difference(df_lstm.iloc[:,0], test_pred_inv[i], len(test_pred_inv)-i)
inverted_pred.append(value)
inverted_pred = pd.DataFrame(inverted_pred)
# reverse differencing y_test
inverted_ytest = list()
for i in range(len(y_test)):
value = inverse_difference(df_lstm.iloc[:,0], y_test[i], len(y_test)+1-i)
inverted_ytest.append(value)
inverted_ytest = pd.DataFrame(inverted_ytest)
# create single dataframe with predictions (1) and expected values (2)
predictions = np.concatenate((inverted_pred, inverted_ytest), axis=1)
predictions = pd.DataFrame(predictions)
return test_pred, test_pred_inv, predictions
这是导致元组错误的函数。如您所见,我从上面创建了带有预测功能的for循环。
repeats = 30
error_scores = list()
def repeat_pred_lstm(repeats):
for i in range(repeats):
# fit the model
model= fit_lstm(x_train_scaled,
y_train_scaled,
batch=100, ep=10,
neurons_lstm=5,
neuron_dense=1)
# make predictions
predictions = predictions_lstm(x_test_scaled, y_test)
# report performance
rmse = sqrt(mean_squared_error(predictions[0], predictions[1]))
mse_error = rmse**2
error_scores.append(rmse)
# summarize the results
results = pd.DataFrame()
results["rmse"] = error_scores
print(results.head())
print(results.describe())
results.boxplot()
return results
results = repeat_pred_lstm(repeats)
但是,for循环给我以下错误,我不理解,因为据我所知,预测函数中没有元组对象。据我了解,对LSTM模型进行预测的输入必须始终是一个numpy数组而不是一个元组。
AttributeError Traceback (most recent call last)
<ipython-input-44-8c61191ae4b3> in <module>
27 return results
28
---> 29 results = repeat_pred_lstm(repeats)
<ipython-input-44-8c61191ae4b3> in repeat_pred_lstm(repeats)
11 neuron_dense=1)
12 # make predictions
---> 13 predictions = predictions_lstm(x_test_scaled, y_test)
14 # report performance
15 rmse = sqrt(mean_squared_error(predictions[0], predictions[1]))
<ipython-input-39-5b4f5fdbedd3> in predictions_lstm(x_test_scaled, y_test)
1 def predictions_lstm(x_test_scaled, y_test):
2 # get predictions
----> 3 test_pred = model.predict(x_test_scaled)
4
5 # reverse transform predictions
AttributeError: 'tuple' object has no attribute 'predict'
当我仅使用“ predictions_lstm(x_test_scaled,y_test)函数”进行一次预测时,我就能获得RMSE分数,但是当我使用for循环时,“ predictions_lsmt(x_test_scaled,y_test)函数”给出错误
有人可以告诉我我想念什么吗?
感谢您的帮助!
答案 0 :(得分:1)
fit_ltsm
返回一个元组:return model, history
。
但是,您不处理历史记录,这意味着,您已将模型设置为整个元组,而不是实际模型:
model= fit_lstm(x_train_scaled,
y_train_scaled,
batch=100, ep=10,
neurons_lstm=5,
neuron_dense=1)
您可以通过将return分成两个变量来解决此问题,例如:
model, history = fit_ltsm(. . .)
答案 1 :(得分:1)
错误与fit_lstm
函数有关。它返回一个元组,并且您已将该元组作为单个变量model
捕获,请更改
model= fit_lstm(x_train_scaled,
y_train_scaled,
batch=100, ep=10,
neurons_lstm=5,
neuron_dense=1)
到
model,history= fit_lstm(x_train_scaled,
y_train_scaled,
batch=100, ep=10,
neurons_lstm=5,
neuron_dense=1)