How do I split an iterable dataset into training and test datasets?
I have an iterable dataset object with all of my data files. How can I split it into train and validation set. I have seen a few solutions for custom datasets but iterable does not support len() operator.
torch.utils.random_sample() and torch.utils.SubsetRandomSample() don't work.
def __init__(self):
    bla bla
def __iter__(self):
    bla bla
    yield batch


Technically you can just set a goal ratio, and start collecting items into two lists randomly using that ratio. The result won't be perfect, but asymptotically it will keep the ratio.
The example is JavaScript, as it can be run here:


{
  let a = [],
      b = [];
  function addsample(x) {
    if (Math.random() &lt; 0.2) // aims for 20%-80% split
      a.push(x);
    else
      b.push(x);
    return {a, b};
  }
}

for(let i=0;i&lt;20;i++)
  console.log(JSON.stringify(addsample(i)));



If you run the snippet a couple times, you will see that the output varies a lot, but even with such a small sample size it's quite visible, that usually there is a suitable split available all the time where a really has around 1/4 the size of b. Sometimes it even manages to end up exactly 4:16, but many times it will be something else. And there can be &quot;unlucky&quot; runs too, when a has more elements than b at the end.
