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()