BERT中是怎么做到只计算[MASK]token的CrossEntropyLoss的?及torch.nn.CrossEntropyLoss()参数

11 篇文章 4 订阅
11 篇文章 3 订阅

nn.CrossEntropyLoss()的参数

torch.nn.CrossEntropyLoss(weight=None, size_average=None,
ignore_index=-100, reduce=None, reduction=‘mean’)

  • weight:不必多说,这就是各class的权重。所以它的值必须满足两点:
    1. type = torch.Tensor
    2. weight.shape = tensor(1, class_num)
  • size_averagereduce :都要被弃用了,直接看 reduction就行
  • reduction:结果的规约方式,取值空间为{'mean', 'none', 'sum}。由于你传入 nn.CrossEntropyLoss()的输入是一个batch,那么按理说得到的交叉熵损失应该是 batch个loss。当前默认的处理方式是,对 batch 个损失取平均;也可以选择不做规约;或者将batch个损失取加和;
  • ignore_index :做交叉熵计算时,若输入为ignore_index指定的数值,则该数值会被忽略,不参与交叉熵计算。

BERT中是怎么做到只计算[MASK]token的CrossEntropyLoss的?

nn.CrossEntropyLoss()ignore_index参数在BERT的mask中用到了。由于BERT中其中一个预训练任务是MLM,只有15%的token被[MASK],所以说只有这15%的词会参与交叉熵loss的计算,其他85%不参与loss计算的槽位,就使用-1填充;而参与loss计算的槽位,会使用在 vocab.txt 里提前定义好的原始token对应的index表示,这些index都是大于101([CLS])的,所以计算时不会被ignore

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
为以下代码写注释:class TransformerClassifier(torch.nn.Module): def __init__(self, num_labels): super().__init__() self.bert = BertForSequenceClassification.from_pretrained('bert-base-chinese', num_labels=num_labels) # print(self.bert.config.hidden_size) #768 self.dropout = torch.nn.Dropout(0.1) self.classifier1 = torch.nn.Linear(640, 256) self.classifier2 = torch.nn.Linear(256, num_labels) self.regress1 = torch.nn.Linear(640, 256) self.regress2 = torch.nn.Linear(256, 2) self.regress3 = torch.nn.Linear(640, 256) self.regress4 = torch.nn.Linear(256, 2) # self.regress3 = torch.nn.Linear(64, 1) # self.regress3 = torch.nn.Linear(640, 256) # self.regress4 = torch.nn.Linear(256, 1) # self.soft1 = torch.nn.Softmax(dim=1) def forward(self, input_ids, attention_mask, token_type_ids): # outputs = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids) # pooled_output = outputs.logits # # pooled_output = self.dropout(pooled_output) # # logits = self.classifier(pooled_output) outputs = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids) logits = outputs.logits clas = F.relu(self.classifier1(logits)) clas = self.classifier2(clas) death = F.relu(self.regress1(logits)) # xingqi = F.relu(self.regress2(xingqi)) death = self.regress2(death) life = F.relu(self.regress3(logits)) # xingqi = F.relu(self.regress2(xingqi)) life = self.regress4(life) # fakuan = F.relu(self.regress3(logits)) # fakuan = self.regress4(fakuan) # print(logits.shape) # logits = self.soft1(logits) # print(logits) # print(logits.shape) return clas,death,life
05-25
注释如下: ``` class TransformerClassifier(torch.nn.Module): # 定义一个名为TransformerClassifier的继承自nn.Module的类 def __init__(self, num_labels): # 初始化函数,num_labels为标签数 super().__init__() # 调用父类的构造函数 self.bert = BertForSequenceClassification.from_pretrained('bert-base-chinese', num_labels=num_labels) # 用预训练的中文Bert模型初始化self.bert, 输出维度为num_labels # print(self.bert.config.hidden_size) #768 self.dropout = torch.nn.Dropout(0.1) # 定义dropout层,丢弃概率为0.1 self.classifier1 = torch.nn.Linear(640, 256) # 定义一个全连接层,输入维度为640,输出维度为256 self.classifier2 = torch.nn.Linear(256, num_labels) # 定义一个全连接层,输入维度为256,输出维度为num_labels self.regress1 = torch.nn.Linear(640, 256) # 定义一个全连接层,输入维度为640,输出维度为256 self.regress2 = torch.nn.Linear(256, 2) # 定义一个全连接层,输入维度为256,输出维度为2 self.regress3 = torch.nn.Linear(640, 256) # 定义一个全连接层,输入维度为640,输出维度为256 self.regress4 = torch.nn.Linear(256, 2) # 定义一个全连接层,输入维度为256,输出维度为2 def forward(self, input_ids, attention_mask, token_type_ids): # 前向传播函数,输入参数分别为input_ids、attention_masktoken_type_ids outputs = self.bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids) # 将输入传入self.bert中,得到输出 logits = outputs.logits # 从输出中得到logits clas = F.relu(self.classifier1(logits)) # 将logits输入到self.classifier1中,经过relu函数后得到clas clas = self.classifier2(clas) # 将clas输入到self.classifier2中,得到分类结果 death = F.relu(self.regress1(logits)) # 将logits输入到self.regress1中,经过relu函数后得到death death = self.regress2(death) # 将death输入到self.regress2中,得到死亡概率 life = F.relu(self.regress3(logits)) # 将logits输入到self.regress3中,经过relu函数后得到life life = self.regress4(life) # 将life输入到self.regress4中,得到生存概率 return clas, death, life # 返回分类结果、死亡概率、生存概率

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_illusion_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值