IndexError: Target -1 is out of bounds error in tabular learner fatai2
Getting the below error when trying to fit a tabular_learner from fastai2 library. 

used data loaders



learn = tabular_learner(dls, layers=[1000,500], metrics=accuracy)

learn.fit(30,1e-2)

IndexError                                Traceback (most recent call last)
&lt;ipython-input-35-f0c57ab3748f&gt; in &lt;module&gt;
----&gt; 1 learn.fit(30,1e-2)

/mnt/c/fastai2/fastai2/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    191                         self.epoch=epoch;          self('begin_epoch')
    192                         self._do_epoch_train()
--&gt; 193                         self._do_epoch_validate()
    194                     except CancelEpochException:   self('after_cancel_epoch')
    195                     finally:                       self('after_epoch')

/mnt/c/fastai2/fastai2/learner.py in _do_epoch_validate(self, ds_idx, dl)
    173             dl,old,has = change_attrs(dl, names, [False,False])
    174             self.dl = dl;                                    self('begin_validate')
--&gt; 175             with torch.no_grad(): self.all_batches()
    176         except CancelValidException:                         self('after_cancel_validate')
    177         finally:

/mnt/c/fastai2/fastai2/learner.py in all_batches(self)
    141     def all_batches(self):
    142         self.n_iter = len(self.dl)
--&gt; 143         for o in enumerate(self.dl): self.one_batch(*o)
    144 
    145     def one_batch(self, i, b):

/mnt/c/fastai2/fastai2/learner.py in one_batch(self, i, b)
    149             self.pred = self.model(*self.xb);                self('after_pred')
    150             if len(self.yb) == 0: return
--&gt; 151             self.loss = self.loss_func(self.pred, *self.yb); self('after_loss')
    152             if not self.training: return
    153             self.loss.backward();                            self('after_backward')

/mnt/c/fastai2/fastai2/layers.py in __call__(self, inp, targ, **kwargs)
    291         if targ.dtype in [torch.int8, torch.int16, torch.int32]: targ = targ.long()
    292         if self.flatten: inp = inp.view(-1,inp.shape[-1]) if self.is_2d else inp.view(-1)
--&gt; 293         return self.func.__call__(inp, targ.view(-1) if self.flatten else targ, **kwargs)
    294 
    295 # Cell

~/anaconda3/envs/py3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--&gt; 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~/anaconda3/envs/py3/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    914     def forward(self, input, target):
    915         return F.cross_entropy(input, target, weight=self.weight,
--&gt; 916                                ignore_index=self.ignore_index, reduction=self.reduction)
    917 
    918 

~/anaconda3/envs/py3/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2019     if size_average is not None or reduce is not None:
   2020         reduction = _Reduction.legacy_get_string(size_average, reduce)
-&gt; 2021     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
   2022 
   2023 

~/anaconda3/envs/py3/lib/python3.6/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   1836                          .format(input.size(0), target.size(0)))
   1837     if dim == 2:
-&gt; 1838         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   1839     elif dim == 4:
   1840         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

IndexError: Target -1 is out of bounds.


Any clue would be much appreciated! Thanks

Finally got figured this out, this happened because my validation set accidentally had more dependent variable classes than in my training set (or may be it was the other way around)......To fix this I had to ensure that my class size of training set and validation set are the same 

i:e make sure you do this check 

len(train_df["my_category"].unique()) == len(valid_df["my_category"].unique())

