ใน ep ที่แล้วเราได้ Neural Network และ Training Loop ที่ทำงานได้ดีพอสมควร มีการวัดผล Metrics กับข้อมูลใน Validation Set เพื่อให้แน่ใจว่าโมเดลทำงานได้ถูกต้องกับข้อมูลที่ไม่เคยเห็นมาก่อน

แต่โค้ด Training Loop ของเรายังมีความซับซ้อนเกินไป ใช้ Parameter จากภายนอกถึง 6 ตัว ซึ่งมากเกินไป ทำให้ยากต่อการต่อยอดเทรนในอัลกอริทึมที่ซับซ้อนยิ่งขึ้น แล้วเราจะแก้ไขอย่างไร

Refactor DataBunch

ขั้นแรก เราจะแก้ไขด้วยการ Refactor โค้ดส่วน Parameter ของ Training Loop สร้าง Wrapper Class ชื่อ DataBunch ห่อหุ้มข้อมูล Training Set DataLoader และ Validation Set DataLoader ที่เป็น Class เดียวกัน ทำงานใกล้เคียงกันเอาไว้

Refactor Learner

ขั้นต่อมา เราจะรวบ Parameter ของ Training Loop ที่เหลือเข้าไว้ด้วยกัน เป็น Class ใหม่ที่ชื่อว่า Learner เราจะได้เก็บ State ของการเทรน และโมเดล

เพื่อที่เราจะพาส Learner ไปยังส่วนต่าง ๆ ในอนาคต ที่จะมีการจัดการแก้ไข State เหล่านี้ ด้วยอัลกอริทึมการเทรน Neural Network ที่ซับซ้อนมากขึ้น เช่น BatchNorm, Dropout, Learning Rate Annealing, etc.

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

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.