gol*_*enk 9 python tensorflow tensorflow-datasets tensorflow2.0
当使用 TensorFlowtf.data.experimental.sample_from_datasets从两个非常不平衡的数据集中进行同等采样时,我最终收到了DirectedInterleave selected an exhausted input: 0警告。基于此 GitHub 问题,当其中的一个数据集sample_from_datasets已耗尽示例时,似乎会发生这种情况,并且需要对已经看到的示例进行采样。
耗尽的数据集是否仍然产生样本(从而保持所需的平衡训练比率),或者数据集是否没有采样,因此训练再次变得不平衡?如果是后者,是否有一种方法可以产生所需的平衡训练比率sample_from_datasets?
注意:正在使用 TensorFlow 2 Beta
较小的数据集不会重复 - 一旦用完,其余部分将来自仍然有示例的较大数据集。
您可以通过执行以下操作来验证此行为:
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)
Run Code Online (Sandbox Code Playgroud)
然后,如果我们迭代,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]--
...
Run Code Online (Sandbox Code Playgroud)
当然,你可以做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]--
...
Run Code Online (Sandbox Code Playgroud)
但从评论看来,您已经意识到这一点,并且它不适用于您的场景。
如果是后者,是否有一种方法可以使用 sample_from_datasets 产生所需的平衡训练比率?
好吧,如果您有 2 个不同长度的数据集,并且从中均匀采样,那么您似乎只有 2 个选择:
n时间(其中n ? len(ds2)/len(ds1))要实现第一个,您可以使用ds1.repeat(n).
要实现第二个,您可以使用ds2.take(m)where m=len(ds1)。
| 归档时间: |
|
| 查看次数: |
2022 次 |
| 最近记录: |