ใน ep นี้เราจะมาสร้าง Artificial Neural Network แบบ Recurrent Neural Network (RNN) กันแต่ต้น ด้วยภาษา Python เริ่มตั้งแต่ ปัญหาว่าทำไมต้องมี RNN พื้นฐานแนวคิด ศึกษาการทำงานของ RNN แบบง่าย ข้อดี ข้อเสีย แล้วพัฒนาโมเดล ปรับปรุง แก้ไขข้อจำกัดของโมเดล RNN แต่ละแบบ ไปจนถึง Gated Recurrent Unit (GRU)

Recurrent Neural Network (RNN) คืออะไร

A diagram for a one-unit recurrent neural network (RNN). From bottom to top : input state, hidden state, output state. U, V, W are the weights of the network. Compressed diagram on the left and the unfold version of it on the right. Credit https://commons.wikimedia.org/wiki/File:Recurrent_neural_network_unfold.svg
A diagram for a one-unit recurrent neural network (RNN). From bottom to top : input state, hidden state, output state. U, V, W are the weights of the network. Compressed diagram on the left and the unfold version of it on the right. Credit https://commons.wikimedia.org/wiki/File:Recurrent_neural_network_unfold.svg

เนื่องจากปกติแล้ว เราจะมอง Artificial Neural Network เป็นฟังก์ชันที่รับ Input ประมวลผลออกมาเป็น Output โดยโมเดลจะมอง Input แต่ละตัวแยกกันชัดเจน ไม่ขึ้นต่อกัน Input ที่รับเข้าไปจะเข้ามาเรียงลำดับอย่างไรก็ได้ โมเดลจะไม่สนใจ ประมวลผลให้ Output ออกมาเหมือนเดิม ตาม Input 1 ตัวอย่างนั้น

ซึ่ง Artificial Neural Network แบบปกตินี้ จะมีปัญหากับข้อมูลที่เป็นลำดับ เช่น NLP, ข้อความ (ลำดับตัวอักษร), เสียง (ลำดับแรงดันอากาศ), วิดีโอ (ลำดับของภาพและเสียง) และข้อมูลที่เป็นแบบ Time Series ต่าง ๆ, etc.

Recurrent Neural Network (RNN) คือ Artificial Neural Network แบบหนึ่งที่ออกแบบมาแก้ปัญหาสำหรับงานที่ข้อมูลมีลำดับ Sequence โดยใช้หลักการ Feed สถานะภายในของโมเดล กลับมาเป็น Input ใหม่ คู่กับ Input ปกติ เรียกว่า Hidden State, Internal State, Memory ช่วยให้โมเดลรู้จำ Pattern ของลำดับ Input Sequence ได้

RNN มีการพัฒนาต่อยอดไปอีกหลาย Variation ที่เป็นที่นิยมได้แก่ Long Short Term Memory (LSTM), Gated Recurrent Unit (GRU)

Gated Recurrent Unit (GRU) คืออะไร

A diagram for a one-unit Gated Recurrent Unit (GRU). From bottom to top : input state, hidden state, output state. Gates are sigmoïds or hyperbolic tangents. Other operators : element-wise plus and multiplication. Weights are not displayed. Credit https://en.m.wikipedia.org/wiki/File:Gated_Recurrent_Unit.svg
A diagram for a one-unit Gated Recurrent Unit (GRU). From bottom to top : input state, hidden state, output state. Gates are sigmoïds or hyperbolic tangents. Other operators : element-wise plus and multiplication. Weights are not displayed. Credit https://en.m.wikipedia.org/wiki/File:Gated_Recurrent_Unit.svg

แต่ RNN ก็มีปัญหา Vanishing Gradient / Exploding Gradient และประสิทธิภาพจะแย่ลงถ้าเจอกับ Sequence ยาว ๆ ทำให้มีการออกแบบ GRU ขึ้นมาแก้ปัญหาเหล่านี้

Gated Recurrent Units (GRU) เป็นกลไลปิดเปิดการอัพเดทสถานะภายใน Recurrent Neural Network ที่คล้ายกับ Long Short-Term Memory (LSTM) ที่จะมี Forget Gate แต่มี Parameter น้อยกว่า LSTM เนื่องจากไม่มี Output Gate

GRU มีประสิทธิภาพใกล้เคียงกับ LSTM ในหลาย ๆ งาน แต่เนื่องจาก Parameter น้อยกว่าทำให้เทรนได้ง่ายกว่า เร็วกว่า และในบางงานที่ DataSet มีขนาดเล็ก พบว่า GRU ประสิทธิภาพดีกว่า

Human Numbers Dataset

ใน ep นี้ เราจะสร้างโมเดล RNN มาเรียนรู้ Human Numbers Dataset และ Predict ข้อความตัวเลขภาษาอังกฤษ ตั้งแต่ 1-9999 ดังด้านล่าง

one  
two  
three  
four  
five  
six  
seven  
eight  
nine  
ten 
....
nine thousand nine hundred ninety  
nine thousand nine hundred ninety one  
nine thousand nine hundred ninety two  
nine thousand nine hundred ninety three  
nine thousand nine hundred ninety four  
nine thousand nine hundred ninety five  
nine thousand nine hundred ninety six  
nine thousand nine hundred ninety seven  
nine thousand nine hundred ninety eight  
nine thousand nine hundred ninety nine 

เราจะเริ่มต้นพัฒนาสถาปัตยกรรมของโมเดล RNN แบบง่าย ไปจนถึงต่อยอดด้วย โมเดลที่ซับซ้อน เช่น GRU 2 Layer เปรียบเทียบประสิทธิภาพ Before / After ของการ Design ออกแบบ Model Architecture แต่ละแบบ

เรามาเริ่มกันเลยดีกว่า

Open In Colab

แชร์ให้เพื่อน:

Surapong Kanoktipsatharporn on FacebookSurapong Kanoktipsatharporn on LinkedinSurapong Kanoktipsatharporn on Rss
Surapong Kanoktipsatharporn
Solutions Architect at Bua Labs
The ultimate test of your knowledge is your capacity to convey it to another.

Published by Surapong Kanoktipsatharporn

The ultimate test of your knowledge is your capacity to convey it to another.