【问题标题】:Mix dense and sparse tensors inside tf.data.Dataset api在 tf.data.Dataset api 中混合密集和稀疏张量
【发布时间】:2017-12-05 15:10:25
【问题描述】:

想象一下,我想训练模型,以最小化图像和查询之间的距离。一方面我有来自 CNN 的图像特征,另一方面我有从单词到嵌入向量的映射(例如 w2v):

def raw_data_generator():
    for row in network_data:
        yield (row["cnn"], row["w2v_indices"])

dataset = tf.data.Dataset.from_generator(raw_data_generator, (tf.float32, tf.int32))
dataset = dataset.prefetch(1000)

在这里我想创建批处理,但我想为 cnn 特征创建密集批处理,为 w2v 创建稀疏批处理,因为显然它具有可变长度(并且我想使用 safe_embeddings_lookup_sparse)。密集有batch函数,稀疏有.apply(tf.contrib.data.dense_to_sparse_batch(..))函数,但是如何同时使用呢? p>

【问题讨论】:

    标签: tensorflow tensorflow-datasets


    【解决方案1】:

    您可以尝试创建两个数据集(每个特征一个),对每个数据集应用适当的批处理,然后将它们与tf.data.Dataset.zip 一起压缩。

    @staticmethod
    zip(datasets)
    

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

    此方法与中内置的 zip() 函数具有相似的语义 Python,主要区别在于 datasets 参数可以 是 Dataset 对象的任意嵌套结构。例如:

    # NOTE: The following examples use `{ ... }` to represent the
    # contents of a dataset.
    a = { 1, 2, 3 }
    b = { 4, 5, 6 }
    c = { (7, 8), (9, 10), (11, 12) }
    d = { 13, 14 }
    
    # The nested structure of the `datasets` argument determines the
    # structure of elements in the resulting dataset.
    Dataset.zip((a, b)) == { (1, 4), (2, 5), (3, 6) }
    Dataset.zip((b, a)) == { (4, 1), (5, 2), (6, 3) }
    
    # The `datasets` argument may contain an arbitrary number of
    # datasets.
    Dataset.zip((a, b, c)) == { (1, 4, (7, 8)),
                                (2, 5, (9, 10)),
                                (3, 6, (11, 12)) }
    
    # The number of elements in the resulting dataset is the same as
    # the size of the smallest dataset in `datasets`.
    Dataset.zip((a, d)) == { (1, 13), (2, 14) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 2012-01-03
      • 1970-01-01
      相关资源
      最近更新 更多