【源码解读】BertLayer
总结
- 分析
BertLayer
元的实现过程 - 此过程是
BERT
源码分析的系列内容之一
1. 代码
先看一下整体的架构:
class BertLayer(nn.Module):def __init__(self, config):super().__init__()self.chunk_size_feed_forward = config.chunk_size_feed_forwardself.seq_len_dim = 1self.attention = BertAttention(config) # 用于计算Attention 的部分self.is_decoder = config.is_decoder # 判断是否是decoderself.add_cross_attention = config.add_cross_attention # TODO?if self.add_cross_attention:assert self.is_decoder, f"{self} should be used as a decoder model if cross attention is added"self.crossattention = BertAttention(config)self.intermediate = BertIntermediate(config) # ?self.output = BertOutput(config) # 对输出的处理def forward(self,hidden_states, # 输入attention_mask=None, # attention maskhead_mask=None, # ?encoder_hidden_states=None, # ?encoder_attention_mask=None, # ?past_key_value=None, # ?output_attentions=False, #是否输出 attention score 的值?):# decoder uni-directional self-attention cached key/values tuple is at positions 1,2self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None # ?# 计算 attention 值self_attention_outputs = self.attention(hidden_states,attention_mask,head_mask,output_attentions=output_attentions,past_key_value=self_attn_past_key_value,)attention_output = self_attention_outputs[0] # 得到了attention 后的输出# 这个在仅用作编码器的时候用不着,暂不分析# if decoder, the last output is tuple of self-attn cacheif self.is_decoder:outputs = self_attention_outputs[1:-1]present_key_value = self_attention_outputs[-1]else:# 其实这里的 self_attention_outputs[1:]是 attention score,所以这里的命名有点儿不合理~ outputs = self_attention_outputs[1:] # add self attentions if we output attention weightscross_attn_present_key_value = Noneif self.is_decoder and encoder_hidden_states is not None:# ... 不是decoder部分,直接忽略 # 对attention_output 做操作,后面详细解释一下这个操作是干嘛的 layer_output = apply_chunking_to_forward(self.feed_forward_chunk, self.chunk_size_feed_forward, self.seq_len_dim, attention_output)# 将最后的结果拼成一个tupleoutputs = (layer_output,) + outputs# if decoder, return the attn key/values as the last outputif self.is_decoder:outputs = outputs + (present_key_value,)# 返回return outputsdef feed_forward_chunk(self, attention_output):intermediate_output = self.intermediate(attention_output)layer_output = self.output(intermediate_output, attention_output)return layer_output
【源码解读】BertLayer相关推荐
- Pytorch Bert源码解读
Bert 源码解读 从 CV 转 NLP 有小半年了,一直在用 Bert 系列做一些自然语言理解任务,包括命名实体识别.关系抽取,文本结构化等信息抽取任务.之前开发任务紧,Bert 拿来就用了,很多代 ...
- Bert系列(二)——源码解读之模型主体
本篇文章主要是解读模型主体代码modeling.py.在阅读这篇文章之前希望读者们对bert的相关理论有一定的了解,尤其是transformer的结构原理,网上的资料很多,本文内容对原理部分就不做过多 ...
- Bert系列(三)——源码解读之Pre-train
https://www.jianshu.com/p/22e462f01d8c pre-train是迁移学习的基础,虽然Google已经发布了各种预训练好的模型,而且因为资源消耗巨大,自己再预训练也不现 ...
- linux下free源码,linux命令free源码解读:Procps free.c
linux命令free源码解读 linux命令free源码解读:Procps free.c 作者:isayme 发布时间:September 26, 2011 分类:Linux 我们讨论的是linux ...
- nodeJS之eventproxy源码解读
1.源码缩影 !(function (name, definition) { var hasDefine = typeof define === 'function', //检查上下文环境是否为AMD ...
- PyTorch 源码解读之即时编译篇
点击上方"AI遇见机器学习",选择"星标"公众号 重磅干货,第一时间送达 作者丨OpenMMLab 来源丨https://zhuanlan.zhihu.com/ ...
- Alamofire源码解读系列(九)之响应封装(Response)
本篇主要带来Alamofire中Response的解读 前言 在每篇文章的前言部分,我都会把我认为的本篇最重要的内容提前讲一下.我更想同大家分享这些顶级框架在设计和编码层次究竟有哪些过人的地方?当然, ...
- Feflow 源码解读
Feflow 源码解读 Feflow(Front-end flow)是腾讯IVWEB团队的前端工程化解决方案,致力于改善多类型项目的开发流程中的规范和非业务相关的问题,可以让开发者将绝大部分精力集中在 ...
- spring-session源码解读 sesion
2019独角兽企业重金招聘Python工程师标准>>> spring-session源码解读 sesion 博客分类: java spring 摘要: session通用策略 Ses ...
- 前端日报-20160527-underscore 源码解读
underscore 源码解读 API文档浏览器 JavaScript 中加号操作符细节 抛弃 jQuery,拥抱原生 JS 从 0 开始学习 GitHub 系列之「加入 GitHub」 js实现克隆 ...
最新文章
- ES6关于Promise的用法详解
- shell脚本编程第一天
- Python基础(1)
- 零次学习(zero-shot learning)
- 基于TCP和多线程实现无线鼠标键盘-Socket(2)
- java 取对象的类_Java中通过Class类获取Class对象的方法详解
- python连接sqlserver_python连接SQL Server数据库
- PAIP批量改名JS实现
- 计算机原理与结构 实验1《实验工具使用》
- android 镜像投屏开发,Android 投屏实现纪要
- win 7 虚拟机VMware Tools安装
- List 接口和常用方法
- 烂土豆(JuicyPotato)提权
- CSS——CSS盒子模型(重点※)
- React Navigation——底部导航栏设计
- 数据库关系代数操作与扩展操作
- 【Ubuntu16.04】ROS · Kinetic安装 · 图文教程与问题详解
- llinux c 常用指令学习
- linux 大牛博客,大牛
- gre模考软件java.exe_5款GRE模考软件神器大汇总