title: How does PyTorch DataLoader interact with a PyTorch dataset to transform batches?
body: <p>I'm creating a custom dataset for NLP-related tasks.</p>
  <p>In the PyTorch <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html#dataset-class" rel="nofollow noreferrer">custom datast tutorial</a>, we see that the <code>__getitem__()</code> method leaves room for a transform before it returns a sample:</p>
  <pre><code>def __getitem__(self, idx):
          if torch.is_tensor(idx):
              idx = idx.tolist()
  
          img_name = os.path.join(self.root_dir,
                                  self.landmarks_frame.iloc[idx, 0])
          image = io.imread(img_name)
         
          ### SOME DATA MANIPULATION HERE ###
  
          sample = {'image': image, 'landmarks': landmarks}
          if self.transform:
              sample = self.transform(sample)
  
          return sample
  </code></pre>
  <p>However, the code here:</p>
  <pre><code>        if torch.is_tensor(idx):
              idx = idx.tolist()
  </code></pre>
  <p>implies that multiple items should be able to be retrieved at a time which leaves me wondering:</p>
  <ol>
  <li><p><strong>How does that transform work on multiple items?</strong> Take the custom transforms in the tutorial for example. They do not look like they could be applied to a batch of samples in a single call.</p>
  </li>
  <li><p><strong>Related, how does a DataLoader retrieve a batch of multiple samples in parallel and apply said transform if the transform can only be applied to a single sample?</strong></p>
  </li>
  </ol>