在 R 中使用 RNN (Keras) 进行时间序列预测

Posted

技术标签:

【中文标题】在 R 中使用 RNN (Keras) 进行时间序列预测【英文标题】:Time Series prediction using RNNs (Keras) in R 【发布时间】:2018-12-23 15:23:25 【问题描述】:

我正在关注 Chollet 的 Deep learning with R 方法 (fitting RNNs to time series data) 来拟合 RNN 以进行时间序列预测。

model <- keras_model_sequential() %>% 
  layer_gru(units = 32, 
            dropout = 0.1, 
            recurrent_dropout = 0.5,
            return_sequences = TRUE,
            input_shape = list(NULL, dim(data)[[-1]])) %>% 
  layer_gru(units = 64, activation = "relu",
            dropout = 0.1,
            recurrent_dropout = 0.5) %>% 
  layer_dense(units = 1)

model %>% compile(
  optimizer = optimizer_rmsprop(),
  loss = "mae"
)

history <- model %>% fit_generator(
  train_gen,
  steps_per_epoch = 500,
  epochs = 40,
  validation_data = val_gen,
  validation_steps = val_steps
)

在这里,训练、验证和测试数据是使用以下方法生成的:

lookback <- 1440
step <- 6
delay <- 144
batch_size <- 128

train_gen <- generator(
  data,
  lookback = lookback,
  delay = delay,
  min_index = 1,
  max_index = 200000,
  shuffle = TRUE,
  step = step, 
  batch_size = batch_size
)

val_gen = generator(
  data,
  lookback = lookback,
  delay = delay,
  min_index = 200001,
  max_index = 300000,
  step = step,
  batch_size = batch_size
)

test_gen <- generator(
  data,
  lookback = lookback,
  delay = delay,
  min_index = 300001,
  max_index = NULL,
  step = step,
  batch_size = batch_size
)

# How many steps to draw from val_gen in order to see the entire validation set
val_steps <- (300000 - 200001 - lookback) / batch_size

# How many steps to draw from test_gen in order to see the entire test set
test_steps <- (nrow(data) - 300001 - lookback) / batch_size

在此之后,我阅读了 Keras 文档并找到了预测功能。要查找测试数据的预测:

m <- model %>% evaluate_generator(test_gen, steps = test_steps)
m

但是,它只给出测试数据的损失值。

我的问题是,如何获得测试数据集中每个点的预测,就像我们可以在其他时间序列方法中获得的一样?如何绘制这些预测值和实际值?

【问题讨论】:

Understanding Keras prediction output of a rnn model in R的可能重复 是的,使用predict_generator,而不是evaluate_generator 【参考方案1】:

在我看来,您需要重新定义generator,您只需要获取samples 作为输出。按照你的例子:

# generator function
generator <- function(data, lookback, delay, min_index, max_index,
                      shuffle = FALSE, batch_size = 128, step = 6) 
  if (is.null(max_index))
    max_index <- nrow(data) - delay - 1
  i <- min_index + lookback
  function() 
    if (shuffle) 
      rows <- sample(c((min_index+lookback):max_index), size = batch_size)
     else 
      if (i + batch_size >= max_index)
        i <<- min_index + lookback
      rows <- c(i:min(i+batch_size-1, max_index))
      i <<- i + length(rows)
    

    samples <- array(0, dim = c(length(rows), 
                                lookback / step,
                                dim(data)[[-1]]))
    targets <- array(0, dim = c(length(rows)))

    for (j in 1:length(rows)) 
      indices <- seq(rows[[j]] - lookback, rows[[j]]-1, 
                     length.out = dim(samples)[[2]])
      samples[j,,] <- data[indices,]
      targets[[j]] <- data[rows[[j]] + delay,2]
                

    list(samples) # just the samples, (quick and dirty solution, I just removed targets)
  


# test_gen is the same
test_gen <- generator(
  data,
  lookback = lookback,
  delay = delay,
  min_index = 300001,
  max_index = NULL,
  step = step,
  batch_size = batch_size
)

现在您可以拨打predict_generator:

preds <- model %>% predict_generator(test_gen, steps = test_steps)

但现在你需要去规范化这些,因为你在拟合之前缩放了每个变量。

denorm_pred = preds * std + mean

注意stdmean 应该在T (degC) 上计算train 数据上,否则你会过拟合。

【讨论】:

以上是关于在 R 中使用 RNN (Keras) 进行时间序列预测的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 keras RNN 在数据集中进行文本分类?

使用Keras与LSTM和RNN进行斗争

Keras 如何处理单元格和隐藏状态(RNN、LSTM)的初始值以进行推理?

Tensorflow.keras:RNN 对 Mnist 进行分类

使用 Keras、Tensorflow 进行具有多个时间序列维度的 RNN 时间序列预测

关于在 RNN (Keras) 中正确使用 dropout