ValueError:分类指标无法处理multiclass-multioutput和multilabel-indicator目标的混合

时间:2019-07-08 04:07:58

标签: python machine-learning scikit-learn fast-ai

一般来说,深度学习的入门者。

我正在尝试使用命令在图像数据集上训练我的模型

learn.fit_one_cycle(4,max_lr = 1e-2)

它训练了4个纪元,但最终失败,并显示以下内容

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-35-94fbcc169259> in <module>
----> 1 learn.fit_one_cycle(4,max_lr = 1e-2)

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\fastai\train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
     20     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
     21                                        final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
---> 22     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
     23 
     24 def lr_find(learn:Learner, start_lr:Floats=1e-7, end_lr:Floats=10, num_it:int=100, stop_div:bool=True, wd:float=None):

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\fastai\basic_train.py in fit(self, epochs, lr, wd, callbacks)
    198         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
    199         if defaults.extra_callbacks is not None: callbacks += defaults.extra_callbacks
--> 200         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
    201 
    202     def create_opt(self, lr:Floats, wd:Floats=0.)->None:

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\fastai\basic_train.py in fit(epochs, learn, callbacks, metrics)
    104             if not cb_handler.skip_validate and not learn.data.empty_val:
    105                 val_loss = validate(learn.model, learn.data.valid_dl, loss_func=learn.loss_func,
--> 106                                        cb_handler=cb_handler, pbar=pbar)
    107             else: val_loss=None
    108             if cb_handler.on_epoch_end(val_loss): break

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\fastai\basic_train.py in validate(model, dl, loss_func, cb_handler, pbar, average, n_batch)
     61             if not is_listy(yb): yb = [yb]
     62             nums.append(first_el(yb).shape[0])
---> 63             if cb_handler and cb_handler.on_batch_end(val_losses[-1]): break
     64             if n_batch and (len(nums)>=n_batch): break
     65         nums = np.array(nums, dtype=np.float32)

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\fastai\callback.py in on_batch_end(self, loss)
    306         "Handle end of processing one batch with `loss`."
    307         self.state_dict['last_loss'] = loss
--> 308         self('batch_end', call_mets = not self.state_dict['train'])
    309         if self.state_dict['train']:
    310             self.state_dict['iteration'] += 1

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\fastai\callback.py in __call__(self, cb_name, call_mets, **kwargs)
    248         "Call through to all of the `CallbakHandler` functions."
    249         if call_mets:
--> 250             for met in self.metrics: self._call_and_update(met, cb_name, **kwargs)
    251         for cb in self.callbacks: self._call_and_update(cb, cb_name, **kwargs)
    252 

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\fastai\callback.py in _call_and_update(self, cb, cb_name, **kwargs)
    239     def _call_and_update(self, cb, cb_name, **kwargs)->None:
    240         "Call `cb_name` on `cb` and update the inner state."
--> 241         new = ifnone(getattr(cb, f'on_{cb_name}')(**self.state_dict, **kwargs), dict())
    242         for k,v in new.items():
    243             if k not in self.state_dict:

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\fastai\callback.py in on_batch_end(self, last_output, last_target, **kwargs)
    342         if not is_listy(last_target): last_target=[last_target]
    343         self.count += first_el(last_target).size(0)
--> 344         val = self.func(last_output, *last_target)
    345         if self.world:
    346             val = val.clone()

<ipython-input-32-1b0d05942cf1> in quadratic_kappa(y_hat, y)
      1 from sklearn.metrics import cohen_kappa_score
      2 def quadratic_kappa(y_hat, y):
----> 3     return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0')

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\sklearn\metrics\classification.py in cohen_kappa_score(y1, y2, labels, weights, sample_weight)
    555     """
    556     confusion = confusion_matrix(y1, y2, labels=labels,
--> 557                                  sample_weight=sample_weight)
    558     n_classes = confusion.shape[0]
    559     sum0 = np.sum(confusion, axis=0)

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\sklearn\metrics\classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight)
    251 
    252     """
--> 253     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
    254     if y_type not in ("binary", "multiclass"):
    255         raise ValueError("%s is not supported" % y_type)

~\Anaconda3\envs\fastai_v1_2\lib\site-packages\sklearn\metrics\classification.py in _check_targets(y_true, y_pred)
     79     if len(y_type) > 1:
     80         raise ValueError("Classification metrics can't handle a mix of {0} "
---> 81                          "and {1} targets".format(type_true, type_pred))
     82 
     83     # We can't have more than one value on y_type => The set is no more needed

ValueError: Classification metrics can't handle a mix of multiclass-multioutput and multilabel-indicator targets

我意识到还有其他线程报告此错误,但是没有一个线程引用该命令

learn.fit()

此处唯一的输入是类型为 fastai.basic_train.Learner 的对象 learn ,历元数和最大学习率。我不明白我要去哪里错了。

0 个答案:

没有答案