样本外神经网络或一步预测RNN

时间:2017-07-29 03:42:14

标签: r neural-network forecasting recurrent-neural-network extrapolation

我一直在研究机器学习模型的预测,特别是多输入神经网络。当y + 1不存在时,如何在测试集中预测(或在此上下文中预测)y + 1?下面,我有1月1日到1月10日。我如何预测1月11日?

我在预测包中尝试了拟合函数,它只适用于时间序列对象。预测函数仅给出1月10日的值,而不是11日。我可以将数据帧转换为STL对象,但不确定它是否会在我的预测之上应用进一步的预测方法。

任何想法或帮助将不胜感激。

谢谢。

library(rnn)
library(zoo)

#Create random dataframe and date column, then merge
df <- data.frame(replicate(3,sample(0:5,10,rep=TRUE)))
Date = seq(from = as.Date("2017-01-01"), to = as.Date("2017-01-10"), by = 'day')
df <- cbind(df, Date)
colnames(df)[1] <- "y"
df

#Create TS dataframe
ts.df <- ts(df, start = c(2017, 01), end = c(2017, 10), frequency = 1)

# Create zoo object so it works well... has time index
df <- zoo(ts.df); rm(ts.df)

#Remove Date from zoo object, as it is already indexed
df$Date <- NULL

#Putting data in the appropriate rnn format
x1 <- as.matrix(df[,2]); x2 <- as.matrix(df[,3])
y <- as.matrix(df[,1])

#Split
n_train = 7
x1_train <- as.matrix(t(x1[1:n_train,])); x2_train <- as.matrix(t(x2[1:n_train,]))
y_train <- as.matrix(y[1:n_train])

#Using the trainr function with rnn / requires the data to be in a 3d array 
x_train <- array(c(x1_train, x2_train), dim = c(dim(x1_train), 2))

#RNN 
set.seed(2018)

#Build Model
model2 <- trainr(Y = t(y_train), X = x_train,
                 learningrate = 0.1,
                 hidden_dim = 3,
                 numepochs = 50,
                 network_type = 'rnn',
                 epoch_function = c(epoch_print,epoch_annealing),
                 sigmoid = 'logistic')

# Preparing the test data
x1_test <- as.matrix(t(x1[(n_train + 1):nrow(x1),])); x2_test <- as.matrix(t(x2[(n_train + 1):nrow(x2),]))
y_test <- as.matrix(y[(n_train + 1):nrow(x1),])

#Using the testr function with rnn / requires the data to be in a 3d array 
x_test <- array(c(x1_test, x2_test), dim = c(dim(x1_test), 2))

#Creating the predictions 
pred_test <- t(predictr(model2, x_test))
pred_test

pred_test <- ts(matrix(pred_test), end = c(2017, 10), frequency = 1)

#This doesn't work, only gives me Jan 10th's value, not 11ths or 20ths..
forecast(pred_test, 10)

1 个答案:

答案 0 :(得分:1)

你必须使用vanilla RNN(循环网​​络)来理解这种新的神经网络更适合于序列分析。 理解递归的概念(新状态取决于先前的stae等...)可以在LSTM之后选择...... Vanilla RNN足以满足您的需求。