在 Tensorflow 中从多个 tf.data.Datasets 中随机抽样
Randomly sample from multiple tf.data.Datasets in Tensorflow
假设我有 N tf.data.Datasets 和 N 概率列表(总和为 1),现在我想要创建数据集,以便示例从具有给定概率的 N 数据集中采样。
我希望它适用于任意概率 -> 简单 zip/concat/flatmap 每个数据集中固定数量的示例可能不是我想要的。
在TF中可以这样做吗?谢谢!
我觉得你可以用tf.contrib.data.rejection_resample
来实现目标分配。
如果 p
是 Tensor
概率(或非标准化相对概率),其中 p[i]
是数据集 i
被选中的概率,您可以使用 tf.multinomial
结合 tf.contrib.data.choose_from_datasets
:
# create some datasets and their unnormalized probability of being chosen
datasets = [
tf.data.Dataset.from_tensors(['a']).repeat(),
tf.data.Dataset.from_tensors(['b']).repeat(),
tf.data.Dataset.from_tensors(['c']).repeat(),
tf.data.Dataset.from_tensors(['d']).repeat()]
p = [1., 2., 3., 4.] # unnormalized
# random choice function
def get_random_choice(p):
choice = tf.multinomial(tf.log([p]), 1)
return tf.cast(tf.squeeze(choice), tf.int64)
# assemble the "choosing" dataset
choice_dataset = tf.data.Dataset.from_tensors([0]) # create a dummy dataset
choice_dataset = choice_dataset.map(lambda x: get_random_choice(p)) # populate it with random choices
choice_dataset = choice_dataset.repeat() # repeat
# obtain your combined dataset, assembled randomly from source datasets
# with the desired selection frequencies.
combined_dataset = tf.contrib.data.choose_from_datasets(datasets, choice_dataset)
注意数据集需要初始化(不能用简单的make_one_shot_iterator):
choice_iterator = combined_dataset.make_initializable_iterator()
choice = choice_iterator.get_next()
with tf.Session() as sess:
sess.run(choice_iterator.initializer)
print ''.join([sess.run(choice)[0] for _ in range(20)])
>> ddbcccdcccbbddadcadb
自 1.12 起,tf.data.experimental.sample_from_datasets
提供此功能:
https://www.tensorflow.org/api_docs/python/tf/data/experimental/sample_from_datasets
编辑:看起来在早期版本中可以通过 tf.contrib.data.sample_from_datasets
访问
假设我有 N tf.data.Datasets 和 N 概率列表(总和为 1),现在我想要创建数据集,以便示例从具有给定概率的 N 数据集中采样。
我希望它适用于任意概率 -> 简单 zip/concat/flatmap 每个数据集中固定数量的示例可能不是我想要的。
在TF中可以这样做吗?谢谢!
我觉得你可以用tf.contrib.data.rejection_resample
来实现目标分配。
如果 p
是 Tensor
概率(或非标准化相对概率),其中 p[i]
是数据集 i
被选中的概率,您可以使用 tf.multinomial
结合 tf.contrib.data.choose_from_datasets
:
# create some datasets and their unnormalized probability of being chosen
datasets = [
tf.data.Dataset.from_tensors(['a']).repeat(),
tf.data.Dataset.from_tensors(['b']).repeat(),
tf.data.Dataset.from_tensors(['c']).repeat(),
tf.data.Dataset.from_tensors(['d']).repeat()]
p = [1., 2., 3., 4.] # unnormalized
# random choice function
def get_random_choice(p):
choice = tf.multinomial(tf.log([p]), 1)
return tf.cast(tf.squeeze(choice), tf.int64)
# assemble the "choosing" dataset
choice_dataset = tf.data.Dataset.from_tensors([0]) # create a dummy dataset
choice_dataset = choice_dataset.map(lambda x: get_random_choice(p)) # populate it with random choices
choice_dataset = choice_dataset.repeat() # repeat
# obtain your combined dataset, assembled randomly from source datasets
# with the desired selection frequencies.
combined_dataset = tf.contrib.data.choose_from_datasets(datasets, choice_dataset)
注意数据集需要初始化(不能用简单的make_one_shot_iterator):
choice_iterator = combined_dataset.make_initializable_iterator()
choice = choice_iterator.get_next()
with tf.Session() as sess:
sess.run(choice_iterator.initializer)
print ''.join([sess.run(choice)[0] for _ in range(20)])
>> ddbcccdcccbbddadcadb
自 1.12 起,tf.data.experimental.sample_from_datasets
提供此功能:
https://www.tensorflow.org/api_docs/python/tf/data/experimental/sample_from_datasets
编辑:看起来在早期版本中可以通过 tf.contrib.data.sample_from_datasets