Neural Machine Translation และ Attention Mechanism: เข้าใจด้วย Animation

Chompakorn C
AIResearch.in.th
Published in
5 min readAug 11, 2020

การทำ Sequence to sequence model นั้นถือเป็นโจทย์ที่ท้าทายมากในงานของ deep learning โดยหน้าที่หลักของโจทย์นี้คือการให้ model เราแปลง sequence ของข้อมูลชุดนึงเป็นอีกรูปแบบนึงที่มีความสอดคล้องกัน

การแปลภาษาถือเป็นหนึ่งในโจทย์ sequence-to-sequence ที่เราจะเรียกกันว่า Machine Translation ซึ่งเราจะพยายามทำให้ model นั้นแปลงจากประโยค (sequence ของคำ) ภาษานึงไปเป็นประโยคของอีกภาษานึง (จาก source ไป target) ตัวอย่างเช่น หากเราต้องแปลงคำว่า “I am once again asking for your financial support” ตัว machine translation model ของเราจะทำงานดังรูป

Fig. 1: Machine Translation Model

โดย source คือ “I am once again asking for your financial support” และ target คือ “ฉัน ขอ การสนับสนุน ทาง การเงิน ของ คุณ อีกครั้ง”

หลายคนอาจจะสงสัยว่า model จะอ่านคำ input แต่ละคำ เช่น I, am, once, ฯลฯ ยังไง คำตอบคือ เราจะแทนคำหนึ่งคำด้วยชุดของตัวเลข (หรือที่เราเรียกว่า vector นั่นเอง) ซึ่งเราจะได้ vector พวกนี้จากเทคนิคที่เรียกว่า word embedding นั่นเอง

Fig. 2: Word Embedding

ภายใน Machine Translation Model: Encoder-Decoder

จากในรูปที่ 1 จะสังเกตเห็นได้ว่า machine translation model นั้นรับ input เป็น sequence ของคำในประโยค (source sentence) และ output เป็น sequence ของคำที่เป็นประโยคในอีกภาษา (target sentence) แต่จริงๆแล้ว ภายใน machine translation model นั้นประกอบไปด้วย 2 ส่วนหลักๆ ได้แก่ Encoder และ Decoder

Fig. 2: Encoder-Decoder Architecture

จะสังเกตได้ว่า Encoder มีหน้าที่แปลงประโยคทั้งประโยคออกมาเป็นชุดตัวเลข 1 ชุดหรือที่เราเรียกว่า vector นั่นเอง โดยเราจะเรียก vector ตัวนี้ว่า context vector หรือเป็น vector ที่มีใจความ (context) ของประโยคทั้งประโยคอยู่

ส่วน Decoder นั้นจะมีหน้าที่ถอดความ context vector ออกมาเป็นประโยคอีกทีนึง

Encoder

หากคุณเป็นคนที่คุ้นชินกับงาน deep learning แล้ว คุณคงคุ้นชินกับสิ่งที่เรียกว่า Recurrent Neural Network (RNN) เป็นอย่างดี ซึ่งคุณสมบัติของมันคือความสามารถในการเรียนรู้ข้อมูลที่เป็น sequence ได้ โดย RNN จะรับ input 2 ตัวต่อ 1 timestep ดังรูป

Fig. 3: Encoder RNN

จะสังเกตได้ว่า RNN จะนำ hidden state ที่ได้จากแต่ละคำก่อนหน้ามาใส่ให้กับคำถัดไปเสมอทำให้ hidden state ตัวสุดท้าย (c_8) นั้นมีข้อมูลของทุกคำอยู่ในนั้น กล่าวอีกนัยคือ c_1 จะมีแค่ข้อมูลของคำว่า “I” c_2 จะมีข้อมูลของคำว่า “I am” c_3 จะมีข้อมูลของคำว่า “I am once” ไปเรื่อยๆ จน c_8 จะมีข้อมูลของทั้งประโยคอยู่ โดย c_0 หรือ initial hidden state นั้นจะเป็น zero vector

กล่าวอีกนัยคือ Encoder จะบีบข้อมูลของ คำในแต่ละคำใน source sentence และสรุปออกมาเป็น context vector ซึ่งใช้เป็น hidden state ตัวสุดท้าย เพียงตัวเดียวเพื่อให้ decoder ถอดความออกมาเป็น target sentence

Decoder

ต่อมาจะเป็นส่วนที่เราจะนำ context vector ที่ได้จาก encoder มาถอดความออกมาเป็นประโยคอีกภาษานึง โดยเราจะใช้โครงสร้าง RNN เหมือนเดิม

Fig. 4: RNN Decoder

จะสังเกตเห็นว่า เราจะเริ่มประโยคด้วย <sos> หรือ start of sentence token โดยที่มี c_0 เป็น context vector ที่ได้มาจาก encoder ซึ่งมีข้อมูลของประโยค “I am once again asking for your financial support” และ RNN จะเดาคำต่อมาเรื่อยๆจนจบด้วย <eos> หรือว่า end of sentence นั่นเอง โดยเราเรียก RNN ที่ทำหน้าที่ถอดความ vector ออกมาเป็นประโยคว่า Decoder

