python - 传递 Dataset.from_tensor_slices 列表与元组

标签 python tensorflow

我在处理一些 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/

相关文章:

python - 如何在不降低精度的情况下调整 tensorflow_federated TFF 中 CIFAR100 的超参数?

python - 具有多个输入的 Keras 网格搜索

python - Keras 自定义层和 Eager Execution

Python:扩展预定义的命名元组

python - 为什么 l.insert(0, i) 在 python 中比 l.append(i) 慢?

python - 如何使用派生类中的属性覆盖基类中的字段?

tensorflow - 如何从 PredictResponse 对象中检索 float_val?

python - 深度学习: Validation Loss Fluctuates Wildly Yet Training Loss is Stable

python - 使用 pandas 根据值按日期对数据进行分组

python - 如何为经过身份验证的用户添加登录后信息?