我训练了一个关于股票价格的 BI-LSTM 模型。 对于这个模型,我做了两种回测方法:
使用 1) 方法,结果看起来相当不错,RMSE 接近 0.25,这可以告诉我该模型在测试集上具有良好的性能,如下:
使用的代码:
pas = 20
train_len=5586
test_data = df_scaled[train_len - pas: ]
print ('len(test_data):', len(test_data))
# Create the data sets x_test and y_test
x_test = []
y_test = df[train_len:, :]
for i in range(pas, len(test_data)):
x_test.append(test_data[i-pas:i, 0])
# Convert the data to a numpy array
x_test = np.array(x_test)
# Reshape the data
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1 ))
# Get the models predicted price values
fichier_modele = f"{symbols}.h5"
model = load_model(fichier_modele)
predictions = model.predict(x_test)
scaler2=dictio_scalers[symbols]
predictions = scaler2.inverse_transform(predictions)
使用的代码:
Pred_Array_Global=df[int(train_len)-pas:int(train_len)]
# Get the models predicted price values
fichier_modele = f"{symbols}.h5"
model = load_model(fichier_modele)
scaler2=dictio_scalers[symbols]
Pred_Array_Global=scaler2.fit_transform(Pred_Array_Global)
for i in range(0,len(test['Close'])):
Pred_Array_Global=np.array(Pred_Array_Global)
Pred_Array=Pred_Array_Global[i:i+pas]
# Convert the data to a numpy array
Pred_Array = np.array(Pred_Array)
# Reshape the data
Pred_Array_Input = np.reshape(Pred_Array,(1,pas, 1 ))
predictions = model.predict(Pred_Array_Input,verbose=0)
Pred_Array_Global=np.append(Pred_Array_Global,predictions)
Pred_Array_Global=Pred_Array_Global.reshape(-1,1)
Pred_Array_Global = scaler2.inverse_transform(Pred_Array_Global)
由于模型在整个测试数据上表现非常好,如果我想每天进行预测,我预计性能会略有下降,但不是直线!
我的代码有错误吗?
注意:这是我用来构建模型的代码(epochs=2000,batch_size=256):
model = Sequential()
model.add(Bidirectional(LSTM(units=128, input_shape=(20, 1))))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,verbose=0)
fichier_modele = f"{symbols}.h5"
model.save(fichier_modele)
您的两种方法是两个不同的问题。这取决于您想要预测未来多久。
通过您的训练代码,它是为模型学习预测未来一步而设计的。然后,您将未来的地面实况数据作为模型的输入,以便在验证阶段输入模型的值看起来很可能是经过训练的数据,这就是它提供良好性能的原因。
使用第二种方法,这是长期预测问题,您希望模型根据过去的 K 步来预测未来的 N 步,这显然比第一种方法更难,而且这不是您训练模型的内容学习。但你仍然可以使用 LSTM 来做到这一点,看看递归预测。