title: Pytorch: What is the best way to sample two items of different labels form a dataset?
body: <p>I'm trying to define a pytorch <code>Dataset</code>/<code>Dataloader</code> for an image style transfer network. I have a dataset of images grouped by styles, and I want each sample from this dataset to consist of two images, one for style, the other for content. My first idea was to implement a <code>Dataset</code> with something like this in <code>__init__</code>:</p>
  <pre class="lang-py prettyprint-override"><code>n = len(images)
  itr_style = random.shuffle([i for i in range(n)]))
  itr_content = random.shuffle([i for i in range(n)]))
  </code></pre>
  <p>and this in <code>__getitem__</code>:</p>
  <pre class="lang-py prettyprint-override"><code>return (images[itr_style[index]], images[itr_content[index]])
  </code></pre>
  <p>Which is probably not the most efficient implementation, and I also need to make sure that:</p>
  <ol>
  <li>The two images don't come from the same style</li>
  <li>The dataset re-shuffles every epoch</li>
  </ol>
  <p>So what is the best way to implement this <code>Dataset</code>?</p>