在 Keras 中使用 LSTM 深度学习模型增强时间序列预测评估 - 提供代码

问题描述 投票:0回答:0

我是深度学习的新手,目前正在从事一个需要预测价值的项目。为了训练自己,我决定使用温度数据集。但是,我面临一些挑战,需要一些指导。

我已经在 Google Colab 上分享了我的项目代码,可以使用以下链接访问:Colab notebook 的链接(里面还有一个数据集的链接)。

总而言之,我的项目涉及根据历史数据集预测未来的温度值。但是,我不确定我的模型的结构以及如何使用测试数据集对其进行评估。对于改进我的代码的任何帮助或建议,我将不胜感激。

我想解决以下问题:

  • 模型的数据准备:我是否使用了正确的技术来准备数据?

  • LSTM:目前的模型在短期预测方面表现相当不错,但我很难在更长的时间范围内获得准确的结果。一旦我确定代码的结构,我将使用贝叶斯优化来微调超参数。如果您有建议,我很感兴趣?

  • Epoch:val_loss 在第一个或第二个 epoch 降到那么低是否正常?

Predicted vs observed evaluation:我尝试了两种方法来比较预测值和观察值,但我不确定哪个是正确的:

解决方案一:

# Create a generator for the test set
test_generator = TimeseriesGenerator(test_data, test_data, length=n_input, batch_size=1)

# Make predictions
preds_scaled = model.predict(test_generator, verbose=0)

# Invert MinMaxScaler to get the original scale
preds_orig = scaler.inverse_transform(preds_scaled)

# Get the true values
true_vals = temp_df.iloc[train_len+val_len+n_input:].values.reshape(-1, 1)

# Calculate the mean squared error
mse = mean_squared_error(true_vals, preds_orig)
print('MSE:', mse)

# Calculate the root mean squared error
rmse = np.sqrt(mse)
print('RMSE:', rmse)

# Plot predicted vs observed values
plt.figure(figsize=(15, 6))
plt.plot(true_vals, label='Observed')
plt.plot(preds_orig, label='Predicted')
plt.legend()
plt.show()

Output plot predicted vs oberved 1

解决方案2:

# Create a generator for the test set
test_generator = TimeseriesGenerator(test_data, test_data, length=n_input, batch_size=1)

# The number of future predictions
future_steps = len(test_generator)

# Start with the last data points from the training set
input_sequence = train_data[-n_input:].reshape((1, n_input, n_features))

# Store the predictions
future_preds_scaled = []

# Iterate over the future steps
for i in range(future_steps):
    # Predict the next value
    pred = model.predict(input_sequence, verbose=0)[0]
    # Append the prediction to the list
    future_preds_scaled.append(pred)
    # Drop the first value of the sequence and append the prediction
    input_sequence = np.append(input_sequence[:,1:,:],[[pred]], axis=1)

# Convert the list to numpy array
future_preds_scaled = np.array(future_preds_scaled)

# Invert MinMaxScaler to get the original scale
future_preds_orig = scaler.inverse_transform(future_preds_scaled)

# Get the true values
true_vals = temp_df.iloc[train_len+val_len+n_input:train_len+val_len+n_input+future_steps].values.reshape(-1, 1)

# Calculate the mean squared error
mse = mean_squared_error(true_vals, future_preds_orig)
print('MSE:', mse)

# Calculate the root mean squared error
rmse = np.sqrt(mse)
print('RMSE:', rmse)

# Plot predicted predicted values vs observed values
plt.figure(figsize=(15, 6))
plt.plot(true_vals, label='Observed')
plt.plot(future_preds_orig, label='Predicted Future')
plt.legend()
plt.show()

Output plot predicted vs observed 2

感谢您的时间和协助!

keras deep-learning time-series lstm recurrent-neural-network
© www.soinside.com 2019 - 2024. All rights reserved.