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

时间:2018-02-28 14:36:00

标签: r machine-learning keras lstm recurrent-neural-network

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

GameObject circle;

void Update(){
    if (circle != null) { //if the circle has been assigned, rotate it
        circle.transform.Rotate(cam.up, -Input.GetAxis("Mouse X") * rotationSpeed, Space.World);
    }
    else if (Input.GetMouseButtonDown(0)) {       //if clicked, assign the circle
        circle = this.GameObject.transform.parent;
    }
    if (circle != null && Input.GetMouseButtonUp(0)) { //supposed to snap to the right spot, this is the part that isn't working
        var rotate = circle.transform.rotation.y;
        var newRotate = Mathf.Round((rotate * 360) / 18) * 18;
        if (newRotate > 360) { newRotate -= 360; }
        else if (newRotate < 0) { newRotate += 360; }
        circle.transform.eulerAngles = new Vector3(0, newRotate, 0);
    }
}

我尝试使用下面的代码预测温度。如果我是正确的,这应该给我每批的标准化预测温度。因此,当我对值进行非规范化并对它们求平均值时,我会得到预测的温度。这是正确的,如果是,那么预测哪个时间(最近观察时间+ 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

此外,使用prediction.set <- test_gen()[[1]] prediction <- predict(model, prediction.set) keras::predict_generator()功能的正确方法是什么?如果我使用以下代码:

test_gen()

它给出了这个错误:

model %>% predict_generator(generator = test_gen,
                            steps = test_steps)

1 个答案:

答案 0 :(得分:4)

注意:我对R的语法了解很少,因此很遗憾,我无法使用R给您答案。相反,我在答案中使用Python。希望您能轻松地至少将我的话翻译回R。


  

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

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

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

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

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

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

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

  

此外,使用keras::predict_generator()的正确方法是   test_gen()函数?

predict_generator使用一个生成器,该生成器仅提供测试样本作为输出,而不提供标签(因为在执行预测时不需要标签;在训练时需要标签,即fit_generator(),以及评估模型时,即evaluate_generator())。这就是为什么错误提示您需要传递一个数组而不是两个数组的原因。因此,您需要定义一个仅提供测试样本的生成器,或者在Python中,将现有的生成器包装在仅提供输入样本的另一个函数中(我不知道是否可以在R中完成此操作) ):

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个步骤使用生成器。

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

loss = model.evaluate_generator(test_gen, number_of_steps)

给定的loss也已归一化,并对其进行归一化(以获得更好的预测误差),您只需将其乘以std(您无需添加{{1 }},因为您使用mean,即平均绝对误差作为损失函数):

mae

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


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

denorm_loss