Transformer(李宏毅课程)

Transformer

Seq2seq

  1. The output length is determined by model
  1. Speech Recognition
  2. Machine Translation
  3. Speech Translation(language without text)
  1. Question Answering
  1. NLP problem can be solved by seq2seq
  2. Syntactic Parsing
    在这里插入图片描述
    output is a tree which can be regarded as seq2seq problems
  1. Multi-label Classification

An object can belong to multiple classes
在这里插入图片描述

  1. Encoder
    Encoder的输入是一排向量,输出也是相同长度的另一排向量。输入首先经过positional encoding加入位置信息后,再通过self-attention加入注意力机制,得到self-attention层的输出后经过residual的操作(将输入和输出相加)得到向量经过normlization得到下一层Fully connet的输入,经过最后全连接层后再一次add&norm后得到最终encoder的输出向量。

1 the block in the net:
(1) residual: add the input to the handled data
(2) norm: x i ′ = x i − m σ x'_i=\frac{x_i-m}{\sigma} xi=σxim
在这里插入图片描述
the final block architecture:
在这里插入图片描述

  1. Decoder
    Decoder有两种,第一种是autoregressive,另一种是nonautogressive
  1. Autoregressive
    在这里插入图片描述
    例如是处理一个语音辨识的问题:
    encoder的输入就是语言的波形,输出对应的一些向量;
    decoder的输入则是encoder的输出和one-hot表示的向量,首先会有一个one-hot形式的begin符号作为decoder的输入,放入decoder后得到一个输出经过softmax后得到一个向量(机),然后(机)作为下一个decoder的输入继续进行。
    在这里插入图片描述
    其中decoder和encoder的不同就在于一开始的输入经过了一层masked self-attention,ta和原先的self-attention不同的地方在于,每个向量都只能考虑之前的所有向量,而非前后所有的向量,因为我们考虑前面语音辨识所说的decoder输入是一个向量一个向量输入,所以每个向量暂时看不到后面的输入,因此无法考虑之后的向量。
    Masked Self-attention
    From the graph above, we have the input one by one, so we can not input all a i a^i ai together
    在这里插入图片描述
    另外一个问题是,seq2seq中输入和输出长度不一样,因此我们要怎么决定输出什么时候停止。实际上就是准备一个特别的终止符号(end),若输入达到这样的end时,就停止输出。
  1. nonAutoregressive
    对于NAT,实际上就是将输入变成一整列的begin,而非顺序的序列。
    在这里插入图片描述
  1. We do not know the output length:
    (1) another predictor for output length
    (2) output a long sequence include END, ignore the tokens after END
  2. Advantages:
    (1) parallel
    (2) controllable output length
  3. NAT is usually worse than AT

3.Encoder-Decoder
接下来就是encoder和decoder之间的连接问题,
input all encoder inputs and one decoder input to the cross attention
在这里插入图片描述

1.cross attention
这里面就是cross attention的操作,我们看到上面的图中可以看出,中间层的self-attention的输入是由encoder的输入+decoder的输入组成。例如下图中,我们首先decoder输入BEGIN,transform后得到q,然后对于 α ‘ 1 \alpha`_1 α1 α ‘ 2 \alpha`_2 α2 α ‘ 3 \alpha`_3 α3 q q q我们进行一个cross attention的处理得到新的输出v,v就是我们最终放入FC的输入。然后下一个(机)再作为输入进入decoder中,依次和左边的encoder输出进行cross attention处理,最终得到decoder的输出。
在这里插入图片描述
在这里插入图片描述

  1. Training
  1. Teacher Forcing
    在我们decoder的训练过程中,实际上我们decoder的输入就是一个正确答案,输出也是一个正确答案,这种方式就叫做teacher forcing。
    然后在testing的过程中,我们decoder的第一个输入是begin符号。
  1. tips:
    (1) char-bot: copy some words in the question
    在这里插入图片描述
    (2) guided attention
    ex: speech recognition
    we should focus on the first word in the beginning
    solution: monotonic attention, location-aware attention
    (3) Beam Search
    greedy decoding: select the max every path
    solution: we select the bream search在这里插入图片描述
    (4) optimizing evaluation metrics
    In the training process, we use cross entropy. But finally we use BLEU score to evaluate the result.
    BLEU score: compare the output with correct answer, but it can not be differentiate.
    solution: If we would like to use BLEU score, use RL method.
    (5) Exposure bias
    For decoder, if every training data is true, once test data is wrong ,the output must be wrong
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Transformer发轫于NLP(自然语言处理),并跨界应用到CV(计算机视觉)领域。目前已成为深度学习的新范式,影响力和应用前景巨大。  本课程Transformer的原理和PyTorch代码进行精讲,来帮助大家掌握其详细原理和具体实现。  原理精讲部分包括:注意力机制和自注意力机制、Transformer的架构概述、Encoder的多头注意力(Multi-Head Attention)、Encoder的位置编码(Positional Encoding)、残差链接、层规范化(Layer Normalization)、FFN(Feed Forward Network)、Transformer的训练及性能、Transformer的机器翻译工作流程。   代码精讲部分使用Jupyter Notebook对Transformer的PyTorch代码进行逐行解读,包括:安装PyTorch、TransformerEncoder代码解读、TransformerDecoder代码解读、Transformer的超参设置代码解读、Transformer的训练示例(人为随机数据)代码解读、Transformer的训练示例(德语-英语机器翻译)代码解读。相关课程: 《Transformer原理与代码精讲(PyTorch)》https://edu.csdn.net/course/detail/36697《Transformer原理与代码精讲(TensorFlow)》https://edu.csdn.net/course/detail/36699《ViT(Vision Transformer)原理与代码精讲》https://edu.csdn.net/course/detail/36719《DETR原理与代码精讲》https://edu.csdn.net/course/detail/36768《Swin Transformer实战目标检测:训练自己的数据集》https://edu.csdn.net/course/detail/36585《Swin Transformer实战实例分割:训练自己的数据集》https://edu.csdn.net/course/detail/36586《Swin Transformer原理与代码精讲》 https://download.csdn.net/course/detail/37045
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值