我正在尝试使用 R 中的 keras 在 LSTM 中使用批量标准化。在我的数据集中,目标/输出变量是Sales
列,并且数据集中的每一行都记录Sales
了一年(2008-2017)中的每一天。数据集如下所示:
我的目标是建立一个基于此类数据集的 LSTM 模型,该模型应该能够在训练结束时提供预测。我正在用 2008-2016 年的数据训练这个模型,并使用 2017 年数据的一半作为验证,其余的作为测试集。
以前,我尝试使用辍学和提前停止来创建模型。如下所示:
mdl1 <- keras_model_sequential()
mdl1 %>%
layer_lstm(units = 512, input_shape = c(1, 3), return_sequences = T ) %>%
layer_dropout(rate = 0.3) %>%
layer_lstm(units = 512, return_sequences = FALSE) %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 1, activation = "linear")
mdl1 %>% compile(loss = 'mse', optimizer = 'rmsprop')
模型如下所示
___________________________________________________________
Layer (type) Output Shape Param #
===========================================================
lstm_25 (LSTM) (None, 1, 512) 1056768
___________________________________________________________
dropout_25 (Dropout) (None, 1, 512) 0
___________________________________________________________
lstm_26 (LSTM) (None, 512) 2099200
___________________________________________________________
dropout_26 (Dropout) (None, 512) 0
___________________________________________________________
dense_13 (Dense) (None, 1) 513
===========================================================
Total params: 3,156,481
Trainable params: 3,156,481
Non-trainable params: 0
___________________________________________________________
为了训练模型,提前停止与验证集一起使用。
mdl1.history <- mdl1 %>%
fit(dt.tr, dt.tr.out, epochs=500, shuffle=F,
validation_data = list(dt.val, dt.val.out),
callbacks = list(
callback_early_stopping(min_delta = 0.000001, patience = 10, verbose = 1)
))
最重要的是,我想使用批量标准化来加速训练。根据我的理解,要使用批量归一化,我需要将数据分成批次,并申请layer_batch_normalization
每个隐藏层的输入。模型层如下所示:
batch_size <- 32
mdl2 <- keras_model_sequential()
mdl2 %>%
layer_batch_normalization(input_shape = c(1, 3), batch_size = batch_size) %>%
layer_lstm(units = 512, return_sequences = T) %>%
layer_dropout(rate = 0.3) %>%
layer_batch_normalization(batch_size = batch_size) %>%
layer_lstm(units = 512, return_sequences = F) %>%
layer_dropout(rate = 0.2) %>%
layer_batch_normalization(batch_size = batch_size) %>%
layer_dense(units = 1, activation = "linear")
mdl2 %>% compile(loss = 'mse', optimizer = 'rmsprop')
该模型如下所示:
______________________________________________________________________________
Layer (type) Output Shape Param #
==============================================================================
batch_normalization_34 (BatchNormalization) (32, 1, 3) 12
______________________________________________________________________________
lstm_27 (LSTM) (32, 1, 512) 1056768
______________________________________________________________________________
dropout_27 (Dropout) (32, 1, 512) 0
______________________________________________________________________________
batch_normalization_35 (BatchNormalization) (32, 1, 512) 2048
______________________________________________________________________________
lstm_28 (LSTM) (32, 1, 512) 2099200
______________________________________________________________________________
dropout_28 (Dropout) (32, 1, 512) 0
______________________________________________________________________________
batch_normalization_36 (BatchNormalization) (32, 1, 512) 2048
______________________________________________________________________________
dense_14 (Dense) (32, 1, 1) 513
==============================================================================
Total params: 3,160,589
Trainable params: 3,158,535
Non-trainable params: 2,054
______________________________________________________________________________
训练模型看起来像以前一样。唯一的区别在于训练和验证数据集,它们的大小是batch_size
(这里是 32)的倍数,通过从第二批到最后一批重新采样数据。
但是, 的性能比 的mdl1
要好得多mdl2
,如下所示。
我不确定我到底做错了什么,因为我从 keras(以及一般的实用神经网络)开始。此外,第一个模型的性能也不是很好;任何关于如何改进的建议也很好。