bert,transformer架构图及面试题

Transformer详解 - mathor

atten之后经过一个全连接层+残差+层归一化

python 复制代码
`class BertSelfOutput(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.dense = nn.Linear(config.hidden_size, config.hidden_size)
        self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)

    def forward(self, hidden_states: torch.Tensor, input_tensor: torch.Tensor) -> torch.Tensor:
        hidden_states = self.dense(hidden_states)  # 全连接 768->768
        hidden_states = self.dropout(hidden_states)
        hidden_states = self.LayerNorm(hidden_states + input_tensor) # 残差和层归一化
        return hidden_states`

残差的作用:避免梯度消失

归一化的作用:避免梯度消失和爆炸,加速收敛

然后再送入一个两层的前馈神经网络

python 复制代码
`class BertIntermediate(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.dense = nn.Linear(config.hidden_size, config.intermediate_size)
        if isinstance(config.hidden_act, str):
            self.intermediate_act_fn = ACT2FN[config.hidden_act]
        else:
            self.intermediate_act_fn = config.hidden_act

    def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
        hidden_states = self.dense(hidden_states)  # [1, 16, 3072] 映射到高维空间:768 -> 3072
        hidden_states = self.intermediate_act_fn(hidden_states)
        return hidden_states`
python 复制代码
`class BertOutput(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.dense = nn.Linear(config.intermediate_size, config.hidden_size)
        self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)

    def forward(self, hidden_states: torch.Tensor, input_tensor: torch.Tensor) -> torch.Tensor:
        hidden_states = self.dense(hidden_states)  # 3072 -> 768
        hidden_states = self.dropout(hidden_states)
        hidden_states = self.LayerNorm(hidden_states + input_tensor)  # 残差和层归一化
        return hidden_states`

面试题:为什么注意力机制中要除以根号dk

答:因为q和k做点积后值会很大,会导致反向传播时softmax函数的梯度很小。除以根号dk是为了保持点积后的值均值为0,方差为1.(q和k都是向量)

证明:已知q和k相互独立,且是均值为0,方差为1。

则D(qi*ki)=D(qi)*D(ki)=1

除以dk则D((qi*ki)/根号dk)=1/dk,每一项是这个值,但是根据上面红框的公式,一共有dk项求和,值为1

所以(q*k)/dk的方差就等1

(背景知识)方差性质:

D(CX)=C^2D(X) ,其中C是常量

相关推荐
淬炼之火4 分钟前
基于pycharm和anaconda的yolo简单部署测试
python·深度学习·yolo·pycharm·ultralytics
搞科研的小刘选手1 小时前
【早稻田大学主办】2026年第三届人工智能与未来教育国际学术会议(AIFE 2026)
人工智能·机器学习·数据挖掘·机器人·未来教育·远程教育·移动学习
数据与人工智能律师1 小时前
解码Web3:DeFi、GameFi、SocialFi的法律风险警示与合规路径
大数据·网络·人工智能·云计算·区块链
Best_Me071 小时前
理解AUROC,AP,F1-scroe,PRO
人工智能·机器学习
IT_陈寒1 小时前
React 性能优化:5个实战技巧让首屏加载提升50%,开发者亲测有效!
前端·人工智能·后端
久未1 小时前
Pytorch autoload机制自动加载树外扩展(Autoload Device Extension)
人工智能·pytorch·python
Apifox.1 小时前
如何在 Apifox 中通过 AI 一键生成几十个测试用例?
人工智能·程序人生·ai·测试用例·ai编程
java1234_小锋1 小时前
TensorFlow2 Python深度学习 - TensorFlow2框架入门 - 使用Keras.Model来定义模型
python·深度学习·tensorflow·tensorflow2
Learn Beyond Limits2 小时前
TensorFlow Implementation of Content-Based Filtering|基于内容过滤的TensorFlow实现
人工智能·python·深度学习·机器学习·ai·tensorflow·吴恩达
java1234_小锋2 小时前
TensorFlow2 Python深度学习 - 函数式API(Functional API)
python·深度学习·tensorflow·tensorflow2