理解R中rnn模型的Keras预测输出

理解R中rnn模型的Keras预测输出,r,machine-learning,keras,lstm,recurrent-neural-network,R,Machine Learning,Keras,Lstm,Recurrent Neural Network,我在R中试用Keras软件包,通过预测温度来实现。然而,本教程没有解释如何使用经过训练的RNN模型进行预测,我想知道如何做到这一点。为了训练模型,我使用了从教程复制的以下代码: dir.create("~/Downloads/jena_climate", recursive = TRUE) download.file( "https://s3.amazonaws.com/keras-datasets/jena_climate_2009_2016.csv.zip", "~/Do

我在R中试用Keras软件包,通过预测温度来实现。然而,本教程没有解释如何使用经过训练的RNN模型进行预测,我想知道如何做到这一点。为了训练模型,我使用了从教程复制的以下代码:

dir.create("~/Downloads/jena_climate", recursive = TRUE)
download.file(
    "https://s3.amazonaws.com/keras-datasets/jena_climate_2009_2016.csv.zip",
      "~/Downloads/jena_climate/jena_climate_2009_2016.csv.zip"
    )
unzip(
  "~/Downloads/jena_climate/jena_climate_2009_2016.csv.zip",
  exdir = "~/Downloads/jena_climate"
)

library(readr)
data_dir <- "~/Downloads/jena_climate"
fname <- file.path(data_dir, "jena_climate_2009_2016.csv")
data <- read_csv(fname)

data <- data.matrix(data[,-1])

train_data <- data[1:200000,]
mean <- apply(train_data, 2, mean)
std <- apply(train_data, 2, sd)
data <- scale(data, center = mean, scale = std)

generator <- function(data, lookback, delay, min_index, max_index,
                      shuffle = FALSE, batch_size = 128, step = 6) {
  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,
                                dim(data)[[-1]]))
    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,]
      targets[[j]] <- data[rows[[j]] + delay,2]
    }            

    list(samples, targets)
  }
}

lookback <- 1440
step <- 6
delay <- 144
batch_size <- 128

train_gen <- generator(
  data,
  lookback = lookback,
  delay = delay,
  min_index = 1,
  max_index = 200000,
  shuffle = TRUE,
  step = step, 
  batch_size = batch_size
)

val_gen = generator(
  data,
  lookback = lookback,
  delay = delay,
  min_index = 200001,
  max_index = 300000,
  step = step,
  batch_size = batch_size
)

test_gen <- generator(
  data,
  lookback = lookback,
  delay = delay,
  min_index = 300001,
  max_index = NULL,
  step = step,
  batch_size = batch_size
)

# How many steps to draw from val_gen in order to see the entire validation set
val_steps <- (300000 - 200001 - lookback) / batch_size

# How many steps to draw from test_gen in order to see the entire test set
test_steps <- (nrow(data) - 300001 - lookback) / batch_size

library(keras)

model <- keras_model_sequential() %>% 
  layer_flatten(input_shape = c(lookback / step, dim(data)[-1])) %>% 
  layer_dense(units = 32, activation = "relu") %>% 
  layer_dense(units = 1)

model %>% compile(
  optimizer = optimizer_rmsprop(),
  loss = "mae"
)

history <- model %>% fit_generator(
  train_gen,
  steps_per_epoch = 500,
  epochs = 20,
  validation_data = val_gen,
  validation_steps = val_steps
)
另外,使用
keras::predict\u generator()
test\u gen()函数的正确方法是什么?如果我使用以下代码:

model %>% predict_generator(generator = test_gen,
                            steps = test_steps)
它给出了以下错误:

error in py_call_impl(callable, dots$args, dots$keywords) : 
 ValueError: Error when checking model input: the list of Numpy
 arrays that you are passing to your model is not the size the model expected. 
 Expected to see 1 array(s), but instead got the following list of 2 arrays: 
 [array([[[ 0.50394005,  0.6441838 ,  0.5990761 , ...,  0.22060473,
          0.2018686 , -1.7336458 ],
        [ 0.5475698 ,  0.63853574,  0.5890239 , ..., -0.45618412,
         -0.45030192, -1.724062...

注意:我对R的语法知之甚少,所以很遗憾,我无法使用R给出答案。相反,我在回答中使用了Python。我希望你能轻松地将我的话翻译回R。


。。。如果我是正确的,这应该给我一个标准化的预测 每批的温度

是的,没错。预测将被标准化,因为您已使用标准化标签对其进行了训练:


data感谢您抽出时间回答此问题。按照您的建议(在R中)-我发现这非常有用-我发现与Python相关的
predict\u generator
函数和
evaluate\u generator
都有错误。对于
predict\u generator
函数,错误显示为“ValueError:包含多个元素的数组的真值不明确。请使用a.any()或a.all()<代码>评估生成器(模型、测试生成、测试步骤)
给出“py\u call\u impl(可调用、dots$args、dots$keywords)中的错误:AttributeError:'str'对象没有属性'ndim'。有什么想法吗?Best@markus你提到的第二个错误。通过升级Keras包,问题似乎得以解决。尝试将Keras升级到最新版本,看看是否已解决。如果没有,请再次通知我,我会进行更多调查。在我将
keras
更新到2.2.0.9000版后,我在尝试拟合模型时遇到以下错误:“AttributeError:'str'对象没有属性'shape'”。我觉得很奇怪。再次降级到2.2.0版后,错误仍然存在。已创建要点。您可以在这里找到:。谢谢。@markus好吧,我今天学到了一些R:)你不需要使用命名列表。只需像以前一样使用普通的基于索引的列表。至于
pred_generator
的定义,我已经更新了我的答案,并包含了正确的方法。修改后,我测试了代码,它在我的机器上运行良好。顺便说一句,我的Keras软件包版本是2.2.0,TF版本是1.9,R版本是3.4.4。
error in py_call_impl(callable, dots$args, dots$keywords) : 
 ValueError: Error when checking model input: the list of Numpy
 arrays that you are passing to your model is not the size the model expected. 
 Expected to see 1 array(s), but instead got the following list of 2 arrays: 
 [array([[[ 0.50394005,  0.6441838 ,  0.5990761 , ...,  0.22060473,
          0.2018686 , -1.7336458 ],
        [ 0.5475698 ,  0.63853574,  0.5890239 , ..., -0.45618412,
         -0.45030192, -1.724062...