当前位置: 首页 > news >正文

transformer-实现单层encoder_layer

Encoder Layer

  • 论文地址

    https://arxiv.org/pdf/1706.03762

Encoder层介绍

  • Encoder层是Transformer编码器的核心组件,由多头自注意力和前馈神经网络两个子层构成。每个子层都包含残差连接(Residual Connection)和层归一化(Layer Normalization),有效缓解梯度消失问题并加速训练。

    image-20250428170531073

    今天这里实现的是上图中蓝色框中的单层EncoderLayer,不包含 embedding和位置编码

    主要处理流程:

    1. 多头自注意力机制:建立序列内部元素之间的依赖关系
    2. 前馈神经网络:进行非线性特征变换
    3. 残差连接 + 层归一化:在两个子层后各执行一次
    4. Dropout:最终输出前进行随机失活

数学表达

  • 子层处理流程公式化表示:
    LayerOutput = LayerNorm ( x + Sublayer ( x ) ) \text{LayerOutput} = \text{LayerNorm}(x + \text{Sublayer}(x)) LayerOutput=LayerNorm(x+Sublayer(x))

    对于Encoder层具体展开:
    z = LayerNorm ( x + MultiHeadAttention ( x ) ) o u t = LayerNorm ( z + FeedForward ( z ) ) \begin{aligned} z &= \text{LayerNorm}(x + \text{MultiHeadAttention}(x)) \\ out &= \text{LayerNorm}(z + \text{FeedForward}(z)) \end{aligned} zout=LayerNorm(x+MultiHeadAttention(x))=LayerNorm(z+FeedForward(z))


代码实现

  • 其他层的实现

    层名链接
    PositionEncodinghttps://blog.csdn.net/hbkybkzw/article/details/147431820
    calculate_attentionhttps://blog.csdn.net/hbkybkzw/article/details/147462845
    MultiHeadAttentionhttps://blog.csdn.net/hbkybkzw/article/details/147490387
    FeedForwardhttps://blog.csdn.net/hbkybkzw/article/details/147515883
    LayerNormhttps://blog.csdn.net/hbkybkzw/article/details/147516529

    下面统一在before.py中导入

  • 实现单层的encoder层

    import torch 
    from torch import nnfrom before import PositionEncoding,calculate_attention,MultiHeadAttention,FeedForward,LayerNormclass EncoderLayer(nn.Module):"""Transformer编码器层"""def __init__(self, n_heads, d_model, d_ff, dropout_prob=0.1):super(EncoderLayer, self).__init__()self.self_multi_head_att = MultiHeadAttention(n_heads, d_model, dropout_prob)self.ffn = FeedForward(d_model, d_ff, dropout_prob)self.norm1 = LayerNorm(d_model)self.norm2 = LayerNorm(d_model)self.dropout = nn.Dropout(dropout_prob)def forward(self, x, mask=None):# 自注意力子层_x = x  # 原始输入保存用于残差连接att_out = self.self_multi_head_att(x, x, x, mask)att_out += _x  # 残差连接att_out = self.norm1(att_out)  # 层归一化# 前馈子层_x = att_outffn_out = self.ffn(att_out) ffn_out += _x  # 残差连接ffn_out = self.norm2(ffn_out)  # 层归一化return self.dropout(ffn_out)  # 最终输出
    
  • 维度变化

    处理阶段张量形状示例
    输入数据[batch_size, seq_len, d_model]
    自注意力输出(不改变维度)[batch_size, seq_len, d_model]
    残差连接+层归一化[batch_size, seq_len, d_model]
    前馈网络输出(不改变维度)[batch_size, seq_len, d_model]
    最终输出[batch_size, seq_len, d_model]

使用示例

  • 测试用例

    if __name__ == "__main__":# 模拟输入:batch_size=4, 序列长度100, 维度512x = torch.randn(4, 100, 512)# 实例化编码器层:8头注意力,512维,前馈层2048维,10% dropoutencoder_layer = EncoderLayer(n_heads=8, d_model=512, d_ff=2048, dropout_prob=0.1)# 前向传播out = encoder_layer(x, mask=None)print("Input shape:", x.shape)    # torch.Size([4, 100, 512])print("Output shape:", out.shape) # torch.Size([4, 100, 512])
    

相关文章:

  • VINS-FUSION:跑通手机录制数据
  • C语言----操作符详解(万字详解)
  • 4月28日日记
  • 待验证---Oracle 19c 在 CentOS 7 上的快速安装部署指南
  • C#与SVN的深度集成:实现版本控制自动化管理​
  • MATLAB实现神经网络的OCR识别
  • Web 基础与 HTTP 协议
  • STM32的SysTick
  • 学成在线。。。
  • 【爬虫】码上爬第2题:headersi请求头验证
  • 排序算法详解笔记
  • 详解UnityWebRequest类
  • Day15(贪心算法)——LeetCode121.买卖股票的最佳时机55.跳跃游戏
  • 文献阅读(一)植物应对干旱的生理学反应 | The physiology of plant responses to drought
  • 机器学习基础理论 - 目标函数,损失函数,代价函数
  • C++入门(namespace/输入输出)
  • docker存储
  • 23.C语言指针相关知识点2
  • C++代码随想录刷题知识分享-----面试题链表相交
  • 传统农耕展陈如何突破?数字多媒体能否重构文化体验边界?
  • 俄罗斯延长非法滞留外国人限期离境时间至9月
  • 夜读丨怀念那个写信的年代
  • 日月谭天丨赖清德强推“脱中入北”是把台湾带向死路
  • 原创话剧风向标!这个展演上《大宅门》《白鹿原》先后上演
  • 兵韬志略|美菲“肩并肩”军演超越传统范畴,凸显防务合作重大转型
  • 又一名90后干部被查,已有多人倒在乡镇领导岗位上