R中的Keras LSTM:如何确保正确的输入尺寸

时间:2019-04-24 16:15:48

标签: r keras lstm

我试图在R中运行lstm模型。我从这里获取了代码: https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/ 在python中工作正常,并尝试使其适应R。运行fit过程时,出现以下错误消息:

  

py_call_impl(可调用,dots $ args,dots $ keywords)错误:   ValueError:检查输入时出错:预期lstm_6_input具有3维,但数组的形状为(400,1)

我已经读到问题可能在于输入尺寸,但R中的输入尺寸与python中的相同,即X数据为400x1x8,y数据为400x1x8。 我还读到问题可能出在输出尺寸上,并且在图层定义中指定return_sequences=FALSE可能会有所帮助,但是我仍然得到相同的错误消息。
我在MachineLearningMastery网站上翻译了该代码后,其中包含以下代码,但起初没有进行初始数据清理和保存的步骤,因此请缩短代码。

rm(list=ls())
library(caret)
library(dplyr)
library(data.table)
library(keras)

source("SeriesToSupervised.R") #included at end of code
dataset <- read.table(fn, stringsAsFactors=F, comment.char="",row.names = NULL,sep=",",header=TRUE)
#label encoding windspeed 
dataset$wnd_dir_num <- 0
dataset$wnd_dir_num[dataset$wnd_dir=="NW"]<-1
dataset$wnd_dir_num[dataset$wnd_dir=="SE"]<-2
dataset$wnd_dir_num[dataset$wnd_dir=="cv"]<-3

DataToUse <- dataset[,c(2:5,10,7:9)]
rownames(DataToUse) <- dataset[,1]
DataToUse$dew <- as.numeric(DataToUse$dew)
DataToUse$snow <- as.numeric(DataToUse$snow)
DataToUse$rain <- as.numeric(DataToUse$rain)

#center and scale - used Caret here, rather than Python routines
preProcValues <- preProcess(DataToUse, method = c("center", "scale"))
scaled <- predict(preProcValues, DataToUse)

#frame as supervised learning
n_out <- 1; n_in <- 1
reframed <- SeriesToSupervised(scaled, n_in=n_in, n_out=n_out, dropnan=T)
# drop columns we don't want to predict
reframed <- reframed[,(1:(n_in*ncol(scaled)+1))]

#make training/test split
nVar <- ncol(reframed)-1
train_X <- reframed[1:400,1:nVar] #just a small sample to get things working - can expand later
test_X <- reframed[401:600,1:nVar]
train_y <- reframed[1:400,1]
test_y <- reframed[401:600,1]

# reshape input to be 3D [samples, timesteps, features]
train_X <- array(data=train_X,dim=c(nrow(train_X),n_in,nVar))
test_X <- array(test_X,dim=c(nrow(test_X),n_in,nVar))

# design network
model <- keras_model_sequential()
model %>%
  layer_lstm(units = 32,
             input_shape = c(n_in,nVar)) %>%
  layer_dense(units = 1)
model %>% compile(loss = 'mae', optimizer = 'adam')

# fit network
history <- model %>% fit(
  x=train_X, 
  y=train_y,
  epochs = 20, 
  batch_size = 72, 
  validation_data = list(test_X, test_y), 
  shuffle=FALSE,
)
SeriesToSupervised <- function(data, n_in=1, n_out=1, dropnan=True){
  #n_vars = 1 if type(data) is list else data.shape[1]
  n_vars <- ncol(data)
  cols <- list()
  MyNames <- character(0)
  i<-n_in
  while (i > 0){
    cols[[i]] <- data.frame(shift(data, n=i))
    for (j in 1:n_vars){
      MyNames <- c(MyNames, paste0("var",j,"(t-",i,")"))
    }
    i<-i-1
  }
  for (i in 1:n_out){
    cols[[length(cols)+1]] <- data.frame(shift(data, n=(i-1)))
    for (j in 1:n_vars){
      if (i==1){
        MyNames <- c(MyNames, paste0("var",j,"(t)"))
      } else {
        MyNames <- c(MyNames, paste0("var",j,"(t+",i,")"))
      }
    }
  }
  agg <- bind_cols(cols) # put it all together
  colnames(agg) <- MyNames

  if (dropnan){ # drop rows with NaN values
    agg <- agg[complete.cases(agg),]
  }
}

有人可以让我知道如何做才能在R中启动并运行这个简单的lstm吗?

0 个答案:

没有答案