R中的LSTM网络用于时间序列预测

时间:2018-09-22 15:25:26

标签: r time-series lstm

我有一个大小为64的单变量每月时间序列。我想使用剩余的月份作为训练集,进行多步预测-最近三个月的值(266、286和230)。

data <- c(113,  55,  77, 114,  73,  72,  75, 135,  84,  66, 167, 93,  83, 
          164,  76,  97, 148,  74,  76, 173,  70,  86, 167,  37,   1,  49,  
          48,37, 117, 178, 167, 177, 295, 167, 224, 225, 198, 217, 220, 175, 
          360, 289, 209, 369, 287, 249, 336, 219, 288, 248, 370, 296, 337, 
          246, 377, 324, 288, 367, 309, 128, 382, 266, 286, 230)

为了对LSTM网络建模,我正在通过以下方式调整训练/测试数据:

X_train = [55,6,1] # 6 timesteps (t-6,t-5,t-4,t-3,t-2,t-1)
Y_train = [55,3,1] # forecast horizon (t+1,t+2,t+3)
X_test  = [1,6,1]  
Y_test  = [1,3,1]

但是,当我按如下所示设置LSTM时,会出现错误

Error in py_call_impl(callable, dots$args, dots$keywords) : 
ValueError: Error when checking target: expected time_distributed_16 to have 
shape (6, 1) but got array with shape (3, 1)

LSTM模型

model <- keras_model_sequential()

model %>%
  layer_lstm(
              units = 32, 
              batch_input_shape  = c(1, 6, 1),
              dropout = 0.2,
              recurrent_dropout = 0.2,
              return_sequences = TRUE
  ) %>% time_distributed(layer_dense(units = 1))

  model %>%
      compile(loss = FLAGS$loss, optimizer = optimizer, metrics = 
              list("mean_squared_error"))

  history <- model %>% fit(x = X_train,
                           y = Y_train,
                           batch_size = 1,
                           epochs = 100,
                           callbacks = callbacks)

我正在为这个错误而苦苦挣扎。有人知道这种建模的概念性错误吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

只有很少的数据,您可能不会利用LSTM神经网络的好处。我建议您使用预测软件包中的SARIMA或HW。如果滞后之间存在一些非文学性,您还可以建立一个具有更相关滞后的数据集,并使用傅立叶级数提取季节成分并训练一个randomForest模型。

关于您的问题,我认为您的数组没有正确的尺寸,因此您需要调整它们的形状。

我不是LSTM神经网络专家,但是下面的链接可能会为您提供帮助:

https://blogs.rstudio.com/tensorflow/posts/2018-06-25-sunspots-lstm/

BR

相关问题