regression - 即使 val_loss 较低,我使用 LSTM 的回归 NN 的输出也是错误的
问题描述
该模型
我目前正在研究一堆 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_X
如下val_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()
所以我不确定为什么会这样。
解决方案
推荐阅读
- google-kubernetes-engine - 使用自定义服务帐户从 GKE 中拉取 GCR 中的 Docker 映像
- c++ - 编辑单个 YAML 值而不更新 YAML 其余部分的格式(YAML CPP)
- angular - 如何在 NestJS 中管理 Strategy 用户信息并在 Angular 中将其传递给客户端?
- sql - Amazon RDS - Postgres 不使用 SELECT 查询的索引
- swift - 替代案例 Swift
- flutter - 如何转换列表
Dart 中的字符串 - mysql - 在mysql数据库的所有表中搜索
- ios13 - 在 iOS13 上读取 ISO 15693 标签
- azure - 用于配置诊断设置的 Azure 网关 ARM 模板(Log Analytics 工作区)
- plot - 如果使用 Pine 脚本在 Tradingview 中的 5m 间隔图表的末尾有一个特定的小数说“.55”,如何标记今天的高点