微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Fastai v2 IndexError:目标 2 越界 软件包版本和参考页面错误信息代码

如何解决Fastai v2 IndexError:目标 2 越界 软件包版本和参考页面错误信息代码

软件包版本和参考页面

嗨,我指的是将 HuggingFace Transformers 模型与 Fastai 微调策略相结合的页面 Blurr。以下是软件包的版本:

  • 变形金刚:4.2.2
  • fastai:2.1.10
  • 欧姆模糊:0.0.22

Blurr 中的示例代码与我的示例代码间的区别在于,我正在尝试使用 4 类总标签解决多类单标签文本分类问题,而教程是二元分类问题。

用于训练和验证的数据集有 3 列:texts、label、is_valid。

  • texts 包含类似于 Blurr 的文本数据。
  • label 包含 4 类字符串标签,对于每个原始数据,只有一个标签
  • is_valid 包含布尔值“True 和 False”,指示这些原始数据是用于训练还是验证。

错误信息

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-16-fc93db149c6f> in <module>
     13 learn.freeze()
     14 
---> 15 learn.fit_one_cycle(3,lr_max=1e-3)

~/fastai/fastai/callback/schedule.py in fit_one_cycle(self,n_epoch,lr_max,div,div_final,pct_start,wd,moms,cbs,reset_opt)
    110     scheds = {'lr': combined_cos(pct_start,lr_max/div,lr_max/div_final),111               'mom': combined_cos(pct_start,*(self.moms if moms is None else moms))}
--> 112     self.fit(n_epoch,cbs=ParamScheduler(scheds)+L(cbs),reset_opt=reset_opt,wd=wd)
    113 
    114 # Cell

~/fastai/fastai/learner.py in fit(self,lr,reset_opt)
    204             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    205             self.n_epoch = n_epoch
--> 206             self._with_events(self._do_fit,'fit',CancelFitException,self._end_cleanup)
    207 
    208     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),None,None

~/fastai/fastai/learner.py in _with_events(self,f,event_type,ex,final)
    153 
    154     def _with_events(self,final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

~/fastai/fastai/learner.py in _do_fit(self)
    195         for epoch in range(self.n_epoch):
    196             self.epoch=epoch
--> 197             self._with_events(self._do_epoch,'epoch',CancelEpochException)
    198 
    199     def fit(self,lr=None,wd=None,cbs=None,reset_opt=False):

~/fastai/fastai/learner.py in _with_events(self,final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

~/fastai/fastai/learner.py in _do_epoch(self)
    189 
    190     def _do_epoch(self):
--> 191         self._do_epoch_train()
    192         self._do_epoch_validate()
    193 

~/fastai/fastai/learner.py in _do_epoch_train(self)
    181     def _do_epoch_train(self):
    182         self.dl = self.dls.train
--> 183         self._with_events(self.all_batches,'train',CancelTrainException)
    184 
    185     def _do_epoch_validate(self,ds_idx=1,dl=None):

~/fastai/fastai/learner.py in _with_events(self,final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

~/fastai/fastai/learner.py in all_batches(self)
    159     def all_batches(self):
    160         self.n_iter = len(self.dl)
--> 161         for o in enumerate(self.dl): self.one_batch(*o)
    162 
    163     def _do_one_batch(self):

~/fastai/fastai/learner.py in one_batch(self,i,b)
    177         self.iter = i
    178         self._split(b)
--> 179         self._with_events(self._do_one_batch,'batch',CancelBatchException)
    180 
    181     def _do_epoch_train(self):

~/fastai/fastai/learner.py in _with_events(self,final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

~/fastai/fastai/learner.py in _do_one_batch(self)
    164         self.pred = self.model(*self.xb)
    165         self('after_pred')
--> 166         if len(self.yb): self.loss = self.loss_func(self.pred,*self.yb)
    167         self('after_loss')
    168         if not self.training or not len(self.yb): return

~/fastai/fastai/losses.py in __call__(self,inp,targ,**kwargs)
     31         if targ.dtype in [torch.int8,torch.int16,torch.int32]: targ = targ.long()
     32         if self.flatten: inp = inp.view(-1,inp.shape[-1]) if self.is_2d else inp.view(-1)
---> 33         return self.func.__call__(inp,targ.view(-1) if self.flatten else targ,**kwargs)
     34 
     35 # Cell

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self,*input,**kwargs)
    725             result = self._slow_forward(*input,**kwargs)
    726         else:
--> 727             result = self.forward(*input,**kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/loss.py in forward(self,input,target)
    959 
    960     def forward(self,input: Tensor,target: Tensor) -> Tensor:
--> 961         return F.cross_entropy(input,target,weight=self.weight,962                                ignore_index=self.ignore_index,reduction=self.reduction)
    963 

~/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py in cross_entropy(input,weight,size_average,ignore_index,reduce,reduction)
   2460         tens_ops = (input,target)
   2461         if any([type(t) is not Tensor for t in tens_ops]) and has_torch_function(tens_ops):
-> 2462             return handle_torch_function(
   2463                 cross_entropy,tens_ops,weight=weight,2464                 size_average=size_average,ignore_index=ignore_index,reduce=reduce,~/anaconda3/lib/python3.8/site-packages/torch/overrides.py in handle_torch_function(public_api,relevant_args,*args,**kwargs)
   1058         # Use `public_api` instead of `implementation` so __torch_function__
   1059         # implementations can do equality/identity comparisons.
-> 1060         result = overloaded_arg.__torch_function__(public_api,types,args,kwargs)
   1061 
   1062         if result is not NotImplemented:

~/fastai/fastai/torch_core.py in __torch_function__(self,func,kwargs)
    317 #         if func.__name__[0]!='_': print(func,kwargs)
    318 #         with torch._C.disabletorchFunction(): ret = _convert(func(*args,**(kwargs or {})),self.__class__)
--> 319         ret = super().__torch_function__(func,args=args,kwargs=kwargs)
    320         if isinstance(ret,TensorBase): ret.set_Meta(self,as_copy=True)
    321         return ret

~/anaconda3/lib/python3.8/site-packages/torch/tensor.py in __torch_function__(cls,kwargs)
    993 
    994         with _C.disabletorchFunction():
--> 995             ret = func(*args,**kwargs)
    996             return _convert(ret,cls)
    997 

~/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py in cross_entropy(input,reduction)
   2466     if size_average is not None or reduce is not None:
   2467         reduction = _Reduction.legacy_get_string(size_average,reduce)
-> 2468     return nll_loss(log_softmax(input,1),reduction)
   2469 
   2470 

~/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py in nll_loss(input,reduction)
   2262                          .format(input.size(0),target.size(0)))
   2263     if dim == 2:
-> 2264         ret = torch._C._nn.nll_loss(input,_Reduction.get_enum(reduction),ignore_index)
   2265     elif dim == 4:
   2266         ret = torch._C._nn.nll_loss2d(input,ignore_index)

IndexError: Target 2 is out of bounds.

代码

learn.fit_one_cycle(3,lr_max=1e-3) 中,它显示了上面的错误消息。通过检查 len(df[df['is_valid']==False]['label'].unique()) == len(df[df['is_valid']==True]['label'].unique()) 并分别打印,我确信训练集和验证集都具有相同的标签

import os
import torch
from transformers import *
from fastai.text.all import *

from blurr.data.all import *
from blurr.modeling.all import *

task = HF_TASKS_AUTO.SequenceClassification
pretrained_model_name = "bert-base-uncased"
hf_arch,hf_config,hf_tokenizer,hf_model = BLURR_MODEL_HELPER.get_hf_objects(pretrained_model_name,task=task)

# data loader
blocks = (HF_TextBlock(hf_arch,hf_model),CategoryBlock)
dblock = DataBlock(blocks=blocks,get_x=ColReader('texts'),get_y=ColReader('label'),splitter=ColSplitter('is_valid'))

dls = dblock.DataLoaders(df,batch_size=4)

model = HF_BaseModelWrapper(hf_model)

learn = Learner(dls,model,opt_func=partial(Adam,decouple_wd=True),loss_func=CrossEntropyLossFlat(),metrics=[accuracy,F1score(average='macro')],cbs=[HF_BaseModelCallback],splitter=hf_splitter)

learn.create_opt()  
learn.freeze()

learn.fit_one_cycle(3,lr_max=1e-3)

有没有人遇到过类似的问题?我在代码中丢失了什么吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。