Pairode Jaroensri

Pairode Jaroensri

Data Scientist at Big Data Institute (Public Organization), BDI

บทความทั้งหมด

All Articles

Author Category Post Filter
Transformer – โครงสร้างพื้นฐานของ Generative AI
ในปัจจุบันไม่สามารถปฏิเสธได้เลยว่ากระแสของ Generative AI กำลังมาแรงมาก ๆ ไม่ว่าจะเป็นโมเดลสำหรับสร้างรูปภาพเช่น Stable Diffusion หรือ Dall-E หรือว่าโมเดลทางภาษาที่สามารถรับคำสั่งหรือคำถามของผู้ใช้ (prompt) แล้วสามารถสร้างคำตอบขึ้นมาตอบโต้กับผู้ใช้ได้ราวกับเป็นคนจริง ๆ ตัวอย่างเช่น ChatGPT ของ OpenAI หรือว่า Gemini ของ Google โมเดลต่าง ๆ เหล่านี้มีโครงสร้างและหลักการที่ซับซ้อน แต่ส่วนใหญ่เป็นการพัฒนาต่อจากโมเดล Transformer ทั้งนั้น แม้แต่โมเดล ChatGPT ซึ่งย่อมาจาก Generative Pretrained Transformer ก็เป็นการพัฒนาต่อจากโมเดล Transformer ดังนั้นในบทความนี้เราจะมาดูกันว่า Transformer นั้นมีหลักการทำงานอย่างไร Transformer เป็นโมเดลที่พัฒนาโดยบริษัท Google ถูกนำเสนอครั้งแรกในปี 2017 ในบทความงานวิจัยชื่อ Attention is All You Need โมเดล Transformer ทำงานโดยการรับ input sequence ที่มีลักษณะเป็นลำดับที่มีความยาว เช่นข้อความต่าง ๆ จากนั้น โมเดลจะหาความสัมพันธ์ระหว่างตัวแปรต่าง ๆ ที่ได้รับเข้ามาและใช้ความสัมพันธ์นั้นในการสร้าง output sequence ออกมาเป็นคำตอบ โดยคำตอบจะมีลักษณะเป็นลำดับที่มีความยาวเช่นกัน ตัวอย่างเช่น input อาจเป็นคำถามว่าท้องฟ้ามีสีอะไร โมเดลที่ผ่านการฝึกมาแล้วจะสามารถหาความสัมพันธ์ระหว่างคำว่าท้องฟ้า สี และสีฟ้า จากนั้นจึงตอบกลับมาว่าท้องฟ้ามีสีฟ้า ความจริงแล้วโมเดลที่ทำงานโดยการรับ input sequence เข้ามาและสร้าง output sequence โดยใช้บริบทความสัมพันธ์จาก input sequence นั้นมีมาก่อนหน้านี้แล้ว เช่นโมเดล seq2seq โครงสร้างของโมเดล seq2seq ประกอบไปด้วยส่วนประกอบหลัก 2 ส่วนคือ Encoder และ Decoder โดย Encoder มีหน้าที่รับ input sequence เข้ามา จากนั้นแปลง input sequence ให้กลายเป็นตัวแปรทางคณิตศาสตร์ที่โมเดลสามารถเข้าใจและทำการคำนวณต่อไปได้ จากนั้น Decoder จึงนำตัวแปรต่าง ๆ เหล่านี้มาทำการคำนวณหาความสัมพันธ์และสร้าง output sequence ออกมาเป็นคำตอบ โดยโครงสร้าง Encoder-Decoder นี้จะเห็นได้ในรูปที่ 2             ในรูปที่ 2 จะเห็นได้ว่าส่วน Encoder ทำการแปลงประโยค “She is eating a green apple” ให้กลายเป็น Context Vector จากนั้นจึงส่ง Context Vector ให้กับ Decoder เพื่อใช้ในการสร้าง Output sequence ออกมาเป็นภาษาจีน การทำงานแบบนี้มีข้อจำกัดคือ Encoder จะต้องแปลงข้อมูลทั้งหมดที่อยู่ใน input sequence ให้กลายเป็นเวกเตอร์เพียงแค่เวกเตอร์เดียวเท่านั้น และนอกจากนี้ คำท้าย ๆ ของ input sequence ยังมีผลต่อเวกเตอร์มากกว่าคำต้น ๆ อีกด้วย เมื่อคำต้น ๆ ไม่ค่อยมีผลกับเวกเตอร์ ส่วน Decoder จึงไม่ค่อยให้ความสำคัญ ทำให้ประสิทธิภาพของโมเดลด้อยลงเมื่อ input sequence มีความยาวมาก โมเดล Transformer จึงมีการคิดค้นการคำนวณแบบใหม่ขึ้นมาแก้ปัญหานี้ โดยเรียกการคำนวณใหม่นี้ว่า Attention Mechanism จากรูปโครงสร้างโมเดล Transformer จะเห็นได้ว่า Transformer ประกอบไปด้วย 2 ส่วนคือ Encoder ทางซ้าย และ Decoder ทางขวา เช่นเดียวกับโมเดล seq2seq แต่มีสิ่งที่เพิ่มมาคือกลไก Attention หรือในรูปคือ Multi-Head Attention กลไกนี้จะคำนวณค่าความสัมพันธ์ระหว่างคำใน output sequence ทุกคำ กับคำใน input sequence ทุกคำ จึงทำให้โมเดลสามารถรับข้อมูลจาก input sequence ทั้งหมดได้พร้อมกัน โมเดล Transformer จึงสามารถใช้ข้อมูลทั้งหมดพร้อมกันในการคำนวณได้ โมเดลจึงมีประสิทธิภาพในการประมวลผลข้อความที่มีความยาว เช่นหน้าเว็บไซต์ทั้งหน้า หรือในกรณีของโมเดลที่มีการสนทนาตอบโต้กับผู้ใช้ โมเดลจะสามารถใช้ข้อความก่อนหน้าเป็นบริบทในการสร้างคำตอบต่อ ๆ ไปได้               ตัวอย่างในรูปที่ 4 เป็นตัวอย่างการแปลภาษาอังกฤษเป็นฝรั่งเศส แถบสีที่อยู่ใต้คำภาษาฝรั่งเศสคือค่า Attention ที่บ่งบอกว่าคำนั้นมีความสัมพันธ์กับคำภาษาอังกฤษใน input sequence มากแค่ไหน ยิ่งสีเข้มก็ยิ่งมีความสัมพันธ์มาก โดยจะเห็นได้ว่า “Comment” มีความสัมพันธ์กับ “How”, “se” มีความสัมพันธ์กับ “How” และ “was”, “passe” มีความสัมพันธ์กับ “was”, “ta” มีความสัมพันธ์กับ “your”, และ “journée” มีความสัมพันธ์กับ “day”             กลไก Attention ประกอบไปด้วยส่วนประกอบ 3 ส่วนได้แก่ Query (Q) เป็นตัวแทนของบริบท หรือสิ่งที่โมเดลกำลังโฟกัสอยู่ในปัจจุบัน Key (K) เป็นตัวแทนส่วนประกอบของข้อมูลที่โมเดลรับเข้ามา และ Value (V) เป็นตัวแทนน้ำหนักที่โมเดลให้กับส่วนประกอบต่าง ๆ ของข้อมูลที่ได้รับเข้ามา โดยโมเดลจะนำทั้งสามส่วนประกอบ Q, K, V มาทำการคำนวณทางคณิตศาสตร์ตามรูปที่ 5 และได้ผลลัพธ์ออกมาเป็นค่าความสัมพันธ์ระหว่าง Q สิ่งที่โมเดลกำลังโฟกัสอยู่ และ K ส่วนประกอบต่าง ๆ ของข้อมูลที่รับเข้ามา สำหรับผู้ที่สนใจรายละเอียดการคำนวณสามารถอ่านต่อได้ในบทความงานวิจัยต้นฉบับชื่อ Attention Is All You Need หรือบทความ Wikipedia ชื่อ Attention (machine learning) นอกจากการคำนวณ Attention แล้ว ในโมเดล Transformer ได้พัฒนาต่อเป็นกลไก Multi-Head Attention เป็นการคำนวณ Attention หลายครั้งโดยใช้ค่าน้ำหนักของโมเดลที่ต่างกัน ทำให้โมเดลสามารถค้นพบความสัมพันธ์ระหว่างแต่ละคำใน input sequence ได้ในหลายมุมมอง...
17 June 2024
ประวัติและหลักการของเครือข่ายประสาทเทียม (Artificial Neural Networks - ANNs)
ประวัติและหลักการทำงานของ Neural Network เริ่มจากโมเดล Perceptron จากนั้นจึงเพิ่ม layer เข้าไปให้กลายเป็น Multilayer Perceptron โดยสามารถเชื่อมต่อ layer ได้หลายวิธี แต่ละวิธีมีชื่อเรียกต่างกันจึงเรียกรวม ๆ ว่า Artificial Neural Network
3 February 2023
PDPA Icon

We use cookies to optimize your browsing experience and improve our website’s performance. Learn more at our Privacy Policy and adjust your cookie settings at Settings

Privacy Preferences

You can choose your cookie settings by turning on/off each type of cookie as needed, except for necessary cookies.

Accept all
Manage Consent Preferences
  • Strictly Necessary Cookies
    Always Active

    This type of cookie is essential for providing services on the website of the Personal Data Protection Committee Office, allowing you to access various parts of the site. It also helps remember information you have previously provided through the website. Disabling this type of cookie will result in your inability to use key services of the Personal Data Protection Committee Office that require cookies to function.
    Cookies Details

  • Performance Cookies

    This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

  • Functional Cookies

    This type of cookie enables the Big Data Institute (Public Organization)’s website to remember the choices you have made and deliver enhanced features and content tailored to your usage. For example, it can remember your username or changes you have made to font sizes or other customizable settings on the page. Disabling these cookies may result in the website not functioning properly.

  • Targeting Cookies

    "This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

Save settings
This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.