我有一个27个特征的数据集,1012个训练数据和125个测试数据.使用LSTM网络我在训练集上训练数据。但当测试时,我不想让它一次预测所有125个数据,因为我正在处理时间序列。相反,我希望网络能够在测试数据中迭代,每次预测一个点,并逐步更新自己。为此,我写了以下代码,使用索引在测试数据中迭代。
Predictions = list()
for i in range(X):
model = load_model('model %s' %i)
y_pred = model.predict(x_test_t[i], batch_size=BATCH_SIZE)
y_pred = y_pred.flatten()
# Descaling the Predicted Values
Dynamic_Trainer.pred = (y_pred * min_max_scaler.data_range_[3]) + min_max_scaler.data_min_[3]
Dynamic_Trainer.test = (y_test_tt * min_max_scaler.data_range_[3]) + min_max_scaler.data_min_[3]
#Saving the model for each new data point predicted and added to training
u = i+1
model = model.save(Output_path + \Model %d'%u)
# Saving each new prediction (Dynamic_Trainer is the function i made of the LSTM)
Predictions.append(Dynamic_Trainer.pred)
然而我得到了这个错误。
ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (4, 27)
TLDR: 我怎样才能在三维数据上迭代一次提取一个三维数据并将其输入网络。
EDIT: 如果有更有效的方法来实现同样的目标,我愿意接受建议。
我找到的解决方案是感谢@Marco Cerliani,对于有同样问题的人来说,只需使用:y_pred = model.pred(x_test_t[i][None,:,:]) for the loop.