掩盖填充值会影响LSTM预测 输入示例输出示例估计输出示例-y_test-

问题描述

我需要使用LSTM进行seq2seq预测。由于我在时间步长方面具有不同长度的序列,因此我用9999.0填充值,然后使用了一个蒙版层以忽略9999值。但是,我的模型不会忽略它们,也不会预测填充值。 我使用了下面的代码

verbose,epochs,batch_size = 0,70,16
n_timesteps,n_features,n_outputs = x_train.shape[1],x_train.shape[2],y_train.shape[2]
print(n_timesteps,n_outputs )
# define model
model_lstm = Sequential()
model.add(tf.keras.layers.Masking(mask_value=9999.,input_shape=(n_timesteps,n_features)))
model_lstm.add(LSTM(200,activation='relu',n_features)))
model_lstm.add(Dense(100,activation='relu'))
model_lstm.add(Dense(n_outputs))
model_lstm.compile(loss='mse',optimizer='adam')
    # fit network
model_lstm.fit(x_train,y_train,epochs=epochs,batch_size=batch_size,verbose=verbose)
y_predict = model.predict(x_test) 

输入示例

enter image description here

输出示例

enter image description here

估计输出示例-y_test-

enter image description here

如您所见,我的模型预测的值与预期值相差很远。你建议我做什么?遮罩层会使模型产生偏差吗?

解决方法

只要知道如何处理掩码,掩码就会通过网络传播到下一层。在 LSTM 层之后,掩码停止传播,因为 LSTM 层具有 return_sequences=False 的默认参数(时间维度崩溃)。您可以通过运行来验证:

for i,l in enumerate(model.layers):
    print(f'layer {i}: {l}')
    print(f'\thas input mask: {l.input_mask}')
    print(f'\thas output mask: {l.output_mask}')