我编写了一个LSTM回归模型.它是最后一个LSTM层的BATCH_SIZE=1和RETURN_Sequence=True的模型.我还设置了VERIFICATION_DATA和耐心进行培训.但似乎存在一些问题.
以下是我的代码.我也有一些基本的问题.
# Import the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from keras.layers import TimeDistributed
from keras import optimizers
from keras.callbacks import EarlyStopping
# x and y are numpy array with shape (1,251,1)
x_test = x[:,-84:,:] # use the last 84 data as test data
y_test = y[:,-84:,:]
x_val = x[:,-36-84:-84,:] # the number of validation data is 36
y_val = y[:,-36-84:-84,:]
x = x[:,:-36-84,:] # x and y are training data
y = y[:,:-36-84,:]
# the first 36 data are not used to calculate the loss,
# set a numpy array for sample_weight used in Model.fit
sw = (np.arange(251) >= 36).astype('float64')
sw = np.reshape(sw, (1, sw.shape[0]))
sw = sw[:,:-36-84]
regressor = Sequential()
# an LSTM model of 3 layers
for k in range(3):
if k == 0:
regressor.add(LSTM(units = 200, return_sequences = True, input_shape = (None, 1)))
else:
regressor.add(LSTM(units = 200, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(TimeDistributed(Dense(units = 1)))
opt1 = optimizers.Adam(learning_rate=0.005,global_clipnorm=0.3)
regressor.compile(optimizer = opt1, loss = 'mean_squared_error')
callback = EarlyStopping(monitor='val_loss', patience=200, restore_best_weights=True)
history = regressor.fit(x, y, epochs = 10000, batch_size = x.shape[0], sample_weight=sw,
validation_data=(x_val, y_val), callbacks=[callback])
我有以下问题
-
设置SAMPLE_WEIGHT是否正确,以便模型在前36个时间步长内不会输出?还有别的办法吗?
-
我为第一个LSTM层设置INPUT_Shape=(None,1)是否正确?因为训练数据和验证数据具有不同的时间步长.
-
我的输出层正确吗?每个TimeStep都有一个输出.
-
VAL_LOSS的计算是否包括辍学层?
-
计算完成后,计算验证数据的均方根,并将其与最小Val_Loss进行比较.值分别为0.00047和0.0010,因此我的代码中存在错误.
非常感谢.
训练数据和验证数据不被视为单个序列.