Putting Encoder-Decoder Together

หากเรานำ Encoder และ Decoder มาต่อกัน เราจะสามารถเขียนได้ให้เข้าใจในรูปเดียวได้ดังรูปนี้

Fig. 5: Detailed Machine Translation Model

อย่างไรก็ตาม RNN นั้นมีข้อจำกัดของตัวมันเองซึ่งก็คือ มันไม่สามารถเรียนรู้ที่จะลืมได้ข้อมูลเก่าได้ แต่ปัญหานี้ได้ถูกแก้ด้วยการพัฒนา architecture ของ RNN ขึ้นด้วยการใช้ gated method ตัวอย่างเช่น Long Short Term Memory (LSTM), หรือ Gated Recurrent Unit (GRU)

อย่างไรก็ตามในยุคแรกของ Neural Machine Translation การพัฒนาส่วนใหญ่จะเน้นไปที่ตัว Encoder หรือ ​Decoder เพียงเท่านั้น

Attention Mechanism

ต่อมาในปี 2014 Bahdanau et al. ได้เสนองานวิจัยชิ้นใหม่ที่ได้แก้ปัญหาที่กล่าวไว้ในส่วนที่แล้วในงานวิจัยชื่อ Neural Machine Translation by Jointly Learning to Align and Translate งานวิจัยนี้ได้บอกถึงปัญหาของการทำ Neural Machine Translation ด้วย Encoder-Decoder ไว้ดังนี้

we conjecture that the use of a fixed-length vector is a bottleneck in improving the performance of this basic encoder-decoder architecture

กล่าวคือ การที่ decoder รับเพืยงแค่ context vector ตัวเดียวจาก encoder (สังเกตได้จากรูปที่ 2) ส่งผลให้ข้อมูลของทั้งประโยคนั้นถูกบีบอัดมากเกินไปและเป็นตัวจำกัดศักยภาพของ encoder-decoder machine translation model

งานวิจัยนี้ได้เสนอเทคนิคที่ได้แก้ปัญหานี้ซึ่งเทคนิคนี้มีชื่อว่า Attention Mechanism ซึ่งแทนที่เราจะใช้ context vector เพียงตัวเดียว เราจะใช้ context vector ที่ไม่เหมือนกันในแต่ละคำของ target sentence ซึ่ง context vector ของแต่ละคำจะต่างกันตามคะแนนของความสัมพันธ์ระหว่างคำของ target sentence คำนั้นและคำแต่ละคำใน source sentence เพื่อที่จะดูว่าคำต่อไปที่จะแปลควรให้ความสำคัญ (Attention) กับคำไหนของประโยค

ตัวอย่างเช่น

I am once again asking for your financial support

ฉัน ขอ การสนับสนุน ทาง การเงิน ของ คุณ อีกครั้ง

คำแต่ละคำของภาษาไทยนั้นจะเกี่ยวกับไม่กี่คำในประโยคภาษาอังกฤษ

  • ฉัน -> I, am
  • ขอ -> asking
  • การสนับสนุน -> support
  • ทาง, การเงิน-> financial
  • ของ, คุณ -> for, your
  • อีกครั้ง -> once, again

กล่าวอีกนัยคือคะแนนของคำว่า “ฉัน”- “I”, “ฉัน”-“am” จะเยอะกว่า “ฉัน” และคำอื่นๆ ซึ่งคะแนนเหล่านี้เราจะเรียกว่า Attention Score

Computing Attention Score

ในการคำนวณ context vector นั้น เราจะเริ่มหา attention score ของคำแต่ละคำ ซึ่งเราจะมองเป็นฟังก์ชั่นที่รับ input สองอย่างคือ

  1. Encoder hidden states (h_1, h_2, .., h_9)
  2. Query (คำที่เราต้องการจะหา attention score)

และ output ของฟังก์ชั่นนี้จะเป็น vector ของคะแนนที่มีจำนวนเท่า encoder hidden states ตัวอย่างเช่น หากเราต้องการหา Attention Score ของคำว่า “ฉัน” และคำอื่นๆใน source sentence ขั้นตอนจะเป็นดังรูป

Fig. 6: Calculating Attention Score

โดย f_attn จะเป็น function ที่ทำหน้าที่ให้คะแนนระหว่าง query และ คำใน source sentence ส่วน α_ฉัน คือ vector ของ attention score ที่บอกว่าคำไหนสำคัญมากน้อยแค่ไหน เช่น จากในรูป attention score ของคำว่า “ฉัน” จะเยอะที่สองคำแรก ซึ่งก็คือ “I am”

โดยในงานวิจัย Effective Approaches to Attention-based Neural Machine Translation โดย Luong et al. ระบุถึงการคำนวณ Attention Score (f_attn) ไว้ 3 แบบดังนี้

Fig. 7: Attention Score Calculation (ref. Luong et al., 2015)
  1. dot product — นำ query มา dot product กับ encoder hidden states เพื่อหา similarity ตรงๆ
  2. general — นำ query มา dot product กับ encoder hidden states ที่ผ่าน fully connected layer 1 ชั้น
  3. concat (Bahdanau et al.) — นำ query มา concatenate กับ encoder hidden states แล้วทำไปผ่าน fully connected layer ที่มี activation เป็น tanh

Attention Mechanism, Putting it all together

Fig. 8: Machine Translation with Attention Machanism

จากรูปจะสังเกตได้ว่า context vector ของ decoder ในแต่ละช่วงจะได้มาจากการ weight sum hidden states ของคำจาก encoder (h_i) กับคะแนนของแต่ละคำ (เส้นสีน้ำเงินเข้ม/อ่อน) เพื่อที่จะดูว่าเราควรจะสนใจคำไหนเป็นคำสำคัญ เช่น เส้นของ คำว่า “ฉัน” และ “I”, “am” จะมีสีเข้ม บอกว่าเราควรจะแปลคำว่าฉันด้วยการสนใจคำว่า “I” และ “am” ซึ่งคะแนนของคำแต่ละคำใน encoder

กล่าวอีกมุมนึงก็คือ Attention Mechanism จะพยายามให้ model เรียนรู้การ align คำของประโยคนึงไปยังอีกประโยคนึงนั่นเอง ซึ่งหากเรานำ attention score ของแต่ละคำมา plot เป็น matrix เราจะสามารถดูการ align ของประโยคนั้นได้

Fig. 9: Attention Score Alignment (ref. Bahdanau et al., 2014)

Key-Value Attention

จากในส่วนที่แล้ว เราพูดถึง Attention Mechanism ที่ใช้ hidden state มาเป็นตัวคำนวณ attention score ซึ่งต่อมา Daniluk et al. ได้เสนองานวิจัยอันถัดไปในชื่อ FRUSTRATINGLY SHORT ATTENTION SPANS IN NEURAL LANGUAGE MODELING ซึ่งเค้าระบุปัญหาไว้ว่า การที่เราใช้ hidden states จาก encoder เพื่อทั้งคำนวณ Attention Score และ คำนวณหา context vector นั้นค่อนข้างไม่จำเป็น และเราควรจะแยกออกมาเป็น 2 vectors โดย vector สองตัวนั้นคือ

  1. Key — ใช้สำหรับคำนวณ Attention scores เท่านั้น
  2. Value — ใช้สำหรับนำมา weight sum กับ attention score

โดย vector สองตัวนี้จะถูกสร้างขึ้นมาจาก encoder hidden states ดังรูป

Fig. 10: Attention VS Key-Value Attention

ซึ่งด้วยวิธีนี้ ก็จะทำให้ vector หนึ่งตัวไม่ต้องมีถึงสองหน้าที่นั่นเอง

ส่งท้าย

บทความนี้ผมเขียนมาเพื่อให้หลายๆคนเข้าใจ concept ของการทำ neural machine translation โดยได้รับแรงบันดาลใจมาจาก article ของคุณ Jay Alammar: Visualizing A Neural Machine Translation Model (Mechanics of Seq2seq Models With Attention) ซึ่งเป็นอีกหนึ่งแหล่งที่น่าสนใจสำหรับการศึกษาเรื่องนี้ครับ

ขอบคุณที่ตามอ่านมาจนจบนะครับ หากมีข้อผิดพลาดหรือมีข้อเสนอแนะ ก็สามารถแสดงความคิดเห็นมาได้เลยนะครับ ยินดีที่จะรับฟังและนำไปแก้ไขครับ : )

บทความนี้จัดทำโดยสถาบันวิจัยปัญญาประดิษฐ์ประเทศไทย (AIResearch)

Facebook & Medium: AIResearch.in.th

Reference

[1] Bahdanau D., Cho K., and Bengio Y. (2014): “Neural Machine Translation by Jointly Learning to Align and Translate,” arXiv preprint arXiv:1409.0473

[2] Luong M. T., Phan H., and Manning C. D. (2015): “Approaches to Attention-based Neural Machine Translation,” arXiv preprint arXiv:1508.04025

[3] Daniluk M., Rocktashel T., Welbl J., and Riedel S. (2017): “Frustratingly Short Attention Spans in Neural Language Modeling,” arXiv preprint arXiv:1702.04521

[4] Alammar J. (2018): “Visualizing A Neural Machine Translation Model (Mechanics of Seq2seq Models With Attention),” https://jalammar.github.io/visualizing-neural-machine-translation-mechanics-of-seq2seq-models-with-attention/

[5] https://www.youtube.com/watch?v=Fexsxz8nLVw

Fig. 11: Sentence Source

--

--

Chompakorn C
AIResearch.in.th

Research Assistant at VISTEC-Depa AIResearch Institute