所以这是我第一次尝试通过RNN运行一个小型时间序列数据集,但是经过大量搜索,我一直无法找到,
1.我如何使用它来预测n个周期?(就像 ARIMA 中的model.predict(start, end)函数一样。)
2.有没有更好的方法使用 NN 来做到这一点?
有关数据的详细信息在下面的代码中作为注释给出。谢谢你。
'''
total timeseries data points = 39
frequency = Months
train = 36
test = 3
Need to forecast 3 periods ahead upto 42, currently using forecast horizon as 1
Limited data so used all in a single batch
'''
periods = 35
f_horizon = 1
batch_size = 35
x_batches = dfp1[ : (len(dfp1) - (len(dfp1) % batch_size))].reshape(-1, periods, 1) # train data for t periods
y_batches = dfp1[1 : len(dfp1) - (len(dfp1) % batch_size) + f_horizon].reshape(-1, periods, 1) # train data for t+1 periods
#defining test data set
def test_data(testdata):
test_x = testdata[ :(len(testdata)-1)] # t
test_y = testdata[1:len(testdata)] # t+1
return test_x, test_y
test_x, test_y = test_data(dfp3[-4: ]) #since 35 points were used in training, used the last 4 points for testing
outputs = 1
inputs = 1
hidden = 100
with tf.device('/cpu:0'):
with tf.variable_scope('var', reuse = tf.AUTO_REUSE):
x = tf.placeholder(tf.float32, [None, periods, inputs])
y = tf.placeholder(tf.float32, [None, periods, outputs])
#creating a basic rnn cell
basicrnn = tf.nn.rnn_cell.BasicRNNCell(num_units = hidden, activation = tf.nn.relu)
rnn_output, states = tf.nn.dynamic_rnn(basicrnn, x, dtype = tf.float32)
stacked_rnn_output = tf.reshape(rnn_output, [-1, hidden])
stacked_output = tf.layers.dense(stacked_rnn_output, outputs)
outputs_ = tf.reshape(stacked_output, [-1, periods, 1])
#using mape for loss fn
loss = tf.reduce_mean(tf.abs(tf.divide(tf.subtract(outputs_, y), y))) * 100
learningrate = 0.001
training_op = tf.train.AdamOptimizer(learning_rate = learningrate).minimize(loss)
init = tf.global_variables_initializer()
epochs = 1501
with tf.Session() as sess:
init.run()
for i in range(epochs):
sess.run(training_op, feed_dict = {x: x_batches, y: y_batches})
if i%100 == 0:
mape = loss.eval(feed_dict = {x: x_batches, y: y_batches})
print("ep %s: %s"%(i, mape))
'''
unable to pass the 4 test data points since input size is 35,
can I reshape x & y before I pass test input, would it alter the model ?
also why is the loss fluctuating, and not steadily decreasing.
'''
#y_pred = sess.run(outputs_, feed_dict = {x: test_x})
#print(y_pred)