大访问量的网站怎么做优化,做网站资料,wordpress注册审批,站长查询域名大模型基础——从零实现一个Transformer(1)-CSDN博客 一、前言
之前两篇文章已经讲了Transformer的Embedding,Tokenizer,Attention,Position Encoding, 本文我们继续了解Transformer中剩下的其他组件.
二、归一化
2.1 Layer Normalization layerNorm是针对序列数据提出的一种…
大模型基础——从零实现一个Transformer(1)-CSDN博客 一、前言
之前两篇文章已经讲了Transformer的Embedding,Tokenizer,Attention,Position Encoding, 本文我们继续了解Transformer中剩下的其他组件.
二、归一化
2.1 Layer Normalization layerNorm是针对序列数据提出的一种归一化方法主要在layer维度进行归一化即对整个序列进行归一化。
layerNorm会计算一个layer的所有activation的均值和方差利用均值和方差进行归一化。
∑1
1∑1(−)2
归一化后的激活值如下
−
其中 和 是可训练的模型参数。 是缩放参数新分布的方差 2 是平移系数新分布的均值为 。 为一个小数添加到方差上避免分母为0。
2.2 LayerNormalization 代码实现
import torch
import torch.nn as nnclass LayerNorm(nn.Module):def __init__(self,num_features,eps1e-6):super().__init__()self.gamma nn.Parameter(torch.ones(num_features))self.beta nn.Parameter(torch.zeros(num_features))self.eps epsdef forward(self,x):Args:x (Tensor): (batch_size, seq_length, d_model)Returns:Tensor: (batch_size, seq_length, d_model)mean x.mean(dim-1,keepdimTrue)std x.std(dim-1,keepdimTrue,unbiasedFalse)normalized_x (x - mean) / (std self.eps)return self.gamma * normalized_x self.betaif __name__ __main__:batch_size 2seqlen 3hidden_dim 4# 初始化一个随机tensorx torch.randn(batch_size,seqlen,hidden_dim)print(x)# 初始化LayerNormlayer_norm LayerNorm(num_featureshidden_dim)output_tensor layer_norm(x)print(output after layer norm:\n,,output_tensor)torch_layer_norm torch.nn.LayerNorm(normalized_shapehidden_dim)torch_output_tensor torch_layer_norm(x)print(output after torch layer norm:\n,torch_output_tensor)
三、残差连接
残差连接(residual connection,skip residual也称为残差块)其实很简单 x为网络层的输入该网络层包含非线性激活函数记为F(x)用公式描述的话就是 代码简单实现
x x layer(x)
四、前馈神经网络
4.1 Position-wise Feed Forward
Position-wise Feed Forward(FFN)逐位置的前馈网络其实就是一个全连接前馈网络。目的是为了增加非线性增强模型的表示能力。
它一个简单的两层全连接神经网络不是将整个嵌入序列处理成单个向量而是独立地处理每个位置的嵌入。所以称为position-wise前馈网络层。也可以看为核大小为1的一维卷积。
目的是把输入投影到特定的空间再投影回输入维度。
FFN具体的公式如下
()(11)22
上述公式对应FFN中的向量变换操作其中f为非线性激活函数。
4.2 FFN代码实现
from torch import nn,Tensor
from torch.nn import functional as Fclass PositonWiseFeedForward(nn.Module):def __init__(self,d_model:int ,d_ff: int ,dropout: float0.1) - None::param d_model: dimension of embeddings:param d_ff: dimension of feed-forward network:param dropout: dropout ratiosuper().__init__()self.ff1 nn.Linear(d_model,d_ff)self.ff2 nn.Linear(d_ff,d_model)self.dropout nn.Dropout(dropout)def forward(self,x: Tensor) - Tensor::param x: (batch_size, seq_length, d_model) output from attention:return: (batch_size, seq_length, d_model)return self.ff2(self.dropout(F.relu(self.ff1(x))))
五、Transformer Encoder Block 如图所示编码器(Encoder)由N个编码器块(Encoder Block)堆叠而成我们依次实现。
from torch import nn,Tensor
## 之前实现的函数引入
from llm_base.attention.MultiHeadAttention1 import MultiHeadAttention
from llm_base.layer_norm.normal_layernorm import LayerNorm
from llm_base.ffn.PositionWiseFeedForward import PositonWiseFeedForwardfrom typing import *class EncoderBlock(nn.Module):def __init__(self,d_model: int,n_heads: int,d_ff: int,dropout: float,norm_first: bool False)::param d_model: dimension of embeddings:param n_heads: number of heads:param d_ff: dimension of inner feed-forward network:param dropout:dropout ratio:param norm_first : if True, layer norm is done prior to attention and feedforward operations(Pre-Norm).Otherwise its done after(Post-Norm). Default to False.super().__init__()self.norm_first norm_firstself.attention MultiHeadAttention(d_model,n_heads,dropout)self.norm1 LayerNorm(d_model)self.ff PositonWiseFeedForward(d_model,d_ff,dropout)self.norm2 LayerNorm(d_model)self.dropout1 nn.Dropout(dropout)self.dropout2 nn.Dropout(dropout)# self attention sub layerdef _self_attention_sub_layer(self,x: Tensor, attn_mask: Tensor, keep_attentions: bool) - Tensor:x self.attention(x,x,x,attn_mask,keep_attentions)return self.dropout1(x)# ffn sub layerdef _ffn_sub_layer(self,x: Tensor) - Tensor:x self.ff(x)return self.dropout2(x)def forward(self,src: Tensor,src_mask: Tensor None,keep_attentions: bool False) - Tuple[Tensor,Tensor]::param src: (batch_size, seq_length, d_model):param src_mask: (batch_size, 1, seq_length):param keep_attentions:whether keep attention weigths or not. Defaults to False.:return:(batch_size, seq_length, d_model) output of encoder block# pass througth multi-head attention# src (batch_size, seq_length, d_model)# attn_score (batch_size, n_heads, seq_length, k_length)x src# post LN or pre LNif self.norm_first:# pre LNx x self._self_attention_sub_layer(self.norm1(x),src_mask,keep_attentions)x x self._ffn_sub_layer(self.norm2(x))else:x self.norm1(x self._self_attention_sub_layer(x,src_mask,keep_attentions))x self.norm2(x self._ffn_sub_layer(x))return x
5.1 Post Norm Vs Pre Norm
公式区别
Pre Norm 和 Post Norm 的式子分别如下 在大模型的区别
Post-LN :是在 Transformer 的原始版本中使用的归一化方案。在此方案中每个子层例如自注意力机制或前馈网络的输出先通过子层自身的操作然后再通过层归一化Layer Normalization
Pre-LN:是先对输入进行层归一化然后再传递到子层操作中。这样的顺序对于训练更深的网络可能更稳定因为归一化的输入可以帮助缓解训练过程中的梯度消失和梯度爆炸问题。
5.2为什么Pre效果弱于Post 文章转载自: http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.zshuhd015.cn.gov.cn.zshuhd015.cn http://www.morning.fpxsd.cn.gov.cn.fpxsd.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.wqgr.cn.gov.cn.wqgr.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.glnxd.cn.gov.cn.glnxd.cn http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.tgwfn.cn.gov.cn.tgwfn.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.wgqtt.cn.gov.cn.wgqtt.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.mrnnb.cn.gov.cn.mrnnb.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.rqckh.cn.gov.cn.rqckh.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.mbprq.cn.gov.cn.mbprq.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.qncqd.cn.gov.cn.qncqd.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.ntkpc.cn.gov.cn.ntkpc.cn http://www.morning.ityi666.cn.gov.cn.ityi666.cn http://www.morning.sgrwd.cn.gov.cn.sgrwd.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.gtqx.cn.gov.cn.gtqx.cn http://www.morning.lmjtp.cn.gov.cn.lmjtp.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.hctgn.cn.gov.cn.hctgn.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.wpsfc.cn.gov.cn.wpsfc.cn http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.mqnbm.cn.gov.cn.mqnbm.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.wlqll.cn.gov.cn.wlqll.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.tbnpn.cn.gov.cn.tbnpn.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.dmlgq.cn.gov.cn.dmlgq.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn