众所周知,bert预训练有加入了两个下游任务进行训练,分别是next sentence prediction和mask prediction。

  • next sentence prediction:输入[CLS]a[SEP]b[SEP],预测b是否为a的下一句,即二分类问题;
  • mask prediction:输入[CLS]我 mask 中 mask 天 安 门[SEP],预测句子的mask,多分类问题

一直很困惑bert在经过transformer的encoder之后的两个任务是怎么做的。比如:

  • 这两个任务的loss函数是什么?
  • 拿到encoder输出做了什么?

很多人都是这么回答的:

  • bert的loss就是下一句预测的分类loss和mask词的预测loss
  • 拿encoder输出直接算loss啊

如果你继续问:

  • 那他们的loss都分别怎么算的?

相信很多人都讲不出来。下面就详细在这阐述下。相信你会对bert更了解的。

源码解读:

BertModel类中的forward部分代码如下

encoded_layers表示encoder所有层隐层状态输出;sequence_output表示最后一层。首先经过一个self.pooler变换:


注意看:将sequence_output输入BertPooler这个类之后,取了句子第一个token(first_token_tensor,即CLS)的向量,然后将first_token_tensor输入self.dense。从该类的初始化中可知,self.dense是一个线性层,参数为768 * 768。经过该线性层之后,又过了一个Tanh激活函数。然后得到pooled_output(1 * 768 )。

经过BertModel类之后,返回了两个输出sequence_output(len * 768)和pooled_output(1 * 768 )。

然后经过self.cls,即类BertPreTrainingHeads。

经过BertPreTrainingHeads时,先进过self.predictions。 即类BertLMPredictionHead:

在BertLMPredictionHead里面又经过self.transformer和self.decoder

self.transformer中经历了self.dense(768 * 768线性层),self.transform_act_fn激活函数(根据bert config,这里应该为gelu),self.LayerNorm(归一化处理)

self.decoder中经历了一个线性层(768 * vocab_size),因为
bert_model_embedding_weights共享了词向量参数,因此该线性层为768 * vocab_size。

终于走到底了,那么现在开始往回走:重新给梳理下,从下往上走:
2个输入:
1、sequence_output(len * 768 ):最后一层隐层状态。依次经过self.transformer(768 * 768线性层) -> self.transform_act_fn(tanh激活函数) -> self.LayerNorm(归一化) -> self.decoder(768 * vocab_size,共享词向量参数)
经过这一系列之后,我们就拿到了prediction_scores(len * vocab_size)然后我们就可以直接算loss了,采用的是CrossEntropyLoss交叉熵函数。

2、pooled_output(1 * 768 ):CLS隐层状态。依次经过self.seq_relationship(768 * 2线性层)
经过这一系列之后,我们就拿到了seq_relationship_score(1 * 2 ),二分类。然后我们就可以直接算loss了,采用的同样是CrossEntropyLoss交叉熵函数。

最后,loss = 上述两部分的loss之和

因此,这也就解释了,为什么bert的参数中包含了以下几个参数了

  • bert.pooler.dense.weight
  • bert.pooler.dense.bias
  • classifier.weight
  • classifier.bias

如果不正确之处,请不吝指正,感激不尽,谢谢!

下面是所有bert预训练参数:

bert.embeddings.word_embeddings.weight
bert.embeddings.position_embeddings.weight
bert.embeddings.token_type_embeddings.weight
bert.embeddings.LayerNorm.weight
bert.embeddings.LayerNorm.bias
bert.encoder.layer.0.attention.self.query.weight
bert.encoder.layer.0.attention.self.query.bias
bert.encoder.layer.0.attention.self.key.weight
bert.encoder.layer.0.attention.self.key.bias
bert.encoder.layer.0.attention.self.value.weight
bert.encoder.layer.0.attention.self.value.bias
bert.encoder.layer.0.attention.output.dense.weight
bert.encoder.layer.0.attention.output.dense.bias
bert.encoder.layer.0.attention.output.LayerNorm.weight
bert.encoder.layer.0.attention.output.LayerNorm.bias
bert.encoder.layer.0.intermediate.dense.weight
bert.encoder.layer.0.intermediate.dense.bias
bert.encoder.layer.0.output.dense.weight
bert.encoder.layer.0.output.dense.bias
bert.encoder.layer.0.output.LayerNorm.weight
bert.encoder.layer.0.output.LayerNorm.bias
bert.encoder.layer.1.attention.self.query.weight
bert.encoder.layer.1.attention.self.query.bias
bert.encoder.layer.1.attention.self.key.weight
bert.encoder.layer.1.attention.self.key.bias
bert.encoder.layer.1.attention.self.value.weight
bert.encoder.layer.1.attention.self.value.bias
bert.encoder.layer.1.attention.output.dense.weight
bert.encoder.layer.1.attention.output.dense.bias
bert.encoder.layer.1.attention.output.LayerNorm.weight
bert.encoder.layer.1.attention.output.LayerNorm.bias
bert.encoder.layer.1.intermediate.dense.weight
bert.encoder.layer.1.intermediate.dense.bias
bert.encoder.layer.1.output.dense.weight
bert.encoder.layer.1.output.dense.bias
bert.encoder.layer.1.output.LayerNorm.weight
bert.encoder.layer.1.output.LayerNorm.bias
bert.encoder.layer.2.attention.self.query.weight
bert.encoder.layer.2.attention.self.query.bias
bert.encoder.layer.2.attention.self.key.weight
bert.encoder.layer.2.attention.self.key.bias
bert.encoder.layer.2.attention.self.value.weight
bert.encoder.layer.2.attention.self.value.bias
bert.encoder.layer.2.attention.output.dense.weight
bert.encoder.layer.2.attention.output.dense.bias
bert.encoder.layer.2.attention.output.LayerNorm.weight
bert.encoder.layer.2.attention.output.LayerNorm.bias
bert.encoder.layer.2.intermediate.dense.weight
bert.encoder.layer.2.intermediate.dense.bias
bert.encoder.layer.2.output.dense.weight
bert.encoder.layer.2.output.dense.bias
bert.encoder.layer.2.output.LayerNorm.weight
bert.encoder.layer.2.output.LayerNorm.bias
bert.encoder.layer.3.attention.self.query.weight
bert.encoder.layer.3.attention.self.query.bias
bert.encoder.layer.3.attention.self.key.weight
bert.encoder.layer.3.attention.self.key.bias
bert.encoder.layer.3.attention.self.value.weight
bert.encoder.layer.3.attention.self.value.bias
bert.encoder.layer.3.attention.output.dense.weight
bert.encoder.layer.3.attention.output.dense.bias
bert.encoder.layer.3.attention.output.LayerNorm.weight
bert.encoder.layer.3.attention.output.LayerNorm.bias
bert.encoder.layer.3.intermediate.dense.weight
bert.encoder.layer.3.intermediate.dense.bias
bert.encoder.layer.3.output.dense.weight
bert.encoder.layer.3.output.dense.bias
bert.encoder.layer.3.output.LayerNorm.weight
bert.encoder.layer.3.output.LayerNorm.bias
bert.encoder.layer.4.attention.self.query.weight
bert.encoder.layer.4.attention.self.query.bias
bert.encoder.layer.4.attention.self.key.weight
bert.encoder.layer.4.attention.self.key.bias
bert.encoder.layer.4.attention.self.value.weight
bert.encoder.layer.4.attention.self.value.bias
bert.encoder.layer.4.attention.output.dense.weight
bert.encoder.layer.4.attention.output.dense.bias
bert.encoder.layer.4.attention.output.LayerNorm.weight
bert.encoder.layer.4.attention.output.LayerNorm.bias
bert.encoder.layer.4.intermediate.dense.weight
bert.encoder.layer.4.intermediate.dense.bias
bert.encoder.layer.4.output.dense.weight
bert.encoder.layer.4.output.dense.bias
bert.encoder.layer.4.output.LayerNorm.weight
bert.encoder.layer.4.output.LayerNorm.bias
bert.encoder.layer.5.attention.self.query.weight
bert.encoder.layer.5.attention.self.query.bias
bert.encoder.layer.5.attention.self.key.weight
bert.encoder.layer.5.attention.self.key.bias
bert.encoder.layer.5.attention.self.value.weight
bert.encoder.layer.5.attention.self.value.bias
bert.encoder.layer.5.attention.output.dense.weight
bert.encoder.layer.5.attention.output.dense.bias
bert.encoder.layer.5.attention.output.LayerNorm.weight
bert.encoder.layer.5.attention.output.LayerNorm.bias
bert.encoder.layer.5.intermediate.dense.weight
bert.encoder.layer.5.intermediate.dense.bias
bert.encoder.layer.5.output.dense.weight
bert.encoder.layer.5.output.dense.bias
bert.encoder.layer.5.output.LayerNorm.weight
bert.encoder.layer.5.output.LayerNorm.bias
bert.encoder.layer.6.attention.self.query.weight
bert.encoder.layer.6.attention.self.query.bias
bert.encoder.layer.6.attention.self.key.weight
bert.encoder.layer.6.attention.self.key.bias
bert.encoder.layer.6.attention.self.value.weight
bert.encoder.layer.6.attention.self.value.bias
bert.encoder.layer.6.attention.output.dense.weight
bert.encoder.layer.6.attention.output.dense.bias
bert.encoder.layer.6.attention.output.LayerNorm.weight
bert.encoder.layer.6.attention.output.LayerNorm.bias
bert.encoder.layer.6.intermediate.dense.weight
bert.encoder.layer.6.intermediate.dense.bias
bert.encoder.layer.6.output.dense.weight
bert.encoder.layer.6.output.dense.bias
bert.encoder.layer.6.output.LayerNorm.weight
bert.encoder.layer.6.output.LayerNorm.bias
bert.encoder.layer.7.attention.self.query.weight
bert.encoder.layer.7.attention.self.query.bias
bert.encoder.layer.7.attention.self.key.weight
bert.encoder.layer.7.attention.self.key.bias
bert.encoder.layer.7.attention.self.value.weight
bert.encoder.layer.7.attention.self.value.bias
bert.encoder.layer.7.attention.output.dense.weight
bert.encoder.layer.7.attention.output.dense.bias
bert.encoder.layer.7.attention.output.LayerNorm.weight
bert.encoder.layer.7.attention.output.LayerNorm.bias
bert.encoder.layer.7.intermediate.dense.weight
bert.encoder.layer.7.intermediate.dense.bias
bert.encoder.layer.7.output.dense.weight
bert.encoder.layer.7.output.dense.bias
bert.encoder.layer.7.output.LayerNorm.weight
bert.encoder.layer.7.output.LayerNorm.bias
bert.encoder.layer.8.attention.self.query.weight
bert.encoder.layer.8.attention.self.query.bias
bert.encoder.layer.8.attention.self.key.weight
bert.encoder.layer.8.attention.self.key.bias
bert.encoder.layer.8.attention.self.value.weight
bert.encoder.layer.8.attention.self.value.bias
bert.encoder.layer.8.attention.output.dense.weight
bert.encoder.layer.8.attention.output.dense.bias
bert.encoder.layer.8.attention.output.LayerNorm.weight
bert.encoder.layer.8.attention.output.LayerNorm.bias
bert.encoder.layer.8.intermediate.dense.weight
bert.encoder.layer.8.intermediate.dense.bias
bert.encoder.layer.8.output.dense.weight
bert.encoder.layer.8.output.dense.bias
bert.encoder.layer.8.output.LayerNorm.weight
bert.encoder.layer.8.output.LayerNorm.bias
bert.encoder.layer.9.attention.self.query.weight
bert.encoder.layer.9.attention.self.query.bias
bert.encoder.layer.9.attention.self.key.weight
bert.encoder.layer.9.attention.self.key.bias
bert.encoder.layer.9.attention.self.value.weight
bert.encoder.layer.9.attention.self.value.bias
bert.encoder.layer.9.attention.output.dense.weight
bert.encoder.layer.9.attention.output.dense.bias
bert.encoder.layer.9.attention.output.LayerNorm.weight
bert.encoder.layer.9.attention.output.LayerNorm.bias
bert.encoder.layer.9.intermediate.dense.weight
bert.encoder.layer.9.intermediate.dense.bias
bert.encoder.layer.9.output.dense.weight
bert.encoder.layer.9.output.dense.bias
bert.encoder.layer.9.output.LayerNorm.weight
bert.encoder.layer.9.output.LayerNorm.bias
bert.encoder.layer.10.attention.self.query.weight
bert.encoder.layer.10.attention.self.query.bias
bert.encoder.layer.10.attention.self.key.weight
bert.encoder.layer.10.attention.self.key.bias
bert.encoder.layer.10.attention.self.value.weight
bert.encoder.layer.10.attention.self.value.bias
bert.encoder.layer.10.attention.output.dense.weight
bert.encoder.layer.10.attention.output.dense.bias
bert.encoder.layer.10.attention.output.LayerNorm.weight
bert.encoder.layer.10.attention.output.LayerNorm.bias
bert.encoder.layer.10.intermediate.dense.weight
bert.encoder.layer.10.intermediate.dense.bias
bert.encoder.layer.10.output.dense.weight
bert.encoder.layer.10.output.dense.bias
bert.encoder.layer.10.output.LayerNorm.weight
bert.encoder.layer.10.output.LayerNorm.bias
bert.encoder.layer.11.attention.self.query.weight
bert.encoder.layer.11.attention.self.query.bias
bert.encoder.layer.11.attention.self.key.weight
bert.encoder.layer.11.attention.self.key.bias
bert.encoder.layer.11.attention.self.value.weight
bert.encoder.layer.11.attention.self.value.bias
bert.encoder.layer.11.attention.output.dense.weight
bert.encoder.layer.11.attention.output.dense.bias
bert.encoder.layer.11.attention.output.LayerNorm.weight
bert.encoder.layer.11.attention.output.LayerNorm.bias
bert.encoder.layer.11.intermediate.dense.weight
bert.encoder.layer.11.intermediate.dense.bias
bert.encoder.layer.11.output.dense.weight
bert.encoder.layer.11.output.dense.bias
bert.encoder.layer.11.output.LayerNorm.weight
bert.encoder.layer.11.output.LayerNorm.bias
bert.pooler.dense.weight
bert.pooler.dense.bias
cls.predictions.bias
cls.predictions.transform.dense.weight
cls.predictions.transform.dense.bias
cls.predictions.transform.LayerNorm.gamma
cls.predictions.transform.LayerNorm.beta
cls.predictions.decoder.weight
cls.seq_relationship.weight
cls.seq_relationship.bias

bert在预训练时的两个下游任务详解相关推荐

  1. Bert在fine-tune训练时的技巧:①冻结部分层参数、②weight-decay (L2正则化)、③warmup_proportion、④

    作为一个NLPer,bert应该是会经常用到的一个模型了.但bert可调参数很多,一些技巧也很多,比如加上weight-decay, layer初始化.冻结参数.只优化部分层参数等等,方法太多了,每次 ...

  2. [深度学习] 自然语言处理 --- Huggingface-Pytorch中文语言Bert模型预训练

    Hugging face 是一家总部位于纽约的聊天机器人初创服务商,开发的应用在青少年中颇受欢迎,相比于其他公司,Hugging Face更加注重产品带来的情感以及环境因素.官网链接在此 https: ...

  3. perplexity和预训练时用的loss的区别

    Perplexity和预训练时用的loss都是用来评估语言模型的性能的指标,但是它们的计算方式和意义有所不同. Perplexity是一种用来衡量语言模型对一个测试集的预测能力的指标.它的计算方式是将 ...

  4. BERT(预训练Transformer模型)

    目录 一.前言 二.随机遮挡,进行预测 三.两句话是否原文相邻 四.两者结合起来 五.总结 六.参考链接 一.前言 Bert在18年提出,19年发表,Bert的目的是为了预训练Transformer模 ...

  5. linux apache两种工作模式详解

    apache两种工作模式详解 刚接触这两个配置时很迷糊,全部开启或全部注释没有几多变化.今天搜索到这么一篇讲得还不错的文章,看了几篇,还是不能完全记住,做一个收藏. 空闲子进程:是指没有正在处理请求的 ...

  6. java的websocket_java 实现websocket的两种方式实例详解

    一.介绍 1.两种方式,一种使用tomcat的websocket实现,一种使用spring的websocket 2.tomcat的方式需要tomcat 7.x,JEE7的支持. 3.spring与we ...

  7. python类的命名空间_Python之关于类变量的两种赋值区别详解

    我就废话不多说了,还是直接看代码吧! # -*- coding:utf-8 -*- #面试题,写一个方法,将一行字符串中所有的单词数量统计出来 class Person(object): TAG = ...

  8. vuecli 编译后部署_基于vue-cli 打包时抽离项目相关配置文件详解

    前言:当使用vue-cli进行开发时时常需要动态配置一些设置,比如接口的请求地址(axios.defaults.baseURL),这些设置可能需要在项目编译后再进行设置的,所以在vue-cli里我们需 ...

  9. 约瑟夫环问题的两种解法(详解)

    约瑟夫环问题的两种解法(详解) 题目: Josephus有过的故事:39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓.于是决定了自杀方式,41个人排成一个圆 ...

最新文章

  1. XGBoost4J-Spark基本原理
  2. mysql额外在哪_MySQL额外操作
  3. Pod详解-生命周期-创建和终止
  4. iOS中self.xxx 和 _xxx 下划线的区别
  5. 好好把握人生的12种财富
  6. Linux目录结构及解释
  7. 【工业串口和网络软件通讯平台(SuperIO)教程】二.架构和组成部分
  8. 丰巢人工智能刷脸取件被小学生破解
  9. 浏览器全面禁止第三方Cookie
  10. 商业智能BI全解析,探寻BI本质与发展趋势
  11. Android 10 默认输入法
  12. iOS中打一个包上传后,iTunes中找不到上传的包的解决方法
  13. MS COCO数据集输出数据的结果格式(result format)和如何参加比赛(participate)(来自官网)
  14. create-react-app配置总结
  15. html+js+css 调用jquery 工人信息管理功能(增删改查)前端实现,以及调用实现鼠标拖尾粒子效果的js库
  16. vue+element el-menu递归多层菜单$emit事件失效
  17. 童话故事 --- CPU的贴身侍卫ITCM和ICache
  18. 【多线程】Volatile和Happens-Before原则
  19. HEVC/H265帧类型判断及NALU TYPE介绍
  20. 一个硕士是怎么发5篇sci的,谈谈研究生感悟

热门文章

  1. 如何恢复CAD软件系统参数?
  2. 百丽时尚×优维科技×道客战略启动「云原生一体化项目」
  3. HTML5 页面video标签视频加载播放空白
  4. 张国荣 继续宠爱●十年 音乐专辑
  5. php从服务器下载图片到本地
  6. 小米是android版本,小米运动
  7. 正则表达式以及在C语言中调用相关函数
  8. 你的蛙以后会寄中国明信片?我们采访了团队制作人,他这样说……
  9. PDF 文件无法打印、转换、合并、修改,提示“PDF文件已被保护” 密码移除
  10. wps居中对齐不在中间_wps文字插入表格单元格内容不能居中(无法居中对齐)