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