ใน ep นี้ เราจะมาเรียนรู้กันว่า Loss Function คืออะไร Loss Function ทำงานอย่างไร Loss Function เกี่ยวอะไรกับ Machine Learning Algorithm

Loss Function คืออะไร

Loss Function, Cost Function หรือ Error Function คือ ฟังก์ชันคณิตศาสตร์ที่ Map ค่า Event จาก Input หลาย ๆ ตัว ออกมาเป็นตัวเลขจำนวนจริง 1 ตัว เพื่อใช้แทน Cost ของ Event นั้น

งาน Optimization คือ การหาวิธีทำให้ค่า Output ของ Loss Function ออกมาน้อยที่สุด (Minimize)

Gradient descent with small (top) and large (bottom) learning rates. Source: Andrew Ng’s Machine Learning course on Coursera
Gradient descent ด้วย Learning Rate น้อยเกิน (รูปบน) และมากเกิน (รูปล่าง) เครดิต: Andrew Ng Machine Learning course on Coursera

Objective Function คือ Loss Function หรือไม่ก็ Negative ของ Loss Function ขึ้นอยู่กับงาน บางทีอาจเรียกว่า Reward Function, Profit Function, Utility Function, Fitness Function, etc. ในบางงานก็เป็นการหาวิธีทำให้ค่า Output มากที่สุด (Maximize)

Loss Function ทำงานอย่างไร ใน Machine Learning

ไอเดียของ Loss Function คือ เราต้องการตัวชี้วัด เป็นตัวเลขค่าเดียว ที่บอกว่า โมเดล Machine Learning ของเราทำงานได้ดีแค่ไหน โดยเปรียบเทียบ Output ของ โมเดล คือ yhat (เส้นสีน้ำเงิน) กับข้อมูล Output ตัวอย่าง y (จุดแดง) ค่าทั้งสองอย่าง ณ Input x เดียวกัน (Deviation เส้นสีเขียว)

Illustration of least squares fitting. The data (red dots) are at co-ordinates (1,6), (2,5), (3,7) and (4,10). A linear approximation is obtained using least-squares estimation (blue line). Credit: https://commons.wikimedia.org/wiki/File:Linear_least_squares_example2.svg
Illustration of least squares fitting. The data (red dots) are at co-ordinates (1,6), (2,5), (3,7) and (4,10). A linear approximation is obtained using least-squares estimation (blue line). Credit: https://commons.wikimedia.org/wiki/File:Linear_least_squares_example2.svg

(yhat, y) คือ ข้อมูลที่เราจะนำมาใช้คำนวนผ่าน Loss Function ว่าโมเดลทำงานผิดพลาดมากน้อยแค่ไหน ถ้า Loss = 0 คือ ไม่ผิดพลาดเลย

กราฟด้านล่าง แสดงพื้นผิว Loss โดยทั่วไปแล้ว เราต้องการเทรนโมเดล ปรับ Parameter ต่าง ๆ ในโมเดล ให้ Gradient Descent ลงไปยังจุดที่ Loss ใกล้ 0 มากที่สุด ดังรูป

gradient descent Credit: Lecture 2 | Machine Learning (Stanford) https://www.youtube.com/watch?v=5u4G23_OohI
gradient descent Credit: Lecture 2 | Machine Learning (Stanford) https://www.youtube.com/watch?v=5u4G23_OohI

โดยการเทรน Deep Neural Network ด้วยอัลกอริทึม Gradient Descent จะหา Gradient ของ Loss Function ขึ้นกับ Parameter ต่าง ๆ ในโมเดล แล้วอัพเดทด้วย Weight, Bias นั้นด้วย อัลกอริทึม เช่น SGD

Loss Funtion ที่นิยมใช้

  • Loss Funtion ที่นิยมในงาน Classification เช่น Cross Entropy Loss
  • Loss Function ที่นิยมในงาน Regression ได้แก่ L1 Loss, L2 Loss
  • และถ้างานของเราซับซ้อนมากขึ้น เราสามารถนำ Loss Function หลาย ๆ อันมาผสมกันสร้างเป็น Loss Function ใหม่ที่เหมาะกับงานของเราได้ เช่น งาน Object Detection ที่มีทั้งงาน Regression และ Classification ในตัว

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

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.