使用 RNN 的多个特征重塑时间序列数据

Reshaping Time Series Data with Multiple Features for RNNs

我有一个包含 3 个测量变量和大约 2000 个样本的时间序列数据集。我想使用 RNN 或在 R 中使用 Keras 的 1D CNN 模型将样本分类为 4 个类别中的 1 个。我的问题是我无法使用 k_reshape() 函数成功重塑模型。

我正在沿着Ch。 Chollet 和 Allaire 的 Deep Learning with R 中的 6 个,但它们的示例与我现在感到困惑的数据集差别不大。我试图模仿本书那一章的代码,但无济于事。 Here's a link to the source code for the chapter.

library(keras)

df <- data.frame()
for (i in c(1:20)) {
    time <- c(1:100)
    var1 <- runif(100)
    var2 <- runif(100)
    var3 <- runif(100)
    run <- data.frame(time, var1, var2, var3)
    run$sample <- i
    run$class <- sample(c(1:4), 1)
    df <- rbind(df, run)
}

head(df)

# time  feature1  feature2     feature3 sample class
#     1 0.4168828 0.1152874 0.0004415961      1     4
#     2 0.7872770 0.2869975 0.8809415097      1     4
#     3 0.7361959 0.5528836 0.7201276931      1     4
#     4 0.6991283 0.1019354 0.8873193581      1     4
#     5 0.8900918 0.6512922 0.3656302236      1     4
#     6 0.6262068 0.1773450 0.3722923032      1     4

k_reshape(df, shape(10, 100, 3))

# Error in py_call_impl(callable, dots$args, dots$keywords) : 
#   TypeError: Failed to convert object of type <class 'dict'> to Tensor. Contents: {'time': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3 

我对重塑数组还很陌生,但我想要一个具有以下形状的数组:(samples, time, features)。我很想听听有关如何正确重塑此数组的建议,或有关如何针对 DL 模型处理此数据的指导,如果我在这方面不符合要求的话。

我找到了两个解决问题的方法。我的困惑源于 k_reshape 的错误消息,我不明白如何解释。

  1. 使用网状包中的 array_reshape() 函数。
  2. 使用来自 keras 的 k_reshape() 函数,但这次使用适当的形状。

这是我成功执行的代码:

# generate data frame
dat <- data.frame()
for (i in c(1:20)) {
        time <- c(1:100)
        var1 <- runif(100)
        var2 <- runif(100)
        var3 <- runif(100)
        run <- data.frame(time, var1, var2, var3)
        run$sample <- i
        run$class <- sample(c(1:4), 1)
        dat <- rbind(df, run)
}

dat_m <- as.matrix(df) # convert data frame to matrix

# time  feature1  feature2     feature3 sample class
#     1 0.4168828 0.1152874 0.0004415961      1     4
#     2 0.7872770 0.2869975 0.8809415097      1     4
#     3 0.7361959 0.5528836 0.7201276931      1     4
#     4 0.6991283 0.1019354 0.8873193581      1     4
#     5 0.8900918 0.6512922 0.3656302236      1     4
#     6 0.6262068 0.1773450 0.3722923032      1     4

# solution with reticulate's array_reshape function
dat_array <- reticulate::array_reshape(x = dat_m[,c(2:4)], dim = c(20, 100, 3))

dim(dat_array)
# [1]  20 100   3

class(dat_array)

# [1] "array"


# solution with keras's k_reshape
dat_array_2 <- keras::k_reshape(x = dat_m[,c(2:4)], shape = c(20, 100, 3))

dim(dat_array)
# [1]  20 100   3

class(dat_array)

# [1]  20 100   3

class(dat_array_2)

# [1] "tensorflow.tensor"  "tensorflow.python.framework.ops.Tensor"     
# [3] "tensorflow.python.framework.ops._TensorLike" "python.builtin.object"   

一些注意事项:

  • 从概念上讲,这种重塑对我来说更有意义,因为用 R 语言来说是数据的转换或传播。
  • array_reshape的输出是数组class,但是k_reshape()输出的是tensorflow张量对象。两者都在创建深度学习网络中为我工作,但我发现数组 class 更易于解释。