在没有 dict_feed 的情况下,使用 TensorFlow 优化基于移动时间序列数据 window 的图表

Using TensorFlow to optimize a graph over a moving time series data window without dict_feed

我正在尝试在 Tensorflow 中设置优化图,避免将 feed_dicttf.FIFOQueue 一起使用 - 至少这似乎是时间序列数据的正确方向。

我将首先使用 feed_dict 描述我的图表的外观(用最少的术语),然后是我尝试使用它的地方。

假设我的时间序列存储在一个 numpy 数组中 time_series:

wnd = 10 # window size
data_wnd = np.array([time_series[n:n+wnd] for n in range(1,time_series.size()+1)])

现在data_wnd[k]在时间序列中可以表示windowk

假设 step 是我的 Tensorflow 优化器,这将按如下方式正常工作:

for k in range(data_wnd.shape[0]):
    for n in range(epochs):
        sess.run(step, feed_dict={data_:data_wnd[k]})
    # Do stuff after optimization and proceed to next window frame
    # the optimized variable values for this frame are the initial values for the next frame

如果我没有 window,那么我可以简单地使用 tf.constant 而不是使用占位符 data_ 并去掉 feed_dict。所以那是不可能的。

所以 tf.FIFOQueue:

q = tf.FIFOQueue(capacity=5, shapes=(wnd))
nq_op = q.enqueue(data_wnd[0])
qr = tf.train.QueueRunner(q, [nq_op]*1) # nq_op is not right
tf.train.add_queue_runner(qr)
data_ = q.dequeue() # instead of a placeholder

太好了,现在我有了一个队列,但这显然是错误的。 qr 需要根据 k 将正确的数据帧送入队列。有没有办法让 enqueueQueueRunner 到 select 正确的帧?

更好的是,是否有专门的 Tensorflow API 以这种方式处理时间序列数据?

解决方案的一个重要约束要求我将所有内容都保存在同一个会话中并且变量不会重新初始化,因为一帧的优化解决方案接近前一帧的最优解决方案。

一个不完整的提案

我想有几个nq_op定义如下:

nq_op = []
for k in range(data_wnd.shape[0]):
    nq_op = np.append(nq_op, q.enqueue(data_wnd[0]))

但这仍然需要 QueueRunner 正确 select 正确 enqueue.

另一个提议

显然 Tensorflow 现在有一个 tf.data API,但我不知道从哪里开始 - 或者就此而言,结束 - API.

我发现 FIFOQueue 不是正确的方向。

相反,this page 预加载数据 下提供了正确的方法。

除了简单地使用占位符,我们还可以使用占位符和变量的组合 data_:

data_init = tf.placeholder(tf.float32, data_wnd.shape[0])
data_ = tf.Variable(data_init, trainable=False, collection=0)

其中 VarTypeTensorShape 已适当定义。

然后在我们的循环中:

for k in range(data_wnd.shape[0]):
    sess.run(data_.initializer, feed_dict={data_:data_wnd[k]})
    for n in range(epochs):
        sess.run(step)

请注意,我们不再需要为每个 n 提供数据,而是仅当我们向前移动 window 时才需要提供数据。