【问题标题】:LSTM network in R for time series predictionR中的LSTM网络用于时间序列预测
【发布时间】:2019-02-26 17:05:59
【问题描述】:

我有一个大小为 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)

我正在努力解决这个错误。有人知道这种建模的概念错误吗?提前致谢。

【问题讨论】:

    标签: r time-series lstm


    【解决方案1】:

    由于数据如此之少,您可能无法利用 LSTM 神经网络的优势。我建议您使用预测包中的 SARIMA 或 HW。如果您在滞后之间有一些非文学性,您还可以构建一个具有更相关滞后和使用傅立叶级数提取的季节性分量的数据集,并训练一个 randomForest 模型。

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

    我不是 LSTM 神经网络方面的专家,但下面的链接可能对您有所帮助:

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

    BR

    【讨论】:

      猜你喜欢
      • 2020-09-15
      • 1970-01-01
      • 2014-11-16
      • 2018-09-22
      • 2014-08-27
      • 2017-08-02
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      相关资源
      最近更新 更多