即使val_loss低,我的带有LSTM的回归NN的输出也是错误的

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

模型

我目前正在研究一堆LSTM,并试图解决回归问题。该模型的体系结构如下:

comp_lstm = tf.keras.models.Sequential([
    tf.keras.layers.LSTM(64, return_sequences = True),
    tf.keras.layers.LSTM(64, return_sequences = True),
    tf.keras.layers.LSTM(64),
    tf.keras.layers.Dense(units=128),
    tf.keras.layers.Dense(units=64),
    tf.keras.layers.Dense(units=32),
    tf.keras.layers.Dense(units=1)
])

comp_lstm.compile(optimizer='adam', loss='mae')

[当我训练模型时,它显示出一些良好的损耗和val_loss数字:

Epoch 6/20
200/200 [==============================] - 463s 2s/step - loss: 1.3793 - val_loss: 1.3578
Epoch 7/20
200/200 [==============================] - 461s 2s/step - loss: 1.3791 - val_loss: 1.3602

现在我运行代码以使用以下代码检查输出:

idx = np.random.randint(len(val_X))
sample_X, sample_y = [[val_X[idx,:]]], [[val_y[idx]]]
test = tf.data.Dataset.from_tensor_slices(([sample_X], [sample_y]))
prediction = comp_lstm.predict(test)
print(f'The actual value was {sample_y} and the model predicted {prediction}')

输出为:

The actual value was [[21.3]] and the model predicted [[2.7479606]]

接下来的几次运行,我得到了值:

The actual value was [[23.1]] and the model predicted [[0.8445232]]
The actual value was [[21.2]] and the model predicted [[2.5449793]]
The actual value was [[22.5]] and the model predicted [[1.2662419]]

我不确定为什么会这样解决。 val_loss非常低,但是输出却大不相同。


数据整理

为了获得train_Xval_X等而进行的数据争夺如下所示:

hist2 = 128 

features2 = np.array(list(map(list,[df["scaled_temp"].shift(x) for x in range(1, hist2+1)]))).T.tolist()
df_feat2 = pd.DataFrame([pd.Series(x) for x in features2], index = df.index)
df_trans2 = df.join(df_feat2).drop(columns=['scaled_temp']).iloc[hist2:]
df_trans2 = df_trans2.sample(frac=1)
target = df_trans2['T (degC)'].values
feat2 = df_trans2.drop(columns = ['T (degC)']).values

feat2的形状为(44435, 128),而target的形状为(44435,)

df["scaled_temp"]列的数据框如下所示(已使用标准缩放器缩放):

Date Time
2020-04-23T21:14:07.546476Z   -0.377905
2020-04-23T21:17:32.406111Z   -0.377905
2020-04-23T21:17:52.670373Z   -0.377905
2020-04-23T21:18:55.010392Z   -0.377905
2020-04-23T21:19:57.327291Z   -0.377905
                                 ...   
2020-06-08T09:13:06.718934Z   -0.889968
2020-06-08T09:14:09.170193Z   -0.889968
2020-06-08T09:15:11.634954Z   -0.889968
2020-06-08T09:16:14.087139Z   -0.889968
2020-06-08T09:17:16.549216Z   -0.889968
Name: scaled_temp, Length: 44563, dtype: float64

df ['T(degC)']的数据帧如下所示:

Date Time
2020-05-09T07:30:30.621001Z    24.0
2020-05-11T15:56:30.856851Z    21.3
2020-05-27T05:02:09.407266Z    28.3
2020-05-02T09:33:03.219329Z    20.5
2020-05-31T03:20:04.326902Z    22.4
                               ... 
2020-05-31T01:47:45.982819Z    23.1
2020-05-27T08:03:21.456607Z    27.2
2020-05-04T21:58:36.652251Z    20.9
2020-05-17T18:42:39.681050Z    22.5
2020-05-04T22:07:58.350329Z    21.1
Name: T (degC), Length: 44435, dtype: float64

数据集创建过程如下:

train_X, val_X = feat2[:int(feat2.shape[0]*0.95), :], feat2[int(feat2.shape[0]*0.95):, :]
train_y, val_y = target[:int(target.shape[0]*0.95)], target[int(target.shape[0]*0.95):]
train = tf.data.Dataset.from_tensor_slices(([train_X], [train_y])).batch(BATCH_SIZE).repeat()
val = tf.data.Dataset.from_tensor_slices(([val_X], [val_y])).batch(BATCH_SIZE).repeat()

因此,我不确定为什么会这样。

regression lstm tensorflow2.0
1个回答
0
投票

您不认为预测需要转换回原始比例,即我们需要对预测进行相同的标准标量对象的逆转换。

© www.soinside.com 2019 - 2024. All rights reserved.