08_Hung-yi Lee_Transformer
Carpe Tu Black Whistle

transformer

一个 Sequence to Sequence 的model

Seq2seq

Input a sequence, output a sequence
The output length is determined by model

image

在一些翻译问题中,我们一般直接翻译(不会有语音转文字转另一种语言的中间过程

image

  1. Seq2seq 的架构,由 Encoder 和 Decoder组成
  2. 最早的 Seq2seq 架构在2014年被发表在 arxiv 上,Seq2seq learning with Neural Networks
  3. 发扬光大的paper(右) Attention is all you need(Transformer)

语言翻译

硬train一发: 最粗暴的训练

  • 做了一个,直接将 语音信号,转换为 繁体中文字母 的 Seq2seq模型
  • 利用youtube上的 乡土剧 (闽南语语言,繁体中文字幕)的资料为训练数据
  • Using 1500 hours of data for training

image

  • Background music & noises? —— Don’t care
  • Noisy transcriptions —— Don’t care
  • Phonemes of Hokkien? —— No…

直接台语声音信号转中文,不是不可能实现,但是一些 倒装 等语言习惯难以学会。

Text-to-Speech Synthesis

文本转合成语音,Text-to-Speech 文本转语音。

image

Seq2seq for Chatbot

image

Notes

Seq2seq NLP应用

Seq2seq model 在 NLP 的领域的应用非常的多。
大多数 NLP 的问题,都可以被视为 Question Answering(QA) 的问题

image

https://arxiv.org/abs/1806.08730
https://arxiv.org/abs/1909.03329

客制化

对于NLP任务而言,一般客制化设计问题的解决方案,可以获得更好的结果。
但是 Seq2seq model 一般被认为是一种通用模型的解决方案。

一些问题转换

文法剖析:

image

一些并非是 Sequence 结构的东西,也可以用一些技巧转换,然后用 Seq2seq model去完成。

一篇14年的paper Grammar as a Foreign Language

Multi-label Classification

Multi-class: 单纯的多分类问题
Multi-label: An object can belong to multiple classes.

image
https://arxiv.org/abs/1909.03434
https://arxiv.org/abs/1707.05495

Seq2seq for Object Detection

image

Encoder

encoder: 输入一同样长的, Self-attention, rnn, cnn 都可以实现同样的

transformer的encoder就是 Self-attention 的结构

image

  1. Residual: Resnet中提出的结构设计,输入与前馈给系统的输出
  2. norm: 这里的 norm,为 Layer Norm ,一个样本的所有dimension进行标准化
  3. 全联接层被写为 FC standfor: fully-connected

image

  • 输入后会加入 Positional Encoding
  • Add & Norm = Residual + Layer norm
  • Feed Forward 是 FC 层
  • encoder 就是 若干个这样的block叠加的结构

Bert 使用了与 transformer encoder 一样的结构

To learn more

Decoder

AutoRegressive(AT)

image

  1. Decoder 会先把 Encoder 的输出读入。以处理语音辨识为例。
  2. 输入一个 special token (Begin Of Sentence, BOS)
  3. 完成以上两步后,在 softmax 之后,会输出一个 vector
  4. vector 的长度很长,跟 vocabulary size 是一样的(加上 END
  5. 输出最大字后,作为输入再次进入Decoder获得下一个字节
    image

image

  • 除开中间的 Multi-Head Attention(从外面输入) 与 Add & Norm, 结构与 Encoder 相近

  • Masked Self-attention 比起原版本,被车轮地依次纳入计算,就像Decoder的循环运行

image

  • Decoder 的论文中,图上,特别强调了是一个 Masked Self-attention, decoder 的 token 是一个个产生的(非并行)。

  • Decoder 自己决定,输出 Sequence 的长度,在某次输出为END后,Decoder的输出就结束了。这样Decoder就自己决定了Sequence的长度。

image

Non-autoregressive(NAT)

image

  • How to decide the ouput length for NAT decoder?
    • Another predictor for output length
    • Output a very long sequence, ignore tokens after END
  • Advantage: parallel, more stable generation(e.g., TTS)
  • NAT is usually worse than AT(why? Multi-modality)
    现在如果要让 NAT 的 Decoder 达到 AT 但表现效果,需要非常多的trick让这件事情办到。

在 Self-attention 出现以后,NAT decoder 已经成为了一个热门的研究领域(大坑)

在语音合成的应用上,NAT的架构,甚至能很方便的 控制输出的长度,讲话变快、变慢

To learn more

https://youtu.be/jvyKmU4OM3c

Encoder-Decoder

image

  • encoder 的输出会被在中间的 Multi-Head Attention 模块输入,计算出 Cross attention

image

经过 Self-attention(Mask) 生成的 query 与 encoder 生成做相关性计算,生成attention score.

最后 weighted sum 最后生成 v 进入 fully-connected network.

Cross Attention

Listen, attend and spell: A neural network for large vocabulary conversational speech recognition

image
跟 state of art 的结果只差一点点。

那个时候的paper,只要是 Seq2seq 的model,paper名就要有3个动词

上面图表:

  1. 上面的是声音信号,是机器的输入,横轴是时间,纵轴是一排排的vector
  2. 左边那排是,encoder的输出(当作特殊的词汇来处理了)
  3. 灰度方块(自己造的)反应了不同时间节点的 attention score

image
https://arxiv.org/abs/2005.08081

decoder 接受的输入不一定要是 encoder最后一层的输出,有各式各样的组合

Train

image

  • 训练时的 loss-functioncross entropy
  • 训练中,每次出现一个输出,解决一次分类

image

  • 在训练的输入,依次输入为 Ground Truth, 这个机制叫作 Teacher Forcing
  • 最后的字段,也是被要求在内

Tip

Copy Mechanism

对于很多任务而言,不需要 encoder 产生输出

Chat-bot

User: 你好,我是库洛洛
Machine:库洛洛你好,很高兴认识你

User: 小杰不能使用念能力了!
Machine: 你所谓的“不使用念能力”是什么意思?

Summarization

image
https://arxiv.org/abs/1704.04368

要让机器能说合理的句子,至少要 百万篇文章

对于摘要而言,Copy Mechanism 尤为重要

Pointer Network: video from Youtube
Incorporating Copying Mechanism in Seq2seq Learning: paper

Guided Attention

  • Montonic Attention
  • Location-aware attention

In some tasks, input and output are monotonically aligned.
For example, speech recognition, TTS, etc.

image

The red path is Greedy Decoding.
The green path is the best one.
Not possible to chech all the paths…Beam Search

image

不一定有用

paper: The Curious Case of Neural Text Degeneration

image

这篇paper处理的任务叫作 content completion, Beam search 加入后的训练结果会是,不断重复。

Randomness is needed for decoder when generating sequence in some tasks.

Beam Search 是否有用,基于要处理的 task

  1. 任务结果非常明确,有唯一答案,通常比较有用
  2. creative task, 很重要。语音合成TTS 要加入 Noise

Optimizing Evaluation Metrics

image
BLEU score: 依次比对,输出与 ground trues
最大化 BLUE score 不等效于 最小化 Cross-entropy

  • 训练的时候 minimize Cross entropy
  • Validation: Maximize BLEU score
  • BLUE score 无法微分,所以不方便作为train时,用的loss func
  • 但是 test score 要用 BLUE score, 所以validation的时候,可以取BLUE score

training with BLUE score

只用当作 强化学习 问题,直接训练。
https://arxiv.org/abs/1511.06/32

Scheduled Sampling

mismatch: exposure bias
如果test的时候,一环的output错误,之后的输出,倾向于全部输出错误。

可以考虑的技巧叫作 Scheduled Sampling

image

image