【问题标题】:Generator for Time Series Analysis using RNN使用 RNN 进行时间序列分析的生成器
【发布时间】:2018-11-11 08:39:42
【问题描述】:

我对使用循环神经网络进行时间序列分析的生成器函数有疑问。我有一个包含 5 个不同 CDS 报价的数据集。我想在多输入/多输出网络中使用递归神经网络分析这些。 5 个引号作为输入,5 个引号作为输出。

因此,我有一个生成器,可以将多个输入转换为一个输出,但我无法为我的目的更改此代码。

Lookback 是网络应该回溯多远, 延迟是要预测的时间范围,并且 step 是 1,因为我有每日数据,而没有更深入的每小时或分钟数据。 使用索引可以决定子集中的哪些行(训练、验证、测试)。

代码如下:

generator <- function(data, lookback, delay, min_index, max_index,
                  shuffle = FALSE, batch_size = 128, step = 1) {
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, 1]
    }            

    list(samples, targets)
}

}

希望有人可以帮助我解决这个问题,或者提供一些其他有用的链接来构建具有时间序列财务数据的 RNN。

感谢您的帮助

【问题讨论】:

    标签: r dataframe time-series rnn


    【解决方案1】:

    好的, 看来我自己找到了解决方案。 而不是

    targets <- array(0, dim = c(length(rows)))
    

    这是一个向量,我用过

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

    现在是一个矩阵。

    然后直接写

    targets[j,] <- data[rows[[j]] + delay, ]
    

    而不是

    targets[[j]] <- data[rows[[j]] + delay, 1]
    

    此生成器为多个输出创建 3D 张量。

    【讨论】:

      猜你喜欢
      • 2017-05-12
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多