title: How to use a Batchsampler within a Dataloader
body: <p>I have a need to use a <code>BatchSampler</code> within a pytorch <code>DataLoader</code> instead of calling <code>__getitem__</code> of the dataset multiple times (remote dataset, each query is pricy). <br>I cannot understand how to use the batchsampler with <strong>any</strong> given dataset.</p>
  
  <p>e.g</p>
  
  <pre><code>class MyDataset(Dataset):
  
      def __init__(self, remote_ddf, ):
          self.ddf = remote_ddf
  
      def __len__(self):
          return len(self.ddf)
  
      def __getitem__(self, idx):
          return self.ddf[idx] --------&gt; This is as expensive as a batch call
  
      def get_batch(self, batch_idx):
          return self.ddf[batch_idx]
  
  my_loader = DataLoader(MyDataset(remote_ddf), 
             batch_sampler=BatchSampler(Sampler(), batch_size=3))
  
  </code></pre>
  
  <p>The thing I do not understand, neither found any example online or in torch docs, is how do I use my <code>get_batch</code> function instead of the __getitem__ function. 
  <br>
  Edit:
  Following the answer of Szymon Maszke, this is what I tried and yet, <code>\_\_get_item__</code> gets one index each call, instead of a list of size <code>batch_size</code></p>
  
  <pre><code>class Dataset(Dataset):
  
      def __init__(self):
         ...
  
      def __len__(self):
          ...
  
      def __getitem__(self, batch_idx):  ------&gt; here I get only one index
          return self.wiki_df.loc[batch_idx]
  
  
  loader = DataLoader(
                  dataset=dataset,
                  batch_sampler=BatchSampler(
                      SequentialSampler(dataset), batch_size=self.hparams.batch_size, drop_last=False),
                  num_workers=self.hparams.num_data_workers,
              )
  </code></pre>