IndexError: Целевая ошибка -1 выходит за границы в табличной форме обучаемого fatai2 - PullRequest
0 голосов
/ 21 апреля 2020

Получение ошибки ниже при попытке установить tabular_learner из библиотеки fastai2.

используемые загрузчики данных

enter image description here

learn = tabular_learner (dls, Layers = [1000 500], метрики = точность)

learn.fit (30,1e-2)

IndexError                                Traceback (most recent call last)
<ipython-input-35-f0c57ab3748f> in <module>
----> 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()
--> 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')
--> 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)
--> 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
--> 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)
--> 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:
--> 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,
--> 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)
-> 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:
-> 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.

Любая подсказка будет принята с благодарностью! Спасибо

1 Ответ

0 голосов
/ 24 апреля 2020

Наконец-то выяснили это, это произошло потому, что в моем наборе валидации случайно было больше классов зависимых переменных, чем в моем тренировочном наборе (или, может быть, это было наоборот) ...... Чтобы это исправить, я должен был убедиться, что что размер моего учебного набора и набора проверки одинаковы

я: убедитесь, что вы делаете эту проверку

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