1

想象一下,我想训练模型,以最小化图像和查询之间的距离。一方面我有来自 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)。密集有批处理功能,稀疏有.apply(tf.contrib.data.dense_to_sparse_batch(..))功能,但如何同时使用它们?

4

1 回答 1

0

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

@staticmethod
zip(datasets)

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

此方法与 Python 中的内置 zip() 函数具有相似的语义,主要区别在于 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) }
于 2018-05-29T00:40:58.717 回答