简体   繁体   English

Keras LSTM和多输入功能:如何定义参数

[英]Keras LSTM and multiple input feature: how to define parameters

I am discoveting Keras in R and the LSTM. 我正在研究R和LSTM中的Keras。 Following this blog post , I want to predict time series, and I would like to use various past time point (t-1, t-2) to predict the t point. 在这篇博客文章之后 ,我想预测时间序列,并且我想使用过去的各种时间点(t-1,t-2)来预测t点。 Here is what I tried so far: 这是我到目前为止尝试过的:

library(data.table)
library(tensorflow)
library(keras)

Serie <- c(5.66333333333333, 5.51916666666667, 5.43416666666667, 5.33833333333333, 
           5.44916666666667, 6.2025, 6.57916666666667, 6.70666666666667, 
           6.95083333333333, 8.1775, 8.55083333333333, 8.42166666666667, 
           8.01333333333333, 8.99833333333333, 11.0025, 10.3116666666667, 
           10.51, 10.9916666666667, 10.6116666666667, 10.8475, 13.7841666666667, 
           16.2916666666667, 15.9975, 14.3683333333333, 13.4041666666667, 
           11.8666666666667, 9.11916666666667, 9.47862416666667, 9.08404666666667, 
           8.79606166666667, 9.93211091666667, 9.03834041666667, 8.58787275, 
           6.77499383333333, 7.21377583333333, 7.53497175, 6.31212966666667, 
           5.5825105, 4.64021041666667, 4.608787, 5.39446983333333, 4.93945983333333, 
           4.8612215, 4.13088808333333, 4.09916575, 3.40943183333333, 3.79573258333333, 
           4.30319966666667, 4.23431266666667, 3.64880758333333, 3.11700716666667, 
           3.321058, 2.53599408333333, 2.20433991666667, 1.66643905833333, 
           0.84187275, 0.467880658333333, 0.810507858333333, 0.795)

Npoints <- 2 # number of previous point to take into account

I then create a data frame with the lagged time series, and create a test and train set: 然后,我创建一个具有滞后时间序列的数据框,并创建一个测试和训练集:

supervised <- data.table(x = diff(Serie, differences = 1))
supervised[,c(paste0("x-",1:Npoints)) := lapply(1:Npoints,function(i){c(rep(NA,i),x[1:(.N-i)])})] # create shifted versions

# take the non NA
supervised <- supervised[!is.na(get(paste0("x-",Npoints)))]
head(supervised)

# Split dataset into training and testing sets
N = nrow(supervised)
n = round(N *0.7, digits = 0)
train = supervised[1:n, ]
test  = supervised[(n+1):N,  ]

I rescale the data 我重新缩放数据

scale_data = function(train, test, feature_range = c(0, 1)) {
  x = train
  fr_min = feature_range[1]
  fr_max = feature_range[2]
  std_train = ((x - min(x,na.rm = T) ) / (max(x,na.rm = T) - min(x,na.rm = T)  ))
  std_test  = ((test - min(x,na.rm = T) ) / (max(x,na.rm = T) - min(x,na.rm = T)  ))
  scaled_train = std_train *(fr_max -fr_min) + fr_min
  scaled_test = std_test *(fr_max -fr_min) + fr_min
  return( list(scaled_train = as.vector(scaled_train), scaled_test = as.vector(scaled_test) ,scaler= c(min =min(x,na.rm = T), max = max(x,na.rm = T))) )
}

Scaled = scale_data(train, test, c(-1, 1))

# define x and y train
y_train = as.vector(Scaled$scaled_train[, 1]) 
x_train = Scaled$scaled_train[, -1] 

And following this post I reshape the data in 3D 这篇文章之后,我将重塑3D数据

x_train_reshaped <- array(NA,dim= c(1,dim(x_train)))
x_train_reshaped[1,,] <- as.matrix(x_train)

I do the following model and try to start the learning : 我使用以下模型并尝试开始学习:

model <- keras_model_sequential() 
model%>%
  layer_lstm(units = 1, batch_size = 1, input_shape = dim(x_train), stateful= TRUE)%>%
  layer_dense(units = 1)

# compile model ####
model %>% compile(
  loss = 'mean_squared_error',
  optimizer = optimizer_adam( lr= 0.02, decay = 1e-6 ),  
  metrics = c('accuracy')
)

# make a test
model %>% fit(x_train_reshaped, y_train, epochs=1, batch_size=1, verbose=1, shuffle=FALSE)

but I get the following error: 但我收到以下错误:

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: No data provided for "dense_11". py_call_impl(可调用,dots $ args,dots $ keywords)中的错误:ValueError:没有为“ dense_11”提供数据。 Need data for each key in: ['dense_11'] 需要每个键中的数据:['dense_11']

Trying to reshape the data differently didn't help. 尝试以不同的方式重塑数据无济于事。 What I am doing wrong ? 我做错了什么?

Keras and tensorflow in R cannot recognise the size of your input/target data when they are data frames. 当它们是数据帧时,R中的Keras和tensorflow无法识别输入/目标数据的大小。

y_train is both a data.table and a data.frame: y_train既是data.table又是data.frame:

class(y_train)
[1] "data.table" "data.frame"

The keras fit documentation states: "y: Vector, matrix, or array of target (label) data (or list if the model has multiple outputs)." keras fit文档指出:“ y:目标(标签)数据的向量,矩阵或数组(如果模型具有多个输出,则列出)。” Similarly, for x. 同样,对于x。

Unfortunately, there still appears to be an input and/or target dimensionality mismatch when y_train is cast to a matrix: 不幸的是,将y_train强制转换为矩阵时,似乎仍然存在输入和/或目标尺寸不匹配的情况:

model %>% 
 fit(x_train_reshaped, as.matrix(y_train), epochs=1, batch_size=1, verbose=1, shuffle=FALSE)
Error in py_call_impl(callable, dots$args, dots$keywords) :
  ValueError: Input arrays should have the same number of samples as target arrays. 
Found 1 input samples and 39 target samples.

Hope this answer helps you, or someone else, make further progress. 希望这个答案可以帮助您或其他人取得更大的进步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM