传递 Dataset.from_tensor_slices 列表与元组

met*_*rem 2 python tensorflow

我在处理一些张量流代码(v1.13.1)时注意到了这种微妙之处:

tf.enable_eager_execution()

for n in Dataset.from_tensor_slices(([1, 2], [3, 4])).make_one_shot_iterator():
    print(n)
#
# prints:
# (<tf.Tensor: id=8, shape=(), dtype=int32, numpy=1>, <tf.Tensor: id=9, shape=(), dtype=int32, numpy=3>)
# (<tf.Tensor: id=12, shape=(), dtype=int32, numpy=2>, <tf.Tensor: id=13, shape=(), dtype=int32, numpy=4>)
#

for n in Dataset.from_tensor_slices([[1, 2], [3, 4]]).make_one_shot_iterator():
    print(n)
#
# prints:
# tf.Tensor([1 2], shape=(2,), dtype=int32)
# tf.Tensor([3 4], shape=(2,), dtype=int32)
#
Run Code Online (Sandbox Code Playgroud)

上面的区别是第一个循环传递元组中的两个张量,第二个循环传递列表中的两个张量。我预计第二个循环的工作方式与第一个循环相同,对张量进行切片。这是 tf 处理传入元组和列表的方式故意不同吗?

met*_*rem 5

感谢@giser_yugang 的链接/答案。

链接的问题来看:

这是按预期工作的:tf.data API 使用 Python 列表来表示应隐式转换为张量的值,并使用 Python 元组来表示应解释为(可能嵌套)结构的多个组件的值。

可能是很多微妙问题的原因......