当使用TensorFlow的tf.data.experimental.sample_from_datasets
对两个非常不平衡的数据集进行同样的采样时,我最终会得到一个DirectedInterleave selected an exhausted input: 0
警告。基于这个GitHub问题,当sample_from_datasets
中的一个数据集已经耗尽了示例,并且需要对已经看到的示例进行采样时,就会出现这种情况。
那么,耗尽的数据集是否仍然产生样本(从而保持所需的均衡培训比率),还是数据集没有采样,从而使培训再次变得不平衡?如果是后者,是否有一种方法可以用sample_from_datasets
生成所需的均衡训练比率?
注:正在使用TensorFlow 2 Beta。
发布于 2019-07-30 22:08:38
较小的数据集不会重复--一旦用完,其余的数据集将来自仍然有示例的较大数据集。
您可以通过这样的操作来验证这种行为:
def data1():
for i in range(5):
yield "data1-{}".format(i)
def data2():
for i in range(10000):
yield "data2-{}".format(i)
ds1 = tf.data.Dataset.from_generator(data1, tf.string)
ds2 = tf.data.Dataset.from_generator(data2, tf.string)
sampled_ds = tf.data.experimental.sample_from_datasets([ds2, ds1], seed=1)
然后,如果我们在sampled_ds
上迭代,我们会发现,一旦data1
耗尽,就不会产生来自它的任何样本:
tf.Tensor(b'data1-0', shape=(), dtype=string)
tf.Tensor(b'data2-0', shape=(), dtype=string)
tf.Tensor(b'data2-1', shape=(), dtype=string)
tf.Tensor(b'data2-2', shape=(), dtype=string)
tf.Tensor(b'data2-3', shape=(), dtype=string)
tf.Tensor(b'data2-4', shape=(), dtype=string)
tf.Tensor(b'data1-1', shape=(), dtype=string)
tf.Tensor(b'data1-2', shape=(), dtype=string)
tf.Tensor(b'data1-3', shape=(), dtype=string)
tf.Tensor(b'data2-5', shape=(), dtype=string)
tf.Tensor(b'data1-4', shape=(), dtype=string)
tf.Tensor(b'data2-6', shape=(), dtype=string)
tf.Tensor(b'data2-7', shape=(), dtype=string)
tf.Tensor(b'data2-8', shape=(), dtype=string)
tf.Tensor(b'data2-9', shape=(), dtype=string)
tf.Tensor(b'data2-10', shape=(), dtype=string)
tf.Tensor(b'data2-11', shape=(), dtype=string)
tf.Tensor(b'data2-12', shape=(), dtype=string)
...
---[no more 'data1-x' examples]--
...
当然,您可以让data1
重复这样的内容:
sampled_ds = tf.data.experimental.sample_from_datasets([ds2, ds1.repeat()], seed=1)
但是,从评论中可以看出,您已经意识到了这一点,并且它不适用于您的场景。
如果是后者,是否有一种方法可以用sample_from_datasets来产生所需的均衡训练比率?
那么,如果您有两个不同长度的数据集,并且从那时开始平均抽样,那么您似乎只有两个选择:
n
时间(其中n ≃ len(ds2)/len(ds1)
)要实现第一个目标,您可以使用ds1.repeat(n)
。
要实现第二个目标,您可以使用ds2.take(m)
where m=len(ds1)
。
https://stackoverflow.com/questions/57278214
复制相似问题