了解R中rnn模型的Keras预测输出 [英] Understanding Keras prediction output of a rnn model in R

查看:97
本文介绍了了解R中rnn模型的Keras预测输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过以下方法试用R中的Keras软件包:指南有关预测温度的信息.但是,本教程没有解释如何使用经过训练的RNN模型进行预测,并且我想知道如何执行此操作.为了训练模型,我使用了从教程复制的以下代码:

I'm trying out the Keras package in R by doing this tutorial about forecasting the temperature. However, the tutorial has no explanation on how to predict with the trained RNN model and I wonder how to do this. To train a model I used the following code copied from the tutorial:

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
)

我尝试使用以下代码预测温度.如果我是正确的话,这应该为我提供每批次的标准化预测温度.因此,当我对这些值进行归一化并取平均值时,便得到了预测的温度.这是正确的吗?如果是这样,那么可以预测哪个时间(最新观察时间+ delay?)?

I tried to predict the temperature with the code below. If I am correct, this should give me the normalized predicted temperature for every batch. So when I denormalize the values and average them, I get the predicted temperature. Is this correct and if so for which time is then predicted (latest observation time + delay?) ?

prediction.set <- test_gen()[[1]]
prediction <- predict(model, prediction.set)

此外,使用keras::predict_generator()test_gen()函数的正确方法是什么?如果我使用以下代码:

Also, what is the correct way to use keras::predict_generator() and the test_gen() function? If I use the following code:

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.

...如果我是正确的,这应该给我归一化的预测 每批温度.

... If I am correct, this should give me the normalized predicted temperature for every batch.

是的,没错.因为您已经使用标准化标签训练了预测,所以预测将被标准化:

Yes, that's right. The predictions would be normalized since you have trained it with normalized labels:

data <- scale(data, center = mean, scale = std)

因此,您需要使用计算出的均值和std对值进行归一化以找到真实的预测:

Therefore, you would need to denormalize the values using the computed mean and std to find the real predictions:

pred = model.predict(test_data)
denorm_pred = pred * std + mean

...然后为其预测时间(最新观察时间+ 延迟?)

... for which time is then predicted (latest observation time + delay?)

是的.具体而言,由于在此特定数据集中每10分钟记录一次新的观测值,并且已设置delay=144,因此这意味着预测值是距目标位置24小时前的温度(即144 * 10 = 1440分钟= 24小时).最后给出的观察结果.

That's right. Concretely, since in this particular dataset every ten minutes a new obeservation is recorded and you have set delay=144, it would mean that the predicted value is the temperature 24 hours ahead (i.e. 144 * 10 = 1440 minutes = 24 hours) from the last given observation.

此外,使用keras::predict_generator()test_gen()函数?

predict_generator 使用一个生成器,该生成器仅作为输出进行测试样本而不是标签(因为在执行预测时我们不需要标签;在训练时需要标签,即

predict_generator takes a generator that gives as output only test samples and not the labels (since we don't need labels when we are performing prediction; the labels are needed when training, i.e. fit_generator(), and when evaluating the model, i.e. evaluate_generator()). That's why the error mentions that you need to pass one array instead of two arrays. So you need to define a generator that only gives test samples or one alternative way, in Python, is to wrap your existing generator inside another function that gives only the input samples (I don't know whether you can do this in R or not):

def pred_generator(gen):
    for data, labels in gen:
        yield data  # discards labels

preds = model.predict_generator(pred_generator(test_generator), number_of_steps)

您需要提供另一个参数,该参数是生成器涵盖测试数据中所有样本的步骤数.实际上,我们有num_steps = total_number_of_samples / batch_size.例如,如果您有1000个样本,并且每次生成器生成10个样本,则需要对1000 / 10 = 100步骤使用生成器.

You need to provide one other argument which is the number of steps of generator to cover all the samples in test data. Actually we have num_steps = total_number_of_samples / batch_size. For example, if you have 1000 samples and each time the generator generate 10 samples, you need to use generator for 1000 / 10 = 100 steps.

奖金:要查看模型的效果如何,可以使用evaluate_generator并使用现有的测试生成器(即test_gen):

Bonus: To see how good your model performs you can use evaluate_generator using the existing test generator (i.e. test_gen):

loss = model.evaluate_generator(test_gen, number_of_steps)

给定的loss也会被规范化和去规范化(以获得更好的预测误差),您只需将其乘以std(因为您正在使用,因此无需添加mean mae,即平均绝对误差,作为损失函数):

The given loss is also normalized and to denormalize it (to get a better sense of prediction error) you just need to multiply it by std (you don't need to add mean since you are using mae, i.e. mean absolute error, as the loss function):

denorm_loss = loss * std

这将告诉您平均而言您的预测有多少偏离.例如,如果您要预测温度,则denorm_loss为5表示预测平均偏离5度(即小于或大于实际值).

This would tell you how much your predictions are off on average. For example, if you are predicting the temperature, a denorm_loss of 5 means that the predictions are on average 5 degrees off (i.e. are either less or more than the actual value).

更新:对于预测,您可以使用R中的现有生成器来定义新生成器,如下所示:

Update: For prediction, you can define a new generator using an existing generator in R like this:

pred_generator <- function(gen) {
  function() { # wrap it in a function to make it callable
    gen()[1]  # call the given generator and get the first element (i.e. samples)
  }
}

preds <- model %>% 
  predict_generator(
    generator = pred_generator(test_gen), # pass test_gen directly to pred_generator without calling it
    steps = test_steps
  )

evaluate_generator(model, test_gen, test_steps)

这篇关于了解R中rnn模型的Keras预测输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