用于连续数据预测的 RNN 算法中的损失值和 val_loss 值不减少

loss value and val_loss value not decreasing in RNN algorithm for continuous data prediction

我正在使用 RNN 构建房价预测模型,下面是代码。数据集没有空值并且已完全清理,但我仍然得到恒定且高的损失和 val_loss 值。我怎样才能使这些值降低值?

A = dataset.drop(['price'],axis="columns")
B = dataset['price']

from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
A_scale = min_max_scaler.fit_transform(A)

from sklearn.model_selection import train_test_split
A_train, A_test, B_train, B_test = train_test_split(A_scale, B, test_size=0.3)
a_val, a_test, b_val, b_test = train_test_split(A_test, B_test, test_size=0.5)

from keras.models import Sequential
from keras.layers import Dense,LSTM,Dropout
regressor = Sequential()

model = Sequential([
Dense(32, activation='relu', input_shape=(10,)),
Dense(32, activation='relu'),
Dense(1, activation='sigmoid'),
])
model.compile(optimizer='adam',loss='mse',metrics=['mae'])

hist = model.fit(A_train, B_train, batch_size=32, epochs=4, validation_data=(a_val, b_val))

输出:

Epoch 1/20
292/292 [==============================] - 0s 1ms/step - loss: 36314.9180 - mae: 111.9050 - val_loss: 23161.0312 - val_mae: 106.9015
Epoch 2/20
292/292 [==============================] - 0s 646us/step - loss: 36295.7930 - mae: 111.8202 - val_loss: 23160.9219 - val_mae: 106.9010
Epoch 3/20
292/292 [==============================] - 0s 715us/step - loss: 36295.7383 - mae: 111.8199 - val_loss: 23160.9121 - val_mae: 106.9009
Epoch 4/20
292/292 [==============================] - 0s 716us/step - loss: 36295.7422 - mae: 111.8199 - val_loss: 23160.9082 - val_mae: 106.9009

这可能意味着很多事情,但我想到了三件事:

  1. 调整模型超参数中的学习率非常重要。 This 会给你一些关于什么是学习率的背景信息:)
  2. 向模型添加更多轮数将有助于它收敛到局部最小值。
  3. 如果要进行回归,请使用线性激活函数。

要实现它,请尝试以下操作:

from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Dense

LR=0.001
EPOCHS=100
BATCH_SIZE=32

opt = Adam(lr=LR, decay=LR/EPOCHS)

model = Sequential([
Dense(32, activation='relu', input_shape=(10,)),
Dense(32, activation='relu'),
Dense(1, activation='linear'),
])
model.compile(optimizer=opt, loss='mse', metrics=['mae'])

hist = model.fit(A_train, B_train, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_data=(a_val, b_val))

我鼓励您进行实验、试错、阅读所有超参数及其效果,并尝试在神经网络的每一层上使用不同的组合。