我在处理一些 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)
#
上面的区别是第一个循环传递元组中的两个张量,第二个循环传递列表中的张量。我希望第二个循环与第一个循环一样工作,对张量进行切片。这是 tf 处理传入元组和列表的方式的故意差异吗?
最佳答案
感谢@giser_yugang 提供链接/回答。
来自linked问题:
This is working as intended: the tf.data API uses Python lists to signify values that should be converted implicitly to tensors, and Python tuples to signify values that should be interpreted as multiple components of a (potentially nested) structure.
可能是很多细微问题的原因...
关于python - 传递 Dataset.from_tensor_slices 列表与元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55891999/