【问题标题】:Keras LSTM and multiple input feature: how to define parametersKeras LSTM 和多输入功能:如何定义参数
【发布时间】:2018-10-12 21:27:47
【问题描述】:

我在 R 和 LSTM 中发现 Keras。按照这个blog post,我想预测时间序列,我想使用过去的各种时间点(t-1,t-2)来预测t点。 到目前为止,这是我尝试过的:

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

然后我创建一个包含滞后时间序列的数据框,并创建一个测试和训练集:

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,  ]

我重新缩放数据

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] 

this post之后,我以 3D 形式重塑数据

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

我做以下模型并尝试开始学习:

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)

但我收到以下错误:

py_call_impl(callable, dots$args, dots$keywords) 中的错误: ValueError:没有为“dense_11”提供数据。需要每个键的数据:['dense_11']

尝试以不同的方式重塑数据并没有帮助。 我做错了什么?

【问题讨论】:

  • 嗨,丹尼斯 我有什么可以添加到我的答案中以获得支持或接受的标记吗?

标签: r keras lstm


【解决方案1】:

当它们是数据帧时,R 中的 Keras 和 tensorflow 无法识别输入/目标数据的大小。

y_train 既是 data.table 也是 data.frame:

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

keras fit 文档指出:“y:目标(标签)数据的向量、矩阵或数组(如果模型有多个输出,则为列表)。”同样,对于 x。

不幸的是,当 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.

希望这个答案可以帮助您或其他人取得进一步的进步。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2019-01-07
    • 2018-06-05
    相关资源
    最近更新 更多