【问题标题】:Reshaping Time Series Data with Multiple Features for RNNs使用 RNN 的多个特征重塑时间序列数据
【发布时间】:2019-04-05 22:02:38
【问题描述】:

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

我正在跟随Ch。 6 of Deep Learning with R by Chollet & Allaire,但他们的例子与我现在很困惑的数据集并没有太大的不同。我试图模仿本书那一章的代码,但无济于事。 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 模型的指导。

【问题讨论】:

    标签: r tensorflow keras deep-learning reshape


    【解决方案1】:

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

    1. 使用 reticulate 包中的 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的输出是一个数组类,但是k_reshape()输出的是一个tensorflow张量对象。两者都为我创建了深度学习网络,但我发现数组类更易于解释。

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 2019-07-31
      • 2019-10-12
      • 2018-06-30
      • 2020-04-11
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多