keras - LSTM 时间序列产生偏移预测?

我正在使用 LSTM NN 和 Keras 进行时间序列预测。作为输入特征,有两个变量(降水量和温度),要预测的一个目标是地下水位。

虽然实际数据和输出之间存在严重的偏移(见图),但它似乎工作正常。

现在我读到这可能是网络不工作的典型标志,因为它似乎在模仿输出和

what the model is actually doing is that when predicting the value at time “t+1”, it simply uses the value at time “t” as its prediction https://towardsdatascience.com/how-not-to-use-machine-learning-for-time-series-forecasting-avoiding-the-pitfalls-19f9d7adf424

但是,这在我的例子中实际上是不可能的,因为目标值没有用作输入变量。我正在使用具有两个特征的多变量时间序列,独立于输出特征。 此外,预测值在未来 (t+1) 时并没有被抵消,而是似乎落后于 (t-1)。

有谁知道可能导致此问题的原因是什么?

这是我的网络的完整代码:

# Split in Input and Output Data 
x_1 = data[['MeanT']].values
x_2 = data[['Precip']].values
y = data[['Z_424A_6857']].values

# Scale Data
x = np.hstack([x_1, x_2])
scaler = MinMaxScaler(feature_range=(0, 1))
x = scaler.fit_transform(x)

scaler_out = MinMaxScaler(feature_range=(0, 1))
y = scaler_out.fit_transform(y)

# Reshape Data
x_1, x_2, y = H.create2feature_data(x_1, x_2, y, window)
train_size = int(len(x_1) * .8)
test_size = int(len(x_1)) #  * .5

x_1 = np.expand_dims(x_1, 2) # 3D tensor with shape (batch_size, timesteps, input_dim) // (nr. of samples, nr. of timesteps, nr. of features)
x_2 = np.expand_dims(x_2, 2)
y = np.expand_dims(y, 1)

# Split Training Data
x_1_train = x_1[:train_size]
x_2_train = x_2[:train_size]
y_train = y[:train_size]

# Split Test Data
x_1_test = x_1[train_size:test_size]
x_2_test = x_2[train_size:test_size]
y_test = y[train_size:test_size]

# Define Model Input Sets
inputA = Input(shape=(window, 1))
inputB = Input(shape=(window, 1))

# Build Model Branch 1
branch_1 = layers.GRU(16, activation=act, dropout=0, return_sequences=False, stateful=False, batch_input_shape=(batch, 30, 1))(inputA)
branch_1 = layers.Dense(8, activation=act)(branch_1)
#branch_1 = layers.Dropout(0.2)(branch_1)
branch_1 = Model(inputs=inputA, outputs=branch_1) 

# Build Model Branch 2
branch_2 = layers.GRU(16, activation=act, dropout=0, return_sequences=False, stateful=False, batch_input_shape=(batch, 30, 1))(inputB)
branch_2 = layers.Dense(8, activation=act)(branch_2)
#branch_2 = layers.Dropout(0.2)(branch_2)
branch_2 = Model(inputs=inputB, outputs=branch_2) 

# Combine Model Branches
combined = layers.concatenate([branch_1.output, branch_2.output])
 
# apply a FC layer and then a regression prediction on the combined outputs
comb = layers.Dense(6, activation=act)(combined)
comb = layers.Dense(1, activation="linear")(comb)
 
# Accept the inputs of the two branches and then output a single value
model = Model(inputs=[branch_1.input, branch_2.input], outputs=comb)
model.compile(loss='mse', optimizer='adam', metrics=['mse', H.r2_score])

model.summary()

# Training
model.fit([x_1_train, x_2_train], y_train, epochs=epoch, batch_size=batch, validation_split=0.2, callbacks=[tensorboard])
model.reset_states()

# Evaluation
print('Train evaluation')
print(model.evaluate([x_1_train, x_2_train], y_train))

print('Test evaluation')
print(model.evaluate([x_1_test, x_2_test], y_test))

# Predictions
predictions_train = model.predict([x_1_train, x_2_train])
predictions_test = model.predict([x_1_test, x_2_test])

predictions_train = np.reshape(predictions_train, (-1,1))
predictions_test = np.reshape(predictions_test, (-1,1))

# Reverse Scaling
predictions_train = scaler_out.inverse_transform(predictions_train)
predictions_test = scaler_out.inverse_transform(predictions_test)

# Plot results
plt.figure(figsize=(15, 6))
plt.plot(orig_data, color='blue', label='True GWL')  
plt.plot(range(train_size), predictions_train, color='red', label='Predicted GWL (Training)')
plt.plot(range(train_size, test_size), predictions_test, color='green', label='Predicted GWL (Test)')
plt.title('GWL Prediction')  
plt.xlabel('Day')  
plt.ylabel('GWL')  
plt.legend()  
plt.show()   

我使用的批处理大小为 30 个时间步长,回溯时间为 90 个时间步长,总数据大小约为 7500 个时间步长。

任何帮助将不胜感激:-) 谢谢!

最佳答案

可能两年后我的回答不相关了,但我在试验 LSTM 编码器-解码器模型时遇到了类似的问题。我通过在 -1 .. 1 范围内缩放输入数据而不是像您的示例中的 0 .. 1 解决了我的问题。

https://stackoverflow.com/questions/57927187/

相关文章:

reactjs - React-Select 从 1.x 升级到 3.0.4 - 未显示选定值

npm - 无法在 Nexus Repository Manager 2.10 上发布库

javascript - 如何转到 Epubjs 中的特定页面

html - 使用具有 Angular Material 和 Flex Layout 的语义 HTM

java - 从 jlink 图像创建所有包含 windows 可执行文件

python - Keras:使用 flow_from _directory() 函数为两个输入模型

python - 使用 image_slicer.py 将切片加入完整图像

reactjs - props 没有传递给 react native 中的另一个组件

python - 如何让这段代码在时间上更加优化?

node.js - 如何在 Node SOAP 中发送原始 XML 请求