记一次debug-‘mask of the first timestep must all be on’报错

在进行序列标注时,使用crf报错:

‘mask of the first timestep must all be on’

搜索相关问题,给出的大多是将输入参数batch_first设置为True。但是我的代码中本来就是True。因此问题不在这里。
这个报错是指mask的第一个值错误,不应该为0。这里进行debug查看了具体出错位置:

ipdb> n
> <ipython-input-60-103a41bbe73a>(46)crf_neg_log_likelihood()
     44             mask = mask.type(torch.uint8)
     45 
---> 46         crf_llh = self.crf(logits, tags, mask, reduction='mean') # Compute the conditional log likelihood of a sequence of tags given emission scores
     47         # crf_llh = self.crf(logits, tags, mask) # Compute the conditional log likelihood of a sequence of tags given emission scores
     48         return -crf_llh 
    
ipdb> n
ValueError: mask of the first timestep must all be on
> <ipython-input-60-103a41bbe73a>(46)crf_neg_log_likelihood()
     44             mask = mask.type(torch.uint8)
     45 
---> 46         crf_llh = self.crf(logits, tags, mask, reduction='mean') # Compute the conditional log likelihood of a sequence of tags given emission scores
     47         # crf_llh = self.crf(logits, tags, mask) # Compute the conditional log likelihood of a sequence of tags given emission scores
     48         return -crf_llh

说明这里输入的mask的第一位对应值错误,打印出来看看:
在这里插入图片描述

这里的false指是填充padding的内容。第一个tensor中,0表示padding,数字表示每个单词转换成的对应id。但是很明显第一个单词的id:0和用来表示填充的0冲突了,因此导致了这一问题:
在这里插入图片描述

修改后就好了:

word2id = {'<pad>': 0, '<unk>': 1}
for sentence in sentences:  # 建立word到索引的映射
    for word in sentence:
        if word not in word2id:
            word2id[word] = len(word2id)
            
print(word2id)
print(len(word2id))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值