tf.keras.layers.GRU 的输入有问题

Problem with input to tf.keras.layers.GRU

我正在尝试在 TensorFlow 2.1 中使用 tfa.seq2seq.BaseDecoder 制作一个 seq2seq 模型。我有

tf.keras.layers.GRU(64)(inputs, [states])

其中输入的形状为 (batch_size, 1, embedding_dimension),来自

inputs = tf.keras.layers.Embedding(1000, 64, mask_zero=True)(tf.fill([batch_size, 1], value=1))

states 是批次的编码器隐藏状态。

我正在实施 tfa.seq2seq.BaseDecoderinitializestep 和一些属性,错误发生在 step 中,其中包含我在此处复制的行.

但是,它给了我以下错误消息(更改了一些函数名称以便更容易解释问题,并且在代码中略有不同)。

    Traceback (most recent call last):
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 2659, in _set_inputs
    outputs = self(inputs, **kwargs)
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 773, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "/home/.local/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py", line 237, in wrapper
    raise e.ag_error_metadata.to_exception(e)
TypeError: in converted code:

    /home/lemmatizer_noattn.py:155 call  *
        output_layer, _, output_lens, _ = self.DecoderTraining((source_states, target_charseqs), True)
    /home/.local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py:785 __call__
        str(e) + '\n"""')

    TypeError: You are attempting to use Python control flow in a layer that was not declared to be dynamic. Pass `dynamic=True` to the class constructor.
    Encountered error:
    """
    in converted code:

        /home/.local/lib/python3.7/site-packages/tensorflow_addons/seq2seq/decoder.py:162 call  *
            return dynamic_decode(
        /home/.local/lib/python3.7/site-packages/tensorflow_addons/seq2seq/decoder.py:405 body  *
            (next_outputs, decoder_state, next_inputs, decoder_finished) = decoder.step(
        /home/.local/lib/python3.7/site-packages/tensorflow_core/python/ops/control_flow_ops.py:2478 while_loop_v2
            return_same_structure=True)
        /home/lemmatizer_noattn.py:79 step  *
            outputs, [states] = self.lemmatizer.target_rnn_cell(inputs, [states])
        /home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:539 __iter__
            self._disallow_iteration()
        /home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:535 _disallow_iteration
            self._disallow_in_graph_mode("iterating over `tf.Tensor`")
        /home/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:515 _disallow_in_graph_mode
            " this function with @tf.function.".format(task))

        OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

    """

我没能从文档中找出错误的来源,也没有在互联网上找到任何建议。关于问题可能出在哪里的任何想法?

这一行看起来像是一个元组和一个列表的扩展,其中签名没有扩展(导致这个令人困惑的错误):

/home/lemmatizer_noattn.py:79 step  *
            outputs, [states] = self.lemmatizer.target_rnn_cell(inputs, [states])

self.lemmatizer.target_rnn_cellreturn是什么意思?尝试在扩展之前打印该值,如下所示:

retval = self.lemmatizer.target_rnn_cell(inputs, [states])
print(retval)
inputs = retval[0]
states = retval[1][0]