我想在训练阶段使用tensorflow / Keras实现具有停止条件而不是固定数量的RNN解码器 . 基本上,我正在测试图像字幕任务的解码器,而不是将所有字幕填充为具有相同的长度(字数) . 我希望算法通过抛出符号"eos"来自行决定训练阶段的序列长度 . 我开始使用该方法的源代码: tensorflow.contrib.legacy_seq2seq.python.ops.seq2seq.rnn_decoder 我尝试使用它但是徒劳无功 . 这是tensorflow方法的原始代码:

def rnn_decoder(decoder_inputs,
            initial_state,
            cell,
            loop_function=None,
            scope=None):
  with variable_scope.variable_scope(scope or "rnn_decoder"):
    state = initial_state
    outputs = []
    prev = None
    #Instead of "for" loop, I want it to be "while condition Q" loop
    for i, inp in enumerate(decoder_inputs):
      if loop_function is not None and prev is not None:
        with variable_scope.variable_scope("loop_function", reuse=True):
          inp = loop_function(prev, i)
      if i > 0:
        variable_scope.get_variable_scope().reuse_variables()
      output, state = cell(inp, state)
      outputs.append(output)
      if loop_function is not None:
        prev = output
      #want to add: if prev ==<eos>, the condition Q gets False
  return outputs, state

我的目标是将rnn_decoder函数中的 for 循环更改为 while 循环,如果面对训练期间生成的序列结尾 eos 符号,则该循环将停止,然后对其执行常规的反向传播 . 它是一种在训练模式中具有推理模式 .