【问题标题】:TensorFlow 1.2 How to Setup Time Series Prediction at Inference Time Using Seq2SeqTensorFlow 1.2 如何使用 Seq2Seq 在推理时设置时间序列预测
【发布时间】:2017-11-22 04:56:48
【问题描述】:

我正在尝试使用玩具模型研究 TensorFlow 库的 tf.contrib.seq2seq 部分。目前,我的图表如下:

tf.reset_default_graph()

# Placeholders
enc_inp = tf.placeholder(tf.float32, [None, n_steps, n_input])
expect = tf.placeholder(tf.float32, [None, n_steps, n_output])
expect_length = tf.placeholder(tf.int32, [None])
keep_prob = tf.placeholder(tf.float32, [])

# Encoder
cells = [tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.BasicLSTMCell(n_hidden), output_keep_prob=keep_prob) for i in range(layers_stacked_count)]
cell = tf.contrib.rnn.MultiRNNCell(cells)
encoded_outputs, encoded_states = tf.nn.dynamic_rnn(cell, enc_inp, dtype=tf.float32)

# Decoder
de_cells = [tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.BasicLSTMCell(n_hidden), output_keep_prob=keep_prob) for i in range(layers_stacked_count)]
de_cell = tf.contrib.rnn.MultiRNNCell(de_cells)

training_helper = tf.contrib.seq2seq.TrainingHelper(expect, expect_length)

decoder = tf.contrib.seq2seq.BasicDecoder(cell=de_cell, helper=training_helper, initial_state=encoded_states)
final_outputs, final_state, final_sequence_lengths = tf.contrib.seq2seq.dynamic_decode(decoder)

decoder_logits = final_outputs.rnn_output

h = tf.contrib.layers.fully_connected(decoder_logits, n_output)

diff = tf.squared_difference(h, expect)
batch_loss = tf.reduce_sum(diff, axis=1)
loss = tf.reduce_mean(batch_loss)

optimiser = tf.train.AdamOptimizer(1e-3)
training_op = optimiser.minimize(loss)

图表训练得很好并且执行得很好。但是,我不确定在推理时要做什么,因为该图始终需要 expect 变量(我试图预测的值)。

据我了解,TrainingHelper 函数使用地面实况作为输入,所以我需要在推理时使用另一个辅助函数。

我发现的大多数 seq2seq 模型的实现似乎都已经过时了 (tf.contrib.legacy_seq2seq)。一些最新的模型经常使用 GreddyEmbeddingHelper,我不确定它是否适合连续时间序列预测。

我发现的另一个可能的解决方案是使用 CustomHelper 函数。但是,那里没有什么材料可供我学习,我只是不停地用头撞墙。

如果我想实现一个 seq2seq 模型进行时间序列预测,我应该在推理时做什么?

任何帮助或建议将不胜感激。提前致谢!

【问题讨论】:

  • 正是那部分“我发现的另一个可能的解决方案是使用 CustomHelper 函数。但是,那里没有什么材料可供我学习,我只是不停地用头撞墙。”丢失,需要样品
  • 您是否设法使用 CustomHelper 解决了这个问题?

标签: tensorflow


【解决方案1】:

您是对的,您需要使用另一个辅助函数进行推理,但您需要在测试和推理之间共享权重。

你可以用 tf.variable_scope() 做到这一点

with tf.variable_scope("decode"):
    training_helper = ...

with tf.variable_scope("decode", reuse = True):
    inference_helper = ...

有关更完整的示例,请参阅以下两个示例之一:

【讨论】:

  • 感谢您的帮助,这就是我一直在寻找的!
  • 我现在面临同样的问题。您是否使用 InferenceHelper 解决了这个问题?
  • @MrfksIV 你找到使用 inferenceHelper 的方法了吗?
猜你喜欢
  • 1970-01-01
  • 2016-10-29
  • 1970-01-01
  • 2020-09-03
  • 2020-10-01
  • 2020-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多