当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tf.compat.v1.data.TextLineDataset.zip用法及代码示例


用法

@staticmethod
zip(
    datasets, name=None
)

参数

  • datasets 数据集的(嵌套)结构。
  • name (可选。) tf.data 操作的名称。

返回

  • Dataset 一个Dataset

通过将给定的数据集压缩在一起来创建 Dataset

此方法与 Python 中的内置 zip() 函数具有相似的语义,主要区别在于 datasets 参数可以是 Dataset 对象的(嵌套)结构。此处记录了支持的嵌套机制。

# The nested structure of the `datasets` argument determines the
# structure of elements in the resulting dataset.
a = tf.data.Dataset.range(1, 4)  # ==> [ 1, 2, 3 ]
b = tf.data.Dataset.range(4, 7)  # ==> [ 4, 5, 6 ]
ds = tf.data.Dataset.zip((a, b))
list(ds.as_numpy_iterator())
[(1, 4), (2, 5), (3, 6)]
ds = tf.data.Dataset.zip((b, a))
list(ds.as_numpy_iterator())
[(4, 1), (5, 2), (6, 3)]

# The `datasets` argument may contain an arbitrary number of datasets.
c = tf.data.Dataset.range(7, 13).batch(2)  # ==> [ [7, 8],
                                           #       [9, 10],
                                           #       [11, 12] ]
ds = tf.data.Dataset.zip((a, b, c))
for element in ds.as_numpy_iterator():
  print(element)
(1, 4, array([7, 8]))
(2, 5, array([ 9, 10]))
(3, 6, array([11, 12]))

# The number of elements in the resulting dataset is the same as
# the size of the smallest dataset in `datasets`.
d = tf.data.Dataset.range(13, 15)  # ==> [ 13, 14 ]
ds = tf.data.Dataset.zip((a, d))
list(ds.as_numpy_iterator())
[(1, 13), (2, 14)]

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.data.TextLineDataset.zip。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。