如何将先前的时间戳预测作为下一个时间戳的附加输入?

克拉涅夫·康斯坦丁

可能有人问过这个问题,但我很困惑。

我正在尝试将RNN类型之一(例如LSTM)应用于时间序列预测。我有输入y(股票收益)。对于每个时间戳,我想获得预测。问题1-我是否正确选择seq2seq方法?

我还想使用以前时间戳的预测(使用一些常数初始化初始值)作为残差平方形式的其他输入(仍使用我现有的输入),即使用eps_ {t-1} =(y_ {t-1} -y ^ _ {t-1})^ 2作为t处的附加输入(以及先前的输入)。

那么,如何在tensorflow或pytorch中做到这一点呢?

我试图在所附图表上描绘我想要的东西。

ps抱歉,这个问题措辞不好

丹妮·亚蒂姆(Dany Yatim)

假设您输入的尺寸为(32,10,1),且具有batch_size为32,时间步长为10,尺寸为1。与您的目标相同(退货)。该代码利用了tf.scan函数,该函数在实现自定义循环网络时很有用(它将在时间步长上进行迭代)。您仍然可以在某处使用t-1中t-1的残差。

ps:这是从头开始的lstm的非常基本的实现,没有任何偏差或输出激活。

import tensorflow as tf
import numpy as np
tf.reset_default_graph()

BS = 32
TS = 10
inputs_dim = 1
target_dim = 1

inputs = tf.placeholder(shape=[BS, TS, inputs_dim], dtype=tf.float32)
stock_returns = tf.placeholder(shape=[BS, TS, target_dim], dtype=tf.float32)

state_size = 16

# initial hidden state
init_state = tf.placeholder(shape=[2, BS, state_size], 
                        dtype=tf.float32, name='initial_state')
# initializer
xav_init = tf.contrib.layers.xavier_initializer
# params
W = tf.get_variable('W', shape=[4, state_size, state_size], 
                initializer=xav_init())
U = tf.get_variable('U', shape=[4, inputs_dim, state_size], 
                initializer=xav_init())
W_out = tf.get_variable('W_out', shape=[state_size, target_dim], 
                   initializer=xav_init())

#the function to feed tf.scan with
def step(prev, inputs_):

   #unpack all inputs and previous outputs
   st_1, ct_1 = prev[0][0], prev[0][1]
   x = inputs_[0]
   target = inputs_[1]

   #get previous squared residual
   eps = prev[1]


   """
   here do whatever you want with eps_t-1
   like x += eps if x if of the same dimension
   or include it somewhere in your graph
   """


   # lstm gates (add bias if needed)
   #
   #  input gate
   i = tf.sigmoid(tf.matmul(x,U[0]) + tf.matmul(st_1,W[0]))
   #  forget gate
   f = tf.sigmoid(tf.matmul(x,U[1]) + tf.matmul(st_1,W[1]))
   #  output gate
   o = tf.sigmoid(tf.matmul(x,U[2]) + tf.matmul(st_1,W[2]))
   #  gate weights
   g = tf.tanh(tf.matmul(x,U[3]) + tf.matmul(st_1,W[3]))

   ct = ct_1*f + g*i
   st = tf.tanh(ct)*o

   """
   make prediction, compute residual in t
   and pass it to t+1
   Normaly, we would compute prediction outside the scan function, 
   but as we need it here, we could just keep it and return it back
   as an output of the scan function 
   """

   prediction_t = tf.matmul(st, W_out) # + bias
   eps = (target - prediction_t)**2 

   return [tf.stack((st, ct), axis=0), eps, prediction_t] 

states, eps, preds = tf.scan(step, [tf.transpose(inputs, [1,0,2]), 
       tf.transpose(stock_returns, [1,0,2])], initializer=[init_state, 
       tf.zeros((32,1), dtype=tf.float32), 
       tf.zeros((32,1),dtype=tf.float32)])


with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  out = sess.run(preds, feed_dict= 
               {inputs:np.random.rand(BS,TS,inputs_dim),                                      
                stock_returns:np.random.rand(BS,TS,target_dim),                                    
                init_state:np.zeros((2,BS,state_size))})
  out = tf.transpose(out,[1,0,2])
  print(out)

和输出:

Tensor("transpose_2:0", shape=(32, 10, 1), dtype=float32)

这里的基本代码

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

自行加入下一个时间戳

在 TensorFlow 2.0 中,如何将 LSTM 模型在上一个时间步的输出作为输入传递到下一个时间步?

获取下一个时间戳值

获取下一个小时的时间戳

如何在 Oracle 中从另一个时间戳中减去一个时间戳

DL4J中的回归-预测下一个时间步

如何比较两个时间戳以查看一个时间戳是否晚了超过特定的分钟数?

如何删除行,当时间戳与另一个时间戳完全一样

选择每个日期的最后一个时间戳

在pyspark中的两个时间戳之间创建一个时间戳数组

在另一个时间戳之前计算时间戳数量的更快方法

如何从python中的时间列中删除一个时间点到另一个时间点的时间戳

如何将时间戳字符串转换为另一个时区的时间戳

聚合日志将第一个时间戳设置为@timestamp

当新的时间戳值等于前一个时间戳值时,DataStax / Cassandra的USING TIMESTAMP行为是不可预测的

我如何评估任何逻辑中下一个时间步的条件?

如何从sqlite表中获取最后一个时间戳日期

如何通过不同的列选择上一个时间戳?

如何提高timescaledb获取上一个时间戳的性能

如何选择多个用户ID和几天的每天的最后一个时间戳读数?

我如何在熊猫中将三列合并为一个时间戳列

如何获得上一个时间戳值:mysql

如何使用上一个时间戳的值填充空列

如何找到指定值之后的下一个零并提取其时间戳?

查找下一个非NaN值的时间戳

PHP DateTime:从特定时间戳获取下一个星期日

从python中的下一个时间值减去前一个时间值

如何将时间轮到下一个半小时?

如何使用 JavaScript 在下一个时间字段中增加一个小时