Neural network 时间序列的卷积神经网络

Neural network 时间序列的卷积神经网络,neural-network,Neural Network,我想知道是否存在一个代码来训练卷积神经网络来进行时间序列分类 我看过一些最近的论文(),但我不确定是否存在某种东西,或者我是否自己编写了代码。完全可以使用CNN进行时间序列预测,无论是回归还是分类。CNN擅长发现局部模式,事实上CNN的工作假设是局部模式在任何地方都是相关的。卷积也是时间序列和信号处理中众所周知的运算。相对于RNN的另一个优势是,它们可以非常快地计算,因为它们可以并行化,而不是RNN的顺序性 在下面的代码中,我将演示一个案例研究,其中可以使用KERA预测R的电力需求。请注意,这不

我想知道是否存在一个代码来训练卷积神经网络来进行时间序列分类


我看过一些最近的论文(),但我不确定是否存在某种东西,或者我是否自己编写了代码。

完全可以使用CNN进行时间序列预测,无论是回归还是分类。CNN擅长发现局部模式,事实上CNN的工作假设是局部模式在任何地方都是相关的。卷积也是时间序列和信号处理中众所周知的运算。相对于RNN的另一个优势是,它们可以非常快地计算,因为它们可以并行化,而不是RNN的顺序性

在下面的代码中,我将演示一个案例研究,其中可以使用KERA预测R的电力需求。请注意,这不是一个分类问题(我手头没有示例),但修改代码以处理分类问题并不困难(使用softmax输出,而不是线性输出和交叉熵损失)

数据集在fpp2库中可用:

library(fpp2)
library(keras)

data("elecdemand")

elec <- as.data.frame(elecdemand)

dm <- as.matrix(elec[, c("WorkDay", "Temperature", "Demand")])
库(fpp2)
图书馆(keras)
数据(“电子需求”)

elec此网站需要凭据才能阅读呈现的pdf。请考虑在您的问题张贴凭证或提供凭据或托管文件在一些自由的位置,非常感谢您的伟大答案。只是想知道是否有可能将lstm和cnn连接在一起?你对此有什么想法吗?:)绝对地在keras中查看此教程:非常感谢。本教程很有趣,也是一个很好的起点:)
data_gen <- function(dm, batch_size, ycol, lookback, lookahead) {

  num_rows <- nrow(dm) - lookback - lookahead
  num_batches <- ceiling(num_rows/batch_size)
  last_batch_size <- if (num_rows %% batch_size == 0) batch_size else num_rows %% batch_size
  i <- 1
  start_idx <- 1
  return(function(){
    running_batch_size <<- if (i == num_batches) last_batch_size else batch_size
    end_idx <- start_idx + running_batch_size - 1
    start_indices <- start_idx:end_idx

    X_batch <- array(0, dim = c(running_batch_size,
                                lookback,
                                ncol(dm)))
    y_batch <- array(0, dim = c(running_batch_size, 
                                length(ycol)))

    for (j in 1:running_batch_size){
      row_indices <- start_indices[j]:(start_indices[j]+lookback-1)
      X_batch[j,,] <- dm[row_indices,]
      y_batch[j,] <- dm[start_indices[j]+lookback-1+lookahead, ycol]
    }
    i <<- i+1
    start_idx <<- end_idx+1 
    if (i > num_batches){
      i <<- 1
      start_idx <<- 1
    }

    list(X_batch, y_batch)

  })
}
lookback <- 72
lookahead <- 1
batch_size <- 168
ycol <- 3
val_dm <- dm[15001:16000,]
test_dm <- dm[16001:nrow(dm),]

train_gen <- data_gen(
  train_dm,
  batch_size = batch_size,
  ycol = ycol,
  lookback = lookback,
  lookahead = lookahead
)


val_gen <- data_gen(
  val_dm,
  batch_size = batch_size,
  ycol = ycol,
  lookback = lookback,
  lookahead = lookahead
)
model <- keras_model_sequential() %>%
  layer_conv_1d(filters=64, kernel_size=4, activation="relu", input_shape=c(lookback, dim(dm)[[-1]])) %>%
  layer_max_pooling_1d(pool_size=4) %>%
  layer_flatten() %>%
  layer_dense(units=lookback * dim(dm)[[-1]], activation="relu") %>%
  layer_dropout(rate=0.2) %>%
  layer_dense(units=1, activation="linear")


model %>% compile(
  optimizer = optimizer_rmsprop(lr=0.001),
  loss = "mse",
  metric = "mae"
)

val_steps <- 48

history <- model %>% fit_generator(
  train_gen,
  steps_per_epoch = 50,
  epochs = 50,
  validation_data = val_gen,
  validation_steps = val_steps
)
####### How to create predictions ####################

#We will create a predict_forecast function that will do the following: 
#The function will be given a dataset that will contain weather forecast values and Demand values for the lookback duration. The rest of the MW values will be non-available and 
#will be "filled-in" by the deep network (predicted). We will do this with the test_dm dataset.

horizon <- 24

#Store all target values in a vector
goal_predictions <- test_dm[1:(lookback+horizon),ycol]
#get a copy of the dm_test
test_set <- test_dm[1:(lookback+horizon),]
#Set all the Demand values, except the lookback values, in the test set to be equal to NA.
test_set[(lookback+1):nrow(test_set), ycol] <- NA

predict_forecast <- function(model, test_data, ycol, lookback, horizon) {
  i <-1
  for (i in 1:horizon){
    start_idx <- i
    end_idx <- start_idx + lookback - 1
    predict_idx <- end_idx + 1
    input_batch <- test_data[start_idx:end_idx,]
    input_batch <- input_batch %>% array_reshape(dim = c(1, dim(input_batch)))
    prediction <- model %>% predict_on_batch(input_batch)
    test_data[predict_idx, ycol] <- prediction
  }

  test_data[(lookback+1):(lookback+horizon), ycol]
}

preds <- predict_forecast(model, test_set, ycol, lookback, horizon)

targets <- goal_predictions[(lookback+1):(lookback+horizon)]

pred_df <- data.frame(x = 1:horizon, y = targets, y_hat = preds)