我正在尝试使用GRU基于深度学习构建时间序列预测。我设法得到一个预测。供参考,如果有人想复制我的过程,这里是我的数据-https://drive.google.com/file/d/1nTQjkMuYFVaYFP4mTtjTZkEXCLwjmGip/view?usp=sharing
这是我的数据。我正在尝试预测“总计。总和”功能-
str(df)
'data.frame': 268 obs. of 5 variables:
$ Total.Sum: num 0.0159 0.0233 0.0234 0.024 0.0157 ...
$ weekday : num 0.25 0.5 0.75 1 0 0.25 0.5 0.75 1 0 ...
$ holiday : num 0 0 0 0 0 0 0 0 0 0 ...
$ date : num 0.978 0.978 0.978 0.978 0.978 ...
$ month : num 0.0201 0.0201 0.0201 0.0201 0.0201 ...
我有256个训练示例。 预处理模型数据-
# Splitting into training and testing set
df <- as.matrix(df)
x_train <- array_reshape(df[1:256,], dim = c(256,5,1)) # shifting testing set by 1 timestep
y_train <- df[,1] # first column is our target variable
y_train <- array_reshape(df, dim = c(256, 1))
这是我的GRU模型-
model <- keras_model_sequential()
model %>%
layer_gru(units = 32, batch_input_shape = c(256, 5, 1), return_sequences = T, stateful = T, activation = "relu") %>%
layer_gru(units = 64, return_sequences = T, stateful = T, activation = "relu") %>%
layer_gru(units = 128, return_sequences = T, stateful = T, activation = "relu") %>%
layer_gru(units = 32, return_sequences = F, stateful = T, activation = "relu") %>%
layer_dense(units = 1) %>%
layer_activation(activation = "linear")
model %>% compile(loss = "mse", optimizer = "adam")
最后,这是我的训练循环-
for (i in 1:1000) {
model %>% fit(x_train, y_train, batch_size = 256, shuffle = FALSE, epochs = 1)
model %>% reset_states()
}
我的问题是,如何获得模型的预测以获取长期预测?我一直在尝试从这篇Understanding Keras LSTMs帖子中了解这是如何实现的,但发现很难理解,尤其是在尝试将python转换为R时。据我所知,我们需要预测下一步在Y中,然后以某种方式将其循环回到我们的模型中...
这就是我走的路-
X <- x_train[-dim(totalSequences)[1],,] #the entire known sequence, except the last step
X <- array_reshape(X, dim = c(255, 5, 1))
Y <- x_train[dim(x_train)[1],,] #one step ahead of X
Y <- array_reshape(Y, dim = c(1, 5))
#loop for resetting states at the start/end of the sequences:
for (i in 1:60){
model %>% reset_states()
model %>% fit(X, Y, batch_size = 256, shuffle = FALSE, epochs = 10)
}
这是我遇到无法解决的形状不匹配错误的地方。任何帮助都将不胜感激!
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Error when checking target: expected activation_14 to have shape (1,) but got array with shape (5,)