提供Tensorflow Seq2Seq输出作为下一步的输入(推断)

提供Tensorflow Seq2Seq输出作为下一步的输入(推断),tensorflow,time-series,seq2seq,Tensorflow,Time Series,Seq2seq,我想创建一个Seq2Seq模型来预测时间序列数据。我正在使用推断助手,我正在努力处理sample\u fn参数。我想通过密集层传递每个单元的解码器输出,以便在每个时间步生成单个输出。因此,我提供了一个函数,可以对sample\u fn参数执行此操作 稍后,我想将rnn单元输出与其他非时间序列特性连接起来,并在其上构建更密集的层 网络在训练时表现良好,但在推理过程中表现不佳。我认为这是因为我没有在训练和推理时间之间共享相同的密集层 我试图设置重用参数,并在tf.variable\u scope()

我想创建一个Seq2Seq模型来预测时间序列数据。我正在使用推断助手,我正在努力处理
sample\u fn
参数。我想通过密集层传递每个单元的解码器输出,以便在每个时间步生成单个输出。因此,我提供了一个函数,可以对
sample\u fn
参数执行此操作

稍后,我想将rnn单元输出与其他非时间序列特性连接起来,并在其上构建更密集的层

网络在训练时表现良好,但在推理过程中表现不佳。我认为这是因为我没有在训练和推理时间之间共享相同的密集层

我试图设置重用参数,并在tf.variable\u scope()环境中使用了
。但是,
sample\u fn
已在
dynamic\u decode
中的特定范围内调用,因此我无法使用与培训期间相同的范围

我的代码的相关部分如下所示:

占位符:

inputs = tf.placeholder(shape=(None, 100, 1), dtype=tf.float32, name='inputs')
input_lengths = tf.placeholder(shape=(None,), dtype=tf.int32, name='input_lengths')

targets = tf.placeholder(shape=(None, 100), dtype=tf.float32, name='targets')
target_lengths = tf.placeholder(shape=(None,), dtype=tf.int32, name='target_lengths')
编码器:

encoder_cell = tf.nn.rnn_cell.MultiRNNCell([tf.contrib.rnn.GRUCell(num_units=16, name='encoder_cell_0'])
self.decoder_cell = tf.nn.rnn_cell.MultiRNNCell([tf.contrib.rnn.GRUCell(num_units=16, name='decoder_cell_0']))

_, final_encoder_states = tf.nn.dynamic_rnn(cell=encoder_cell, inputs=inputs,
                                                sequence_length=input_lengths, dtype=tf.float32)
解码器(培训)

解码器(推理)。不正确的部分:

def sample_fn(outputs):
    return tf.layers.dense(outputs, 1, activation=None,         
                           name='output_dense_layer', reuse=tf.AUTO_REUSE)

infer_helper = tf.contrib.seq2seq.InferenceHelper(sample_fn=sample_fn, sample_shape=(1), 
                                                       sample_dtype=tf.float32, start_inputs=start_tokens, end_fn=lambda sample_ids: False, next_inputs_fn=None)
infer_decoder = tf.contrib.seq2seq.BasicDecoder(cell=decoder_cell, helper=infer_helper, initial_state=final_encoder_states)

infer_outputs, _, _ = tf.contrib.seq2seq.dynamic_decode(decoder=infer_decoder, maximum_iterations=max_target_sequence_length, impute_finished=True)

infer_predictions = infer_outputs.rnn_output
infer_predictions = sample_fn(infer_predictions)
还有一个类似的问题:


作者使用
sample\u fn=lambda outputs:outputs
。但在我的例子中,这会返回一个ValueError,因为维度不匹配。他们怎么可能有多个细胞
sample\u fn
应该返回一个值。

现在,我已经通过创建自己的动态解码函数解决了我的问题。我把所有的东西都抄了下来

with variable_scope.variable_scope(scope, "decoder", reuse=reuse) as varscope:
以及一个与
varscope
相关的if条件,以及另一个测试
tf.contrib.seq2seq.dynamic_decode
中解码器类的if条件

这不是一个好的解决方案,但现在已经足够好了

with variable_scope.variable_scope(scope, "decoder", reuse=reuse) as varscope: