【機器學習2021】Transformer (上)

Hung-yi Lee
26 Mar 202132:48

Summary

TLDRThis video script delves into the intricacies of Transformer models, particularly their application in various natural language processing and speech-related tasks. It covers the architecture of the Transformer encoder, explaining the role of self-attention, residual connections, and normalization layers. The versatility of sequence-to-sequence models is highlighted, showcasing their use in tasks like machine translation, speech recognition, grammar parsing, and multi-label classification. The script also touches on the potential of Transformer models in tackling unconventional problems and explores alternative design choices for optimizing their performance.

Takeaways

  • 😃 The Transformer model is a powerful Sequence-to-Sequence (Seq2Seq) model used for various tasks like machine translation, speech recognition, and natural language processing (NLP).
  • 🤖 The Transformer consists of an Encoder and a Decoder, where the Encoder processes the input sequence and the Decoder generates the output sequence.
  • 🧠 The Encoder in the Transformer uses self-attention mechanisms to capture dependencies between words in the input sequence.
  • 🔄 The Encoder has a residual connection that adds the input to the output of each block, followed by layer normalization.
  • 💡 The original Transformer architecture is not necessarily the optimal design, and researchers continue to explore modifications like changing the order of operations or using different normalization techniques.
  • 🌐 Seq2Seq models can be applied to various NLP tasks like question answering, sentiment analysis, and grammar parsing by representing the input and output as sequences.
  • 🚀 End-to-end speech translation, where the model translates speech directly into text without intermediate transcription, is possible with Seq2Seq models.
  • 🎤 Seq2Seq models can be used for tasks like text-to-speech synthesis, where the input is text, and the output is an audio waveform.
  • 🔢 Multi-label classification problems, where an instance can belong to multiple classes, can be addressed using Seq2Seq models by allowing the model to output a variable-length sequence of class labels.
  • 🌉 Object detection, a computer vision task, can also be tackled using Seq2Seq models by representing the output as a sequence of bounding boxes and class labels.

Q & A

  • What is a Transformer?

    -A Transformer is a powerful sequence-to-sequence model widely used in natural language processing (NLP) and speech processing tasks. It uses self-attention mechanisms to capture long-range dependencies in input sequences.

  • What are some applications of sequence-to-sequence models discussed in the script?

    -The script discusses several applications, including speech recognition, machine translation, chatbots, question answering, grammar parsing, multi-label classification, and object detection.

  • How does a Transformer Encoder work?

    -The Transformer Encoder consists of multiple blocks, each containing a self-attention layer, a feed-forward network, and residual connections with layer normalization. The blocks process the input sequence and output a new sequence representation.

  • What is the purpose of residual connections in the Transformer architecture?

    -Residual connections are used to add the input of a block to its output, allowing the model to learn residual mappings and potentially improve gradient flow during training.

  • Why is layer normalization used in the Transformer instead of batch normalization?

    -Layer normalization is used instead of batch normalization because it operates on individual samples rather than mini-batches, which is more suitable for sequential data like text or speech.

  • What is the role of positional encoding in the Transformer?

    -Positional encoding is used to inject information about the position of each token in the input sequence, as self-attention alone cannot capture the order of the sequence.

  • Can the Transformer architecture be improved or modified?

    -Yes, the script mentions research papers that explore alternative designs for the Transformer, such as rearranging the layer normalization position or introducing a new normalization technique called Power Normalization.

  • What is the purpose of multi-head attention in the Transformer?

    -Multi-head attention allows the model to attend to different representations of the input sequence in parallel, capturing different types of relationships and dependencies.

  • Can sequence-to-sequence models be applied to tasks beyond translation?

    -Yes, the script mentions that sequence-to-sequence models can be applied to various NLP tasks, such as question answering, grammar parsing, multi-label classification, and even object detection in computer vision.

  • Why are sequence-to-sequence models useful for languages without written forms?

    -Sequence-to-sequence models can be used for speech-to-text translation, allowing languages without written forms to be translated directly into written languages that can be read and understood.

Outlines

00:00

🤖 Introduction to Transformer and Seq2Seq Models

This paragraph introduces the topic of Transformer and its relation to BERT. It explains the concept of Sequence-to-Sequence (Seq2Seq) models, which can be used for various tasks such as speech recognition, machine translation, and speech translation. The paragraph provides examples of how Seq2Seq models can be applied to tasks like translating Taiwanese to Chinese and Taiwanese speech synthesis.

05:01

🗣️ Applications of Seq2Seq Models in Language Processing

This paragraph discusses the potential applications of Seq2Seq models in language processing tasks, such as training a chatbot, question answering, translation, summarization, and sentiment analysis. It explains how various NLP tasks can be framed as question-answering problems and solved using Seq2Seq models. The paragraph also mentions the limitations of using a single Seq2Seq model for all tasks and the need for task-specific models.

10:03

🌐 Versatility of Seq2Seq Models across NLP Tasks

This paragraph further explores the versatility of Seq2Seq models in solving various NLP tasks, including grammar parsing, multi-label classification, and object detection. It explains how even tasks that may not seem like Seq2Seq problems can be formulated as such and solved using these models. The paragraph also mentions the potential of using Seq2Seq models for end-to-end language tasks without intermediate steps.

15:05

🧩 Seq2Seq Models for Grammar Parsing and Classification

This paragraph delves into the application of Seq2Seq models for grammar parsing and multi-label classification tasks. It explains how a tree structure can be represented as a sequence and fed into a Seq2Seq model for grammar parsing. The paragraph also discusses how multi-label classification problems, where an instance can belong to multiple classes, can be solved using Seq2Seq models by allowing the model to output variable-length sequences.

20:08

🔍 Overview of Transformer Architecture

This paragraph provides an overview of the Transformer architecture, which is the focus of the lecture. It explains that the Transformer consists of an Encoder and a Decoder, and the Encoder's primary function is to take a sequence of vectors as input and output another sequence of vectors. The paragraph introduces the concept of self-attention and its role in the Encoder architecture.

25:08

🧱 Detailed Structure of Transformer Encoder

This paragraph goes into the detailed structure of the Transformer Encoder. It explains the various components and operations within each block of the Encoder, including multi-head self-attention, residual connections, layer normalization, and feed-forward networks. The paragraph also discusses the reasoning behind certain design choices and potential alternatives proposed in research papers.

30:09

🔄 Alternatives and Improvements to Transformer Architecture

This paragraph discusses potential alternatives and improvements to the original Transformer architecture. It mentions research papers that explore the positioning of layer normalization within the blocks and the use of different normalization techniques, such as Power Normalization, which may outperform layer normalization in certain scenarios. The paragraph encourages exploring and considering alternative architectures for optimal performance.

Mindmap

Keywords

💡Transformer

The Transformer is a type of neural network architecture that is particularly well-suited for handling sequences of data, such as text or audio. In the video, it's highlighted as a core technology behind many natural language processing tasks, including machine translation and speech recognition. The mention of Transformers being related to BERT indicates its importance in the development of state-of-the-art models for processing human language.

💡Sequence-to-sequence model (Seq2Seq)

Seq2Seq models are designed to convert sequences from one domain (input sequence) into sequences in another domain (output sequence), with potentially different lengths. This concept is crucial in the video, especially when discussing tasks like machine translation where the model needs to determine the output length dynamically. The flexibility of Seq2Seq models makes them suitable for a variety of applications including speech recognition and text generation.

💡Speech recognition

Speech recognition is the process of converting spoken language into text. In the video, it's presented as a prime example of a Seq2Seq model application. The model listens to the input audio signal, processes it as a sequence of vectors, and outputs the corresponding textual representation. This task illustrates the Seq2Seq model's ability to handle variable input and output lengths.

💡Machine translation

Machine translation involves converting text from one language to another. The video uses this task to illustrate the application of Seq2Seq models, demonstrating how the input text in one language is transformed into output text in another language. This process underlines the model's capability to determine the relationship between the lengths of input and output sequences.

Highlights

Transformer is a sequence-to-sequence (seq2seq) model, which takes in a sequence as input and outputs another sequence, but the output length is determined by the machine itself.

Transformer can be applied to various tasks like speech recognition, machine translation, chatbots, question answering, grammar parsing, multi-label classification, and object detection, by treating the task as a seq2seq problem.

The concept of using seq2seq models for tasks like grammar parsing, which traditionally do not seem like seq2seq problems, was introduced in a 2014 paper titled "Grammar as a Foreign Language".

Transformer has an encoder and a decoder architecture, with the encoder processing the input sequence and the decoder determining the output sequence.

In the Transformer encoder, each block consists of a multi-head self-attention layer, followed by a residual connection and layer normalization, then a feed-forward network with residual connection and layer normalization.

The self-attention layer in the Transformer encoder considers the entire input sequence and outputs a vector for each position, allowing the model to capture long-range dependencies.

The residual connection in the Transformer encoder adds the input vector to the output vector, while layer normalization normalizes the vector across its dimensions.

The original Transformer architecture design may not be optimal, as suggested by research papers exploring alternative designs, such as changing the order of layer normalization and residual connections.

A paper titled "On Layer Normalization in the Transformer Architecture" proposes moving the layer normalization before the residual connection, improving performance.

Another paper, "Power Norm: Rethinking Batch Normalization In Transformers", suggests using power normalization instead of layer normalization, potentially improving performance.

The Transformer encoder processes the input sequence using positional encoding, as self-attention alone cannot capture the order of the sequence.

The Transformer decoder architecture, which determines the output sequence, is not covered in detail in this transcript.

Transformer models can be trained on large datasets, such as TV shows and movies for chatbots, or paired data like audio and transcripts for speech recognition and translation tasks.

The transcript provides an example of training a Transformer model on 1,500 hours of Taiwanese drama data to perform Taiwanese speech recognition and translation to Chinese text.

The transcript discusses the potential of using Transformer models for end-to-end speech translation, particularly for languages without written forms, by training on paired audio and text data from another language.

Transcripts

00:02

好 那接下來

00:04

我們要講這個作業五

00:06

大家會用上的Transformer

00:09

那我們在之前已經提了Transformer

00:11

提了不下N次

00:13

那如果你們還不知道

00:14

Transformer是什麼的話

00:15

Transformer其實就是變形金剛知道嗎

00:18

變形金剛的英文就是Transformer

00:22

那Transformer也跟我們之後會

00:24

提到的BERT有非常強烈的關係

00:28

所以這邊有一個BERT探出頭來

00:31

代表說Transformer跟BERT

00:33

是很有關係的

00:35

那Transformer是什麼呢

00:37

Transformer就是一個

00:39

Sequence-to-sequence的model

00:42

那Sequence-to-sequence的model

00:44

他的縮寫

00:45

我們會寫做Seq2seq

00:47

那Sequence-to-sequence的model

00:49

又是什麼呢

00:50

我們之前在講input a sequence的

00:53

case的時候

00:54

我們說input是一個sequence

00:56

那output有幾種可能

00:58

一種是input跟output的長度一樣

01:01

這個是在作業二的時候做的

01:04

有一個case是output指

01:06

output一個東西

01:07

這個是在作業四的時候做的

01:09

那接來作業五的case是

01:11

我們不知道應該要output多長

01:15

由機器自己決定output的長度

01:20

那有什麼樣的例子

01:21

有什麼樣的應用

01:22

是我們需要用到這種

01:24

Sequence-to-sequence的model

01:26

也就是input是一個sequence

01:27

output是一個sequence

01:28

但是我們不知道output應該有的長度

01:31

應該要由機器來自己決定

01:33

output的長度

01:34

有什麼樣的應用呢

01:35

舉例來說

01:36

一個很好的應用就是 語音辨識

01:39

在做語音辨識的時候

01:40

輸入是聲音訊號

01:42

我們在這一門課裡面

01:44

已經看過好多次

01:44

輸入的聲音訊號其實就是

01:47

一串的vector

01:49

輸出是什麼

01:50

輸出是語音辨識的結果

01:53

也就是輸入的這段聲音訊號

01:55

所對應的文字

01:56

我們這邊用圈圈來代表文字

02:00

每一個圈圈就代表

02:02

比如說中文裡面的一個方塊子

02:05

今天輸入跟輸出的長度

02:08

當然是有一些關係

02:10

但是卻沒有絕對的關係

02:12

我們說輸入的聲音訊號

02:14

他的長度是大T

02:15

我們並沒有辦法知道說

02:18

根據大T輸出的這個長度N

02:21

一定是多少

02:22

怎麼辦呢 由機器自己決定

02:23

由機器自己去聽這段聲音訊號的內容

02:28

自己決定他應該要輸出幾個文字

02:31

他輸出的語音辨識結果

02:33

輸出的句子裡面應該包含幾個字

02:36

由機器自己來決定

02:38

這個是語音辨識

02:40

還有很多其他的例子

02:42

比如說作業五我們會做機器翻譯

02:45

讓機器讀一個語言的句子

02:47

輸出另外一個語言的句子

02:50

那在做機器翻譯的時候

02:52

輸入的文字的長度是N

02:54

輸出的句子的長度是N'

02:57

那N跟N'之間的關係

03:00

也要由機器自己來決定

03:03

我們說輸入機器學習這個句子

03:05

輸出是machine learning

03:07

輸入是有四個字

03:10

輸出有兩個英文的詞彙

03:13

但是並不是所有中文跟英文的關係

03:15

都是輸出就是輸入的二分之一

03:18

到底輸入一段句子

03:20

輸出英文的句子要多長

03:23

由機器自己決定

03:25

甚至你可以做更複雜的問題

03:27

比如說做語音翻譯

03:30

什麼叫做語音翻譯

03:31

語音翻譯就是

03:32

你對機器說一句話

03:34

比如說machine learning

03:36

他輸出的不是英文

03:38

他直接把他聽到的英文的

03:40

聲音訊號翻譯成中文

03:42

你對他說machine learning

03:44

他輸出的是機器學習

03:47

你可能會問說

03:48

為什麼我們要做

03:49

Speech Translation這樣的任務

03:51

為什麼我們不說

03:52

我們直接做一個語音辨識

03:54

再做一個機器翻譯

03:56

把語音辨識系統跟機器翻譯系統

03:59

接起來 就直接是語音翻譯

04:02

那是因為其實世界上有很多語言

04:04

他根本連文字都沒有

04:07

世界上有超過七千種語言

04:10

那其實在這七千種語言

04:12

有超過半數其實是沒有文字的

04:15

對這些沒有文字的語言而言

04:18

你要做語音辨識

04:19

可能根本就沒有辦法

04:21

因為他沒有文字

04:22

所以你根本就沒有辦法做語音辨識

04:24

但我們有沒有可能對這些語言

04:27

做語音翻譯

04:29

直接把它翻譯成

04:31

我們有辦法閱讀的文字

04:34

一個很好的例子也許就是

04:36

台語的語音辨識

04:38

但我不會說台語沒有文字

04:40

很多人覺得台語是有文字的

04:41

但台語的文字並沒有那麼普及

04:43

現在聽說小學都有教台語的文字了

04:47

但台語的文字

04:48

並不是一般人能夠看得懂的

04:51

所以如果你做語音辨識

04:53

你給機器一段台語

04:56

然後它可能輸出是母湯

04:58

你根本就不知道

04:59

這段話在說什麼對不對

05:01

所以我們期待說機器也許可以做翻譯

05:04

做語音的翻譯

05:05

對它講一句台語

05:07

它直接輸出的是同樣意思的

05:10

中文的句子

05:11

那這樣一般人就可以看懂

05:14

那有沒有可能做到這件事呢

05:16

有沒有可能訓練一個類神經網路

05:18

這個類神經網路聽某一種語言

05:20

的聲音訊號

05:21

輸出是另外一種語言的文字呢

05:24

其實是有可能的

05:26

那對於台語這個例子而言

05:28

我們知道說

05:29

今天你要訓練一個neural network

05:31

你就需要有input跟output的配合

05:34

你需要有台語的聲音訊號

05:36

跟中文文字的對應關係

05:39

那這樣的資料好不好蒐集呢

05:41

這樣的資料

05:43

並不是沒有可能蒐集的

05:45

比如說YouTube上面

05:46

有很多的鄉土劇

05:47

你知道鄉土劇就是

05:49

台語語音 中文字幕

05:51

所以你只要它的台語語音載下來

05:53

中文字幕載下來

05:55

你就有台語聲音訊號

05:56

跟中文之間的對應關係

05:58

你就可以硬train一個模型

06:00

你就可以train我們剛才講的

06:01

我們等一下要講的Transformer

06:03

然後叫機器直接做台語的語音辨識

06:07

輸入台語 輸出中文

06:10

那你可能會覺得這個想法很狂

06:12

而且好像 聽起來有很多很多的問題

06:14

那我們實驗室就載了

06:15

一千五百個小時的鄉土劇的資料

06:18

然後 就真的拿來訓練一個

06:20

語音辨識系統

06:21

你可能會覺得說

06:22

這聽起來有很多的問題

06:23

舉例來說 鄉土劇有很多雜訊

06:26

有很多的音樂

06:27

不要管它這樣子

06:29

然後 鄉土劇的字幕

06:31

不一定跟聲音有對起來

06:33

就不要管它這樣子

06:34

然後呢你可能會想說

06:37

台語不是還有一些

06:39

比如說台羅拼音

06:40

台語也是有類似音標這種東西

06:42

也許我們可以先辨識成音標

06:44

當作一個中介

06:45

然後在從音標轉成中文

06:47

也沒有這樣做 直接訓練一個模型

06:49

輸入是聲音訊號

06:50

輸出直接就是中文的文字

06:53

這種沒有想太多 直接資料倒進去

06:56

就訓練一個模型的行為

06:58

就叫作硬train一發知道嗎

07:03

那你可能會想說

07:04

這樣子硬train一發到底能不能夠

07:07

做一個台語語音辨識系統呢

07:09

其實 還真的是有可能的

07:12

以下是一些真正的結果

07:15

機器在聽的一千五百個小時的

07:18

鄉土劇以後

07:19

你可以對它輸入一句台語

07:21

然後他就輸出一句中文的文字

07:23

以下是真正的例子

07:24

機器聽到的聲音是這樣子的

07:25

可以做一下台語的聽力測驗

07:28

看看你辨識出來的跟機器是不是一樣的

07:31

機器聽到這樣的句子

07:33

你的身體撐不住(台語)

07:34

那機器輸出是什麼呢

07:36

它的輸出是 你的身體撐不住

07:38

這個聲音訊號是你的身體撐不住(台語)

07:41

但機器並不是輸出無勘

07:43

而是它就輸出撐不住

07:45

或者是機器聽到的

07:47

是這樣的聲音訊號

07:48

沒事你為什麼要請假(台語)

07:50

沒事你為什麼要請假

07:52

機器聽到沒事(台語)

07:54

它並不是輸出 沒代沒誌

07:56

它是輸出 沒事

07:58

這樣聽到四個音節沒代沒誌(台語)

08:00

但它知道說台語的沒代沒誌(台語)

08:02

翻成中文 也許應該輸出 沒事

08:05

所以機器的輸出是

08:06

沒事你為什麼要請假

08:08

但機器其實也是蠻容易犯錯的

08:10

底下特別找機個犯錯的例子

08:12

給你聽一下

08:13

你聽聽這一段聲音訊號

08:15

不會膩嗎(台語)

08:16

他說不會膩嗎(台語)

08:18

我自己聽到的時候我覺得

08:19

我跟機器的答案是一樣的

08:20

就是說要生了嗎

08:23

但其實這句話

08:24

正確的答案就是

08:25

不會膩嗎(台語)

08:27

不會膩嗎

08:29

當然機器在倒裝

08:30

你知道有時候你從台語

08:32

轉成中文句子需要倒裝

08:34

在倒裝的部分感覺就沒有太學起來

08:38

舉例來說它聽到這樣的句子

08:40

我有跟廠長拜託(台語)

08:42

他說我有跟廠長拜託(台語)

08:44

那機器的輸出是

08:45

我有幫廠長拜託

08:47

但是你知道說這句話

08:49

其實是倒裝

08:50

我有跟廠長拜託(台語)

08:52

是我拜託廠長

08:54

但機器對於它來說

08:55

如果台語跟中文的關係需要倒裝的話

08:58

看起來學習起來還是有一點困難

09:00

這個例子想要告訴你說

09:02

直接台語聲音訊號轉繁體中文

09:05

不是沒有可能

09:06

是有可能可以做得到的

09:09

那其實台灣有很多人都在做

09:10

台語的語音辨識

09:11

如果你想要知道更多有關

09:13

台語語音辨識的事情的話

09:15

可以看一下下面這個網站

09:18

那台語語音辨識反過來

09:20

就是台語的語音合成對不對

09:24

我們如果是一個模型

09:25

輸入台語聲音 輸出中文的文字

09:28

那就是語音辨識

09:30

反過來 輸入文字 輸出聲音訊號

09:34

就是語音合成

09:36

這邊就是demo一下台語的語音合成

09:40

這個資料用的是

09:41

台灣 媠聲(台語)的資料

09:42

來找GOOGLE台灣媠聲(台語)

09:44

就可以找到這個資料集

09:45

裡面就是台語的聲音訊號

09:48

聽起來像是這個樣子

09:50

比如說你跟它說

09:51

歡迎來到台灣台大語音處理實驗室

09:54

不過這邊是需要跟大家說明一下

09:56

現在還沒有真的做End to End的模型

09:58

這邊模型還是分成兩階

10:00

他會先把中文的文字

10:02

轉成台語的台羅拼音

10:05

就像是台語的KK音標

10:07

在把台語的KK音標轉成聲音訊號

10:10

不過從台語的KK音標

10:11

轉成聲音訊號這一段

10:12

就是一個像是Transformer的network

10:15

其實是一個叫做echotron的model

10:18

它本質上就是一個Seq2Seq model

10:20

大概長的是這個樣子

10:22

所以你輸入文字

10:24

歡迎來到台大語音處理實驗室

10:26

機器的輸出是這個樣子的

10:28

歡迎來到台大(台語)

10:30

語音處理實驗室(台語)

10:32

或是你對他說這一句中文

10:35

然後他輸出的台語是這個樣子

10:38

最近肺炎真嚴重(台語)

10:40

要記得戴口罩 勤洗手(台語)

10:43

有病就要看醫生(台語)

10:45

所以你真的是可以

10:46

合出台語的聲音訊號的

10:49

就用我們在這一門課裡面學到的

10:52

Transformer或者是Seq2Seq的model

10:56

剛才講的是跟語音比較有關的

10:59

那在文字上

11:01

也會很廣泛的使用了Seq2Seq model

11:05

舉例來說你可以用Seq2Seq model

11:08

來訓練一個聊天機器人

11:11

聊天機器人就是你對它說一句話

11:14

它要給你一個回應

11:16

輸入輸出都是文字

11:18

文字就是一個vector Sequence

11:21

所以你完全可以用Seq2Seq 的model

11:24

來做一個聊天機器人

11:27

那怎麼訓練一個聊天機器人呢

11:29

你就要收集大量人的對話

11:31

像這種對話你可以收集

11:33

電視劇 電影的台詞 等等

11:36

你可以收集到

11:36

一堆人跟人之間的對話

11:39

假設在對話裡面有出現

11:40

某一個人說Hi

11:42

和另外一個人說

11:43

Hello How are you today

11:44

那你就可以教機器說

11:46

看到輸入是Hi

11:47

那你的輸出就要跟

11:49

Hello how are you today

11:51

越接近越好

11:52

那就可以訓練一個Seq2Seq model

11:55

那跟它說一句話

11:56

它就會給你一個回應

11:59

那事實上Seq2Seq model

12:01

在NLP的領域

12:02

在natural language processing的領域

12:04

的使用

12:05

是比你想像的更為廣泛

12:09

其實很多natural language processing的任務

12:12

都可以想成是question answering

12:16

QA的任務

12:18

怎麼說呢

12:19

所謂的Question Answering

12:20

就是給機器讀一段文字

12:23

然後你問機器一個問題

12:25

希望他可以給你一個正確的答案

12:28

很多你覺得跟question answering

12:30

沒什麼關係的任務

12:32

都可能可以想像成是QA

12:36

怎麼說呢 舉例來說

12:37

假設你今天想做的是翻譯

12:39

那機器讀的文章就是一個英文句子

12:42

問題是什麼 問題就是

12:44

這個句子的德文翻譯是什麼

12:46

然後輸出的答案就是德文

12:49

或者是你想要叫機器自動作摘要

12:51

摘要就是給機器讀一篇長的文章

12:53

叫他把長的文章的重點節錄出來

12:56

那你就是給機器一段文字

12:58

問題是這段文字的摘要是什麼

13:01

然後期待他可以輸出一個摘要

13:03

或者是你想要叫機器做

13:05

Sentiment analysis

13:06

什麼是Sentiment analysis呢

13:08

就是機器要自動判斷一個句子

13:09

是正面的還是負面的

13:11

像這樣子的應用在

13:13

假設你有做了一個產品

13:14

然後上線以後

13:15

你想要知道網友的評價

13:17

但是你又不可能一直

13:18

找人家ptt上面

13:18

把每一篇文章都讀過

13:20

所以怎麼辦

13:21

你就做一個Sentiment analysis model

13:23

看到有一篇文章裡面

13:24

有提到你的產品

13:26

然後就把這篇文章丟到

13:27

你的model裡面

13:28

去判斷這篇文章

13:29

是正面還是負面

13:31

怎麼把sentiment analysis這個問題

13:33

看成是QA的問題呢

13:35

你就給機器

13:36

你要判斷正面還負面的文章

13:38

你問題就是這個句子

13:40

是正面還是負面的

13:41

然後希望機器可以告訴你答案

13:43

所以各式各樣的NLP的問題

13:46

往往都可以看作是QA的問題

13:50

而QA的問題

13:51

就可以用Seq2Seq model來解

13:56

QA的問題怎麼用

13:57

Seq2Seq model來解呢

14:00

就是有一個Seq2Seq model輸入

14:03

就是有問題跟文章把它接在一起

14:06

輸出就是問題的答案

14:08

就結束了

14:09

你的問題加文章合起來

14:11

是一段很長的文字

14:12

答案是一段文字

14:14

Seq2Seq model只要是輸入一段文字

14:17

輸出一段文字

14:18

只要是輸入一個Sequence

14:19

輸出一個Sequence就可以解

14:21

所以你可以把QA的問題

14:23

硬是用Seq2Seq model解

14:25

叫它讀一篇文章讀一個問題

14:27

然後就直接輸出答案

14:29

所以各式各樣NLP的任務

14:32

其實都有機會使用Seq2Seq model

14:37

但是我這邊必須要強調一下

14:39

對多數NLP的任務

14:41

或對多數的語音相關的任務而言

14:45

往往為這些任務客製化模型

14:49

你會得到更好的結果

14:51

什麼問題都Seq2Seq model

14:53

就好像說你不管做什麼事情

14:56

都用瑞士刀一樣 對不對

14:57

瑞士刀可以做各式各樣的問題

14:59

砍柴也可以用瑞士刀

15:01

切菜也可以用瑞士刀

15:02

但是它不見得是一個最好用的

15:04

所以 如果你為各式各樣不同的任務

15:07

客製化各式各樣的模型

15:09

往往可以得到

15:11

比單用Seq2Seq model更好的結果

15:14

但是各個任務客製化的模型

15:16

就不是我們這一門課的重點了

15:19

如果你對人類語言處理

15:22

包括語音 包括自然語言處理

15:24

這些相關的任務有興趣的話呢

15:26

可以參考一下以下課程網頁的連結

15:29

就是去年上的深度學習

15:32

與人類語言處理

15:33

這門課的內容裡面就會教你

15:35

各式各樣的任務最好的模型

15:38

應該是什麼

15:39

舉例來說在做語音辨識

15:41

我們剛才講的是一個Seq2Seq model

15:44

輸入一段聲音訊號

15:45

直接輸出文字

15:47

今天啊 Google的 pixel4

15:50

Google官方告訴你說

15:52

Google pixel4也是用

15:54

N to N的Neural network

15:56

pixel4裡面就是

15:57

有一個Neural network

15:58

輸入聲音訊號

15:59

輸出就直接是文字

16:01

但他其實用的不是Seq2Seq model

16:05

他用的是一個叫做

16:07

RNN transducer的 model

16:09

像這些模型他就是為了

16:11

語音的某些特性所設計

16:14

這樣其實可以表現得更好

16:16

至於每一個任務

16:17

有什麼樣客製化的模型

16:19

這個就是另外一門課的主題

16:22

就不是我們今天想要探討的重點

16:26

那我剛才講了很多Seq2Seq model

16:29

在語音還有自然語言處理上的應用

16:31

其實有很多應用

16:34

你不覺得他是一個

16:35

Seq2Seq model的問題

16:37

但你都可以硬用

16:39

Seq2Seq model的問題硬解他

16:43

舉例來說文法剖析

16:45

文法剖析要做的事情就是

16:47

給機器一段文字

16:48

比如Deep learning is very powerful

16:50

機器要做的事情是產生

16:52

一個文法的剖析樹 告訴我們

16:55

deep加learning合起來

16:57

是一個名詞片語

16:59

very加powerful合起來

17:01

是一個形容詞片語

17:03

形容詞片語加is以後會變成

17:05

一個動詞片語

17:06

動詞片語加名詞片語合起來

17:08

是一個句子

17:09

那今天文法剖析要做的事情

17:12

就是產生這樣子的一個Syntactic tree

17:15

所以在文法剖析的任務裡面

17:18

假設你想要deep learning解的話

17:20

輸入是一段文字

17:22

他是一個Sequence

17:23

但輸出看起來不像是一個Sequence

17:26

輸出是一個樹狀的結構

17:29

但事實上一個樹狀的結構

17:32

可以硬是把他看作是一個Sequence

17:36

怎麼說呢

17:37

這個樹狀結構可以對應到一個

17:40

這樣子的Sequence

17:42

從這個Sequence裡面

17:44

你也可以看出

17:44

這個樹狀的結構有一個S

17:47

有一個左括號

17:48

有一個右括號

17:49

S裡面有一個noun phrase

17:51

有一個左括號跟右括號

17:53

NP裡面有一個左括號跟右括號

17:56

NP裡面有is

17:58

然後有這個形容詞片語

18:00

他有一個左括號右括號

18:02

這一個Sequence他就代表了

18:04

這一個tree 的structure

18:06

你先把tree 的structure

18:08

轉成一個Sequence以後

18:10

你就可以用Seq2Seq model硬解他

18:14

你就train一個Seq2Seq model

18:16

讀這個句子

18:17

然後直接輸入這一串文字

18:19

再把這串文字轉成一個樹狀的結構

18:22

你就可以硬是用Seq2Seq model

18:25

來做文法剖析這件事

18:28

這個概念聽起來非常的狂

18:31

但這是真的可以做得到的

18:34

你可以讀一篇文章叫做

18:35

grammar as a Foreign Language

18:38

這篇文章其實不是太新的文章

18:40

你會發現她放在arxiv上面的時間

18:42

是14年的年底

18:44

所以其實也是一個

18:46

上古神獸等級的文章

18:48

這篇文章問世的時候

18:49

那個時候Seq2Seq model還不流行

18:52

那時候Seq2Seq model

18:54

主要只有被用在翻譯上

18:57

所以這篇文章的title才會取說

18:59

grammar as a Foreign Language

19:01

他把文法剖析這件事情

19:04

當作是一個翻譯的問題

19:07

把文法當作是另外一種語言

19:10

直接套用當時人們認為

19:12

只能用在翻譯上的模型硬做

19:14

結果他得到state of the art的結果

19:17

我其實在國際會議的時候

19:19

有遇過這個第一作者Oriol Vlnyals

19:24

那個時候Seq2Seq model

19:25

還是個非常潮的東西

19:27

那個時候在我的認知裡面

19:28

我覺得這個模型

19:29

應該是挺難train的

19:30

我問他說

19:30

train Seq2Seq model有沒有什麼tips

19:35

沒想到你做個文法剖析

19:39

用Seq2Seq model

19:40

居然可以硬做到state of the art

19:41

這應該有什麼很厲害的tips吧

19:44

他說什麼沒有什麼tips

19:45

他說我連Adam都沒有用

19:47

我直接gradient descent

19:48

就train起來了

19:49

我第一次train就成功了

19:51

只是我要衝到state of the art

19:53

還是稍微調了一下參數而已

19:58

我也不知道是真的還假的啦

19:59

不過今天Seq2Seq model

20:01

真的是已經被很廣泛地

20:03

應用在各式各樣的應用上了

20:07

還有一些任務可以用seq2seq's model

20:10

舉例來說 multi-label的classification

20:14

什麼是multi-label的classification呢

20:17

這邊你要比較一下

20:18

multi-class的classification

20:20

跟multi-label的classification

20:23

multi-class的classification

20:24

跟multi-label的classification

20:26

聽起來名字很像

20:27

但他們其實是不一樣的事情

20:29

multi-class的classification意思是說

20:31

我們有不只一個class機器要做的事情

20:35

是從數個class裡面

20:37

選擇某一個class出來

20:40

但是multi-label的classification

20:42

意思是說同一個東西

20:44

它可以屬於多個 不只一個class

20:48

舉例來說 你在做文章分類的時候

20:50

可能這篇文章 屬於class 1跟3

20:53

這篇文章屬於class 3 9 17等等

20:58

你可能會說

20:59

這種multi-label classification的問題

21:01

要怎麼解呢 能不能直接把它當作一個

21:04

multi-class classification的問題來解

21:06

舉例來說

21:07

我把這些文章丟到一個class file裡面

21:11

本來class file只會輸出一個答案

21:13

輸出分數最高的那個答案

21:15

我現在就輸出分數最高的前三名

21:18

看看能不能解

21:19

multi-label的classification的問題

21:21

但這種方法可能是行不通的 為什麼

21:25

因為每一篇文章對應的class的數目

21:28

根本不一樣 有些東西 有些文章

21:32

對應的class的數目

21:33

是兩個 有的是一個 有的是三個

21:35

所以 如果你說 我直接取一個threshold

21:37

我直接取分數最高的前三名

21:40

class file output分數最高的前三名

21:42

來當作我的輸出 顯然

21:43

不一定能夠得到好的結果 那怎麼辦呢

21:47

這邊可以用seq2seq硬做 你知道嗎

21:50

輸入一篇文章 輸出就是class 就結束了

21:54

機器自己決定 它要輸出幾個class

21:57

我們說seq2seq model

21:59

就是由機器自己決定輸出幾個東西

22:02

輸出的output sequence的長度是多少

22:04

既然 你沒有辦法決定class的數目

22:06

怎麼辦 機器幫你決定 它自己決定

22:10

每篇文章 要屬於多少個class

22:14

或者是object detection

22:16

這個看起來跟seq2seq model

22:18

應該八竿子打不著的問題

22:20

它也可以用seq2seq's model硬解

22:23

object detection就是給機器一張圖片

22:26

然後它把圖片裡面的物件框出來

22:28

把它框出說 這個是斑馬 這個也是斑馬

22:32

但這種問題 可以用seq2seq's硬做

22:36

至於怎麼做 我們這邊就不細講

22:39

我在這邊放一個文獻

22:41

放一個連結給大家參考

22:43

講這麼多就是要告訴你說

22:45

seq2seq's model 它是一個

22:47

很powerful的model

22:48

它是一個很有用的model

22:50

我們現在就是要來學

22:52

怎麼做seq2seq這件事

22:56

一般的seq2seq's model

22:58

它裡面會分成兩塊 一塊是Encoder

23:04

另外一塊是Decoder

23:06

你input一個sequence有Encoder

23:09

負責處理這個sequence

23:10

再把處理好的結果丟給Decoder

23:12

由Decoder決定

23:14

它要輸出什麼樣的sequence

23:16

等一下 我們都還會再細講

23:19

Encoder跟 Decoder內部的架構

23:22

seq2seq's model的起源

23:24

其實非常的早 在14年的9月

23:28

就有一篇seq2seq's model

23:30

用在翻譯的文章 被放到Arxiv上

23:34

可以想像當時的seq2seq's model

23:37

看起來還是比較陽春的

23:39

今天講到seq2seq's model的時候

23:41

大家第一個會浮現在腦中的

23:43

可能都是我們今天的主角

23:46

也就是transformer

23:47

它有一個Encoder架構

23:49

有一個Decoder架構

23:50

它裡面有很多花花綠綠的block

23:53

等一下就會講一下

23:54

這裡面每一個花花綠綠的block

23:56

分別在做的事情是什麼

23:59

接下來 我們就來講Encoder的部分

24:03

seq2seq's model Encoder要做的事情

24:07

就是給一排向量輸出另外一排向量

24:13

給一排向量 輸出一排向量這件事情

24:16

很多模型都可以做到

24:19

可能第一個想到的是

24:20

我們剛剛講完的self-attention

24:22

其實不只self-attention

24:24

RNN CNN 其實也都能夠做到

24:27

input一排向量

24:28

output另外一個同樣長度的向量

24:32

在transformer裡面

24:34

transformer的Encoder

24:36

用的就是self-attention

24:39

這邊看起來有點複雜

24:40

我們用另外一張圖

24:42

來仔細地解釋一下

24:43

這個Encoder的架構

24:45

等一下再來跟原始的transformer的

24:48

論文裡面的圖進行比對

24:51

現在的Encoder裡面

24:53

會分成很多很多的block

24:57

每一個block都是輸入一排向量

25:00

輸出一排向量

25:01

你輸入一排向量 第一個block

25:03

第一個block輸出另外一排向量

25:05

再輸給另外一個block

25:07

到最後一個block

25:08

會輸出最終的vector sequence

25:11

每一個block 其實

25:12

並不是neural network的一層

25:14

這邊之所以不稱說

25:16

每一個block是一個layer

25:17

是因為每一個block裡面做的事情

25:19

是好幾個layer在做的事情

25:22

在transformer的Encoder裡面

25:24

每一個block做的事情

25:26

大概是這樣子的

25:27

先做一個self-attention

25:29

input一排vector以後

25:31

做self-attention

25:32

考慮整個sequence的資訊

25:34

Output另外一排vector.

25:36

接下來這一排vector

25:38

會再丟到fully connected的feed forward network裡面

25:43

再output另外一排vector

25:45

這一排vector就是block的輸出

25:48

事實上在原來的transformer裡面

25:51

它做的事情是更複雜的

25:54

實際上做的事情是這個樣子的

25:56

這是self-attention的layer

25:58

在我們剛才self-attention的時候

25:59

我們說 輸入一排vector

26:01

就輸出一排vector

26:02

這邊的每一個vector

26:04

它是考慮了 所有的input以後

26:07

所得到的結果 在transformer裡面

26:10

它加入了一個設計

26:12

是說 我們不只是輸出這個vector

26:15

我們還要把這個vector加上它的input

26:19

它要把input拉過來 直接加給輸出

26:23

得到新的output 也就是說

26:26

這邊假設這個vector叫做A

26:28

這個vector叫做B 你要把A B加起來

26:31

當作是新的輸出 這件事情

26:35

這樣子的network架構

26:36

叫做residual connection

26:39

那其實這種residual connection

26:41

在deep learning的領域用的是非常的廣泛

26:43

之後如果我們有時間的話

26:45

再來詳細介紹

26:46

為什麼要用residual connection

26:48

那你現在就先知道說 有一種connection

26:50

有一種network設計的架構

26:52

叫做residual connection

26:54

它會把input直接跟output加起來

26:58

得到新的vector

27:00

得到residual的結果以後

27:03

再把它做一件事情叫做normalization

27:07

這邊用的不是batch normalization

27:11

這邊用的叫做layer normalization

27:15

layer normalization做的事情

27:18

比bacth normalization更簡單一點

27:20

layer normalization做的事情

27:21

是這個樣子的

27:23

輸入一個向量 輸出另外一個向量

27:25

這邊不需要考慮batch

27:26

剛剛在講batch normalization的時候

27:28

需要考慮batch

27:29

那這邊這個layer normalization

27:31

不用考慮batch的資訊

27:32

輸入一個向量 輸出另外一個向量

27:35

這個layer normalization

27:37

做的事情是什麼呢

27:38

它會把輸入的這個向量

27:40

計算它的mean跟standard deviation

27:44

但是要注意一下 剛才在講

27:45

batch normalization的時候

27:47

我們是對同一個dimension

27:49

不同的feature

27:51

我們是對不同example

27:53

不同feature的同一個dimension

27:55

去計算mean跟standard deviation

27:58

但layer normalization

27:59

它是對同一個feature

28:02

同一個example裡面

28:04

不同的dimension

28:05

去計算mean跟standard deviation

28:09

計算出mean

28:10

跟standard deviation以後

28:11

就可以做一個normalize

28:13

output這邊的每一個vector

28:15

就是把原來input的vector

28:19

我發現這邊有一個bug

28:20

這個bug是什麼

28:21

這個bug就是這邊不需要 '

28:23

不好意思 記得把這個 '拿掉

28:26

這邊不需要 ' 我們把input vector

28:29

input 這個vector裡面每一個

28:31

dimension減掉m 減掉mean

28:33

再除以standard deviation以後得到x'

28:36

就是layer normalization的輸出

28:40

得到layer normalization的輸出以後

28:43

它的這個輸出 才是FC network的輸入

28:48

而FC network這邊

28:50

也有residual的架構

28:52

所以 我們會把FC network的input

28:55

跟它的output加起來 做一下residual

28:58

得到新的輸出 這個才是

29:01

transformer encoder裡面

29:03

一個block的輸出

29:05

這邊還有一件事情是漏講了

29:07

這個FC network做完residual以後

29:10

還不是結束 你要把residual的結果

29:13

再做一次layer normalization

29:16

這邊已經做過一次了

29:17

這邊還要再做一次 得到的輸出

29:19

才是residual network裡面

29:22

一個block的輸出

29:23

所以這個是挺複雜的

29:27

所以我們這邊講的 這一個圖

29:29

其實就是我們剛才講的那件事情

29:31

首先 你有self-attention

29:34

其實在input的地方

29:36

還有加上positional encoding

29:39

我們之前已經有講過

29:40

如果你只光用self-attention

29:42

你沒有未知的資訊

29:44

所以你需要加上positional的information

29:47

然後在這個圖上

29:48

有特別畫出positional的information

29:51

這一塊它這邊寫一個

29:53

Multi-Head Attention

29:54

這個就是self-attention的block

29:56

這邊有特別強調說

29:58

它是Multi-Head的self-attention

30:01

這邊有一個Add&norm是什麼意思

30:05

就是residual加layer normalization

30:09

我們剛才有說self-attention

30:11

有加上residual的connection

30:13

加下來還要過layer normalization

30:16

這邊這個圖上的Add&norm

30:18

就是residual加layer norm的意思

30:22

接下來 這邊要過

30:24

feed forward network

30:25

fc的feed forward network

30:26

以後再做一次Add&norm

30:29

再做一次residual加layer norm

30:32

才是一個block的輸出

30:34

然後這個block會重複n次

30:38

這個複雜的block

30:40

其實在之後會講到的

30:41

一個非常重要的模型BERT裡面

30:44

會再用到 BERT

30:46

它其實就是transformer的encoder

30:50

講到這邊 你心裡一定充滿了問號

30:54

就是為什麼 transformer的encoder

30:57

要這樣設計 不這樣設計行不行

30:59

行 不一定要這樣設計

31:02

這個encoder的network架構

31:04

現在設計的方式

31:05

我是按照原始的論文講給你聽的

31:08

但原始論文的設計 不代表它是最好的

31:12

最optimal的設計 舉例來說

31:14

有一篇文章叫

31:16

on layer normalization in the transformer architecture

31:19

它問的問題就是 為什麼

31:22

layer normalization是放在那個地方呢

31:24

為什麼我們是先做

31:26

residual再做layer normalization

31:29

能不能夠把layer normalization

31:37

放到每一個block的input

31:39

也就是說 你做residual以後

31:43

再做layer normalization

31:44

再加進去 你可以看到說左邊這個圖

31:47

是原始的transformer

31:49

右邊這個圖是稍微把block

31:52

更換一下順序以後的transformer

31:54

更換一下順序以後 結果是會比較好的

31:57

這就代表說

31:58

原始的transformer 的架構

32:00

並不是一個最optimal的設計

32:02

你永遠可以思考看看

32:04

有沒有更好的設計方式

32:06

再來還有一個問題就是

32:08

為什麼是layer norm 為什麼是別的

32:10

不是別的

32:11

為什麼不做batch normalization

32:13

也許這篇paper可以回答你的問題

32:16

這篇paper是Power Norm:

32:17

Rethinking Batch Normalization In Transformers

32:20

它首先告訴你說 為什麼

32:22

batch normalization不如

32:24

layer normalization

32:25

在Transformers裡面為什麼

32:26

batch normalization不如

32:28

layer normalization

32:29

接下來在說

32:30

它提出來一個power normalization

32:32

一聽就是很power的意思

32:34

都可以比layer normalization

32:36

還要performance差不多或甚至好一點

Rate This

5.0 / 5 (0 votes)

Related Tags
NLPSpeech RecognitionMachine TranslationDeep LearningTransformerSequence-to-SequenceEncoder-DecoderLanguage ModelingComputational LinguisticsNeural Networks