fastNLP icon indicating copy to clipboard operation
fastNLP copied to clipboard

predictor 预测报错

Open songsh opened this issue 4 years ago • 7 comments

我在用BertForQuestionAnswering 预测时 我是想要答案的,但是结果返回一堆数字 代码如下:

`from fastNLP.embeddings import BertEmbedding from fastNLP.models import BertForQuestionAnswering from fastNLP.core.losses import CMRC2018Loss from fastNLP.core.metrics import CMRC2018Metric from fastNLP.io.pipe.qa import CMRC2018BertPipe from fastNLP import Trainer, BucketSampler from fastNLP import WarmupCallback, GradientClipCallback from fastNLP.core.optimizer import AdamW import torch from fastNLP.io.model_io import ModelSaver from fastNLP.io.model_io import ModelLoader from fastNLP.core.predictor import Predictor from fastNLP import BucketSampler from fastNLP import DataSetIter

from fastNLP.core.dataset import DataSet from fastNLP.core.instance import Instance from fastNLP import Vocabulary

data_bundle = CMRC2018BertPipe().process_from_file() data_bundle.rename_field('chars', 'words')

instance=data_bundle.get_dataset('train').getitem(1) def prepare_fake_dataset(): vocab = data_bundle.get_vocab('words') context='《战国无双3》()是由光荣和ω-force开发的战国无双系列的正统第三续作。本作以三大故事为主轴,分别是以武田信玄等人为主的《关东三国志》,织田信长等人为主的《战国三杰》,石田三成等人为主的《关原的年轻武者》,丰富游戏内的剧情。此部份专门介绍角色,欲知武器情报、奥义字或擅长攻击类型等,请至战国无双系列1.由于乡里大辅先生因故去世,不得不寻找其他声优接手。从猛将传 and Z开始。2.战国无双 编年史的原创男女主角亦有专属声优。此模式是任天堂游戏谜之村雨城改编的新增模式。本作中共有20张战场地图(不含村雨城),后来发行的猛将传再新增3张战场地图。但游戏内战役数量繁多,部分地图会有兼用的状况,战役虚实则是以光荣发行的2本「战国无双3 人物真书」内容为主,以下是相关介绍。(注:前方加☆者为猛将传新增关卡及地图。)合并本篇和猛将传的内容,村雨城模式剔除,战国史模式可直接游玩。主打两大模式「战史演武」&「争霸演武」。系列作品外传作品'

data_set = DataSet([Instance(title='',
context=context,question='《战国无双3》是由哪两个公司合作开发的?')])
list1=list(context)
list2=[list1]
data_set.add_field('words',list2)
vocab.index_dataset(data_set, field_name='words')
print(data_set)
return data_set

embed = BertEmbedding(data_bundle.get_vocab('words'), model_dir_or_name='cn', requires_grad=True, include_cls_sep=False, auto_truncate=True, dropout=0.5, word_dropout=0.01) model = BertForQuestionAnswering(embed) loss = CMRC2018Loss() metric = CMRC2018Metric()

modelLoader=ModelLoader() modelLoader.load_pytorch(model,'model/model1.pkl')

predictor = Predictor(model) data = prepare_fake_dataset() data.set_input("context","question","words") ans = predictor.predict(data) print(ans) `

efaultdict(<class 'list'>, {'pred_start': [array([[-4.7147093, -4.4635706, -7.3092337, -6.9669356, -8.095401 , -6.820217 , -7.989754 , -7.2523155, -7.2693043, -5.87125 , -6.137492 , -5.6169624, -7.930938 , -7.567548 , -7.066292 , -7.808277 , -7.630378 , -7.7889004, -7.78243 , -7.7503047, -8.478406 , -7.4418545, -8.324611 , -7.8676033, -4.955612 , -7.46612 , -7.253308 , -8.177212 , -6.600925 , -8.167936 , -7.489729 , -6.4176855, -7.8482227, -6.6474295, -7.935684 , -7.702264 , -8.386292 , -8.347514 , -4.537798 , -7.196664 , -5.720659 , -6.0188417, -7.8715553, -7.562133 , -8.312002 , -7.5542316, -7.4076543, -8.400216 , -7.6481533, -5.4345045, -7.251315 , -6.859527 , -5.2960033, -5.5886755, -7.413539 , -7.5307817, -8.45147 , -7.777984 , -8.314718 , -7.656053 , -8.215085 , -7.8636026, -4.6867647, -6.6244097, -7.794947 , -7.60705 , -8.076643 , -8.242118 , -8.425303 , -7.2666025, -6.2899275, -7.6495104, -7.719425 , -8.269446 , -7.774742 , -8.261978 , -7.695258 , -8.131538 , -8.001849 , -5.704524 , -6.7971497, -7.7787113, -7.85799 , -8.3356695, -8.445191 , -7.401602 , -6.2489095, -7.6873555, -7.7742987, -8.45337 , -7.7811885, -8.288642 , -7.771181 , -8.152343 , -8.01656 , -5.698987 , -6.399955 , -8.019159 , -7.657894 , -7.483568 , -7.9722424, -7.8437767, -8.391513 , -8.3743925, -7.5895834, -5.9450264, -7.515527 , -6.815183 , -7.872198 , -7.8869267, -8.107394 , -7.6159906, -8.4485445, -8.266912 , -4.910048 , -6.855068 , -7.760604 , -6.235383 , -7.634518 , -6.733061 , -7.783947 , -6.931315 , -8.074651 , -7.493991 , -5.951476 , -7.630535 , -6.744482 , -7.875449 , -7.608136 , -8.159413 , -7.711028 , -7.4634027, -7.9520817, -8.151088 , -7.8866577, -7.511101 , -8.01236 , -7.68281 , -8.173285 , -7.903372 , -8.371099 , -8.332081 , -7.5445347, -6.3632164, -6.6286592, -5.826534 , -7.3725243, -7.4565763, -8.226766 , -7.0788603, -8.125027 , -5.9246387, -7.790598 , -5.3822575, -7.26466 ,

不知这数字如何转为答案

songsh avatar Jun 14 '20 04:06 songsh

pred_start取argmax得到start的index, pred_end也许argmax得到end的index; 然后再从raw_words中取出raw_words[start:end]这样。

yhcc avatar Jun 14 '20 17:06 yhcc

感谢, 你这说的raw_words 就是我程序中 words 字段吧。还有argmax 是在哪个api ?

songsh avatar Jun 15 '20 03:06 songsh

argmax就是pytorch的argmax,tensor.argmax(dim=-1)这样,或者torch.argmax(tensor, dim=-1)

yhcc avatar Jun 17 '20 04:06 yhcc

pend=torch.Tensor(ans['pred_end']) pstart=torch.Tensor(ans['pred_start']) print(torch.argmax(pend,dim=-1)) print(torch.argmax(pstart,dim=-1))

代码这么写的, 我问了两个问题, 返回结果都是 1,113 , 哪有问题?

songsh avatar Jun 20 '20 00:06 songsh

之前我说的做法有误,因为在阅读理解中一定不会出现start在end后面的情况。所以可以这样

pend=torch.Tensor(ans['pred_end'])  # 应该是一个length长的vecctor
pstart=torch.Tensor(ans['pred_start'])

start = pstart.argmax(dim=-1)  
end = pend[start+1:].argmax(dim=-1)

不过全部预测1和113的问题应该不是这个导致的。你训练结束后em和f1分别是多少呢?

yhcc avatar Jun 22 '20 11:06 yhcc

f1=86.52, em=66.85

songsh avatar Jun 27 '20 13:06 songsh

我又训练了一遍,现在取值都是103, 154 . pend=torch.Tensor(ans['pred_end']) # 应该是一个length长的vecctor pstart=torch.Tensor(ans['pred_start'])

start = pstart.argmax(dim=-1)
end = pend[start+1:].argmax(dim=-1) 报错: index 154 is out of bounds for dimension 0 with size 1

songsh avatar Jun 28 '20 00:06 songsh