Error while using DataLoader

garima: <p>This is my method for training my model:<br>
def train_model(model, optim, train_dl):<br>
model.train()<br>
total = 0<br>
sum_loss = 0<br>
print(type(train_dl))<br>
for x1, x2, y in train_dl:<br>
batch = y.shape[0]<br>
output = model(x1, x2)<br>
loss = F.cross_entropy(output, y)<br>
optim.zero_grad()<br>
loss.backward()<br>
optim.step()<br>
total += batch<br>
sum_loss += batch*(loss.item())<br>
return sum_loss/total</p>
<h2>And this the error I get when I try to execute it:</h2>
<p>KeyError                                  Traceback (most recent call last)<br>
 in <br>
----&gt; 1 train_loop(model, epochs=8, lr=0.05, wd=0.00001)</p>
<p> in train_loop(model, epochs, lr, wd)<br>
2     optim = get_optimizer(model, lr = lr, wd = wd)<br>
3     for i in range(epochs):<br>
----&gt; 4         loss = train_model(model, optim, train_dl)<br>
5         print("training loss: ", loss)<br>
6         val_loss(model, valid_dl)</p>
<p> in train_model(model, optim, train_dl)<br>
4     sum_loss = 0<br>
5     print(type(train_dl))<br>
----&gt; 6     for x1, x2, y in train_dl:<br>
7         batch = y.shape[0]<br>
8         output = model(x1, x2)</p>
<p>~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in <strong>next</strong>(self)<br>
344     def <strong>next</strong>(self):<br>
345         index = self._next_index()  # may raise StopIteration<br>
–&gt; 346         data = self.dataset_fetcher.fetch(index)  # may raise StopIteration<br>
347         if self.pin_memory:<br>
348             data = _utils.pin_memory.pin_memory(data)</p>
<p>~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\utils\data_utils\fetch.py in fetch(self, possibly_batched_index)<br>
42     def fetch(self, possibly_batched_index):<br>
43         if self.auto_collation:<br>
—&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]<br>
45         else:<br>
46             data = self.dataset[possibly_batched_index]</p>
<p>~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\utils\data_utils\fetch.py in (.0)<br>
42     def fetch(self, possibly_batched_index):<br>
43         if self.auto_collation:<br>
—&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]<br>
45         else:<br>
46             data = self.dataset[possibly_batched_index]</p>
<p> in <strong>getitem</strong>(self, idx)<br>
10<br>
11     def <strong>getitem</strong>(self, idx):<br>
—&gt; 12         return self.X1[idx], self.X2[idx], self.y[idx]<br>
13<br>
14 # creating train and valid datasets</p>
<p>~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in <strong>getitem</strong>(self, key)<br>
1069         key = com.apply_if_callable(key, self)<br>
1070         try:<br>
-&gt; 1071             result = self.index.get_value(self, key)<br>
1072<br>
1073             if not is_scalar(result):</p>
<p>~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)<br>
4728         k = self._convert_scalar_indexer(k, kind=“getitem”)<br>
4729         try:<br>
-&gt; 4730             return self._engine.get_value(s, k, tz=getattr(series.dtype, “tz”, None))<br>
4731         except KeyError as e1:<br>
4732             if len(self) &gt; 0 and (self.holds_integer() or self.is_boolean()):</p>
<p>pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()</p>
<p>pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()</p>
<p>pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()</p>
<p>KeyError: 15928942</p>

iskode: <p>Hi, the error stack trace indicates a key error.<br>
Look at your method:</p>
<blockquote>
<p>def <strong>getitem</strong> (self, idx):<br>
—&gt; 12 return self.X1[idx], self.X2[idx], self.y[idx]</p>
</blockquote>
<p>Print length(if applicable) and contents of  <code>idx</code>, <code>self.X1</code>, <code>self.X2</code>,  <code>self.y</code>.<br>
The error surely lays there.</p>
<p>Hope it helps.</p>