1

我正在尝试在一个只有 3000 多个样本的小型数据集上执行 1D-CNN。生成器的 Lookback 参数设置为 7。R 会话在几秒钟的训练后中止。

但是,当lookback 参数设置为7 以外的任何值(例如5、9 或10)时,1D-CNN 模型可以工作!我还尝试了 GRU、LSTM 和全连接层,将回溯参数设置为 7,一切正常。我的环境是休闲:R 3.6.1,Rstudio 1.5.5001,Tensorflow 2.0.0,Keras 2.2.5.0。代码如下所示:

lookback <- 7
step <- 1
delay <- 1
batch_size <- 32
nbfeature <- 31

generator_WeeklyTotal <- function(data, lookback, delay, min_index, max_index,
                      shuffle = FALSE, batch_size, 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, max_index))
      i <<- i + length(rows)
    }
    samples <- array(0, dim = c(length(rows),
                                lookback / step,
                                nbfeature))
    targets <- array(0, dim = c(length(rows)))
    for (j in 1:length(rows)) {
      indices <- seq(rows[[j]] - lookback, rows[[j]],
                     length.out = dim(samples)[[2]])
      samples[j,,] <- data[indices,60:90] 
      targets[[j]] <- data[rows[[j]] + delay,6]
    }
    list(samples, targets)
  }
}

train_gen <- generator_WeeklyTotal(
  data = DLtrain,
  lookback = lookback,
  delay = delay,
  min_index = 1+lookback,
  max_index = 2922+lookback,
  shuffle = FALSE,
  step = step,
  batch_size = batch_size
)

val_gen = generator_WeeklyTotal(
  data = DLtrain,
  lookback = lookback,
  delay = delay,
  min_index = 2923+lookback, 
  max_index = 3287+lookback, 
  step = step,
  batch_size = batch_size
)

test_gen <- generator_WeeklyTotal(
  data = DLtrain,
  lookback = lookback,
  delay = delay,
  min_index = 3288+lookback,
  max_index = 3652+lookback,
  step = step,
  batch_size = batch_size
)

val_steps <- (3287+lookback - 2923+lookback - lookback) / batch_size
test_steps <- (3652+lookback - 3288+lookback - lookback) / batch_size

model_conv1d <- keras_model_sequential() %>%
  layer_conv_1d(filters = 64, kernel_size = 5, activation = "relu", padding = "same",
                input_shape = list(NULL, nbfeature)) %>%
  layer_max_pooling_1d(pool_size = 3) %>%
  layer_conv_1d(filters = 64, kernel_size = 5, activation = "relu",padding = "same") %>%
  layer_max_pooling_1d(pool_size = 3) %>%
  layer_conv_1d(filters = 64, kernel_size = 5, activation = "relu",padding = "same") %>%
  layer_global_max_pooling_1d() %>%
  layer_dense(units = 1)]

我想知道为什么当我将回顾参数设置为 7 但使用其他值时 R 会话中止?有什么办法可以解决这个问题,因为需要将lookback设置为7,以便使用每周数据进行预测。

4

0 回答 0