循环数据集时 tf.function 中的异常执行顺序

Abnormal execution order in tf.function while looping through Dataset

给定以下代码片段。

import tensorflow as tf

a=tf.Variable(0)

@tf.function
def f():
    dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
    for i in dataset:
        a.assign(i)
        tf.print(a)
    tf.print(a)

f()

如果在TF 2.0中执行,结果为1 2 3 3

如果在TF 2.1中执行,结果为1 2 3 0

为什么两个版本之间存在差异,为什么第二个输出有效?

这已在 TF 2.2 中修复。