R Keras:LSTM模型拟合问题-输出数组不匹配

时间:2018-07-19 18:17:40

标签: r tensorflow keras deep-learning lstm

我正在尝试使用R中的Keras包为时间序列预测构建一维LSTM。我总共有280个观测值,并且正在前200个时间步上训练模型。

这是我的数据:

>data
[1] 104.674211  30.325553  25.725943  30.330470  33.919494  98.885751  31.036799  26.120383  29.929011  33.808716 102.614454  31.275918 28.122760
[14]  34.533204  38.933596 128.535598  31.998126  34.371124  35.299264 109.320850  37.147248  31.808292  36.188401  38.587492 110.945496  37.613322
[27]  31.879796  36.953585  39.512948 112.558327  35.770147  32.377192  37.855373  41.062163 115.672291  39.300869  34.376489  40.510942  44.743456
[40] 110.583600  45.455882  38.806954  46.087124  55.842239 153.782248  40.906253  46.261350  46.811348 125.209661  41.185677  36.597384  44.202294
[53]  47.642739 128.715730  41.212162  35.633601  47.998697  49.195762 128.057030  41.817710  36.760703  46.538546  47.615336 127.419624  44.014974
[66]  38.174168  46.570199  45.621642   5.754298 151.115306  40.753132  43.220820  45.130268 123.692596  41.815542  35.708891  44.339171  49.007662
[79] 127.696261  41.360583  36.686370  44.653017  47.199684 128.268945  43.121450  37.955169  47.769333  50.841333 164.244415  44.517725  44.149837
[92]  44.434503 131.707918  38.443468  33.104037  41.244507  45.168913 135.171285  38.445318  36.950023  42.376044  44.458236 131.206613  39.028980
[105]  35.517673  42.078143  46.161538 138.633018  40.927631  36.198797  46.439896  52.050011 158.697836  38.882071  40.011038  43.325122 131.811553
[118]  36.905217  32.293378  39.500356  45.379647 134.351935  38.506473  33.520961  40.085801  46.334602 141.613712  38.471661  34.943294  39.906911
[131]  43.418520 132.690750  37.797729  32.547215  41.195844  45.958671 161.540738  37.320867  40.446669  44.155767 138.347426  39.871351  33.207285
[144]  41.884156  53.102881 140.518116  41.748416  35.454985  45.002361  48.146979 147.215505  41.641961  37.037602  44.843474  48.614302 145.899917
[157]  39.998814  39.289558  47.533372  51.855770 155.500667  47.670483  46.645426  59.782910  71.429109 179.485628  47.311633  46.626551 141.468447
[170]  46.678339  43.436311  41.047591 113.229233  51.382804  31.984584  37.683744  41.678862 129.190309  36.947320  30.810330  39.766751  46.827086
[183] 134.329501  34.619556  31.565425  38.311338  41.986885 132.490021  34.646048  31.747129  40.542554  43.372881 129.704915  36.685621  30.791035
[196]  36.475763  43.158314 123.691776  41.041269  32.554182  38.578906  43.003428   6.936479 151.447335  35.098190  39.185031  41.954784 129.592065
[209]  37.503257  33.010408  41.009276  43.435023 127.724863  37.358387  33.076456  38.507582  44.386869 130.483615  37.264937  30.637003  41.278207
[222]  46.165747 132.370094  40.618600  34.049411  39.469918  42.530621 134.158590  41.086867  37.480499  58.861908 160.573323  40.978109  31.784450
[235]  40.325526  44.253421 132.244392  37.118805  30.989261  39.009905  44.869277 132.788785  36.757391  32.615994  40.015909  46.235674 134.601224
[248]  37.946823  33.413523  42.159784  45.313646 137.384025  39.622680  33.547060  42.216867  46.148574 135.930897  39.762899  34.289215  43.038315
[261]  48.735232 137.753104  42.082613  38.095095  48.786945  53.691134 171.482539  43.339101  44.989966  46.820114 135.725437  42.084594  35.534636
[274]  44.720649  49.923844 141.186341  41.887333  36.098428  45.116291  48.133853 

拆分数据并建立模型:

train <- data[1:200]
test <- data[201:length(data)]

lstm_num_timesteps <- 1
X_train <- t(sapply(1:(length(train) - lstm_num_timesteps), function(x) 
train[x:(x + lstm_num_timesteps - 1)]))
X_test <- t(sapply(1:(length(test) - lstm_num_timesteps), function(x) test[x: 
(x + lstm_num_timesteps - 1)]))

y_train <- sapply((lstm_num_timesteps + 1):(length(train)), function(x) train[x])
y_test <- sapply((lstm_num_timesteps + 1):(length(test)), function(x) test[x])

X_train <- k_expand_dims(X_train, axis = 3)

# Building the model
model <- keras_model_sequential() %>% 
  layer_lstm(units = 5, batch_input_shape = c(200, 5, 1), stateful = TRUE, batch_size = 5, return_sequences = TRUE) %>% 
  layer_dense(units = 1)

history <- model %>% compile(
  optimizer = 'rmsprop',
  loss = 'categorical_crossentropy',
  metrics = c('accuracy')
)

model %>% fit(X_train, y_train, batch_size = 10, epochs = 20, validation_data = list(X_test, y_test))

一切都很好,直到最终拟合线,出现以下错误:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
ValueError: Error when checking input: expected lstm_37_input to have 3 dimensions, but got array with shape (199, 5)

我尝试在第一个lstm层和最后一个密集层之间添加一个layer_flatten,但这也不起作用。不知道我是否在计算图层输入-高度赞赏任何建议!

0 个答案:

没有答案