4. Transformer_4_输出部分

1. 输出部分介绍

2️⃣ 输出部分:输出部分包含:linear线性层、softmax层;经过 Linear层:对于分类任务,如 18分类的人名分类器,则Linear最后输出层即为18,再进行 softmax;

2. 线性层的作用

通过对上一步的线性变化得到指定维度的输出,也就是转换维度的作用;

3. softmax层的作用

使最后一维的向量中的数字缩放到0-1的概率值域内,并满足他们的和为1;

4. 线性层和softmax层的代码分析

init中:参数:d_model, vocab_size;d_model解码器输出的词嵌入维度512、vocab_size解码器单词总个数;nn.Linear(d_model, vocab_size)一个linear层,输出vocab_size;

forward:参数:x;x即为解码器的输出结果,经过linear得到预测值,在经过log_softmax返回最终输出x;

python 复制代码
from dm3_decoder import *

class Generator(nn.Module):
    def __init__(self, d_model, vocab_size):
        super(Generator, self).__init__()
        # d_model:词嵌入维度大小,解码器输出的词嵌入维度大小;vocab_size:解码器端词表大小
        self.linear = nn.Linear(d_model, vocab_size)

    def forward(self, x):
        output = F.log_softmax(self.linear(x), dim=-1)
        return output

def test_generator():
    decoder_output = test_decoder()
    # 实例化输出层对象
    my_generator = Generator(d_model=512, vocab_size=2000)
    result = my_generator(decoder_output)
    print(f'输出结果:{result.shape}')  # [2, 6, 2000]


if __name__ == '__main__':
    test_generator()
相关推荐
Meinianda1 小时前
我用Agent 使用瑞幸官方MCP下了一单:过程全记录,优缺点分析
人工智能
没事别瞎琢磨1 小时前
七、敏感路径预检——Protected Paths
人工智能·node.js
用户600071819101 小时前
【翻译】构建 Claude Code 的经验:我们如何使用 Skills
人工智能
没事别瞎琢磨1 小时前
五、进程执行——spawn、超时与进程树清理
人工智能·node.js
没事别瞎琢磨1 小时前
四、命令风险分级与审批策略
人工智能·node.js
阿乔外贸日记1 小时前
埃塞俄比亚出口全流程注意事项
大数据·人工智能·智能手机·云计算·汽车
程序员cxuan1 小时前
Agents.md 是什么
人工智能·后端·程序员
人工小情绪1 小时前
Windows 安装 Codex 桌面版,并用 CC Switch 管理配置
人工智能·windows·codex·cc switch
godspeed_lucip2 小时前
LLM和Agent——专题6:Multi Agent 入门(5)
人工智能·python