ในการเรียนรู้ Neural Network เราจะพบเจอโค้ดที่ใช้ List, Vector, NumPy Array ไปจนถึง High-Order Tensor หมายถึง Array ที่มีมากกว่า 2 มิติขึ้นไป เช่น 3 มิติ 4 มิติ หรือ 5 มิติ จนเป็นเรื่องธรรมดา ใน ep นี้เราจะมาเรียนรู้การใช้งาน Tensor ทำความเข้าใจ element-wise, broadcasting operations

Vector และ List คืออะไร

Vector (เวกเตอร์) และ List (ลิสต์) คือ ภาชนะบรรจุข้อมูลที่มีขนาดเท่ากัน ชนิดเดียวกัน ที่มี 1 มิติ เช่น [1, 2, 3, 4]

NumPy Array และ Matrix คืออะไร

NumPy Array (นัมไพ อาเรย์) และ Matrix (เมตริกซ์) คือ ภาชนะบรรจุข้อมูลที่มีขนาดเท่ากัน ชนิดเดียวกัน ที่มี 2 มิติ เช่น
[[1, 2, 3],
[4, 5, 6] ,
[7, 8, 9]]

Tensor คืออะไร

Tensor (เทนเซอร์) คือ ภาชนะบรรจุข้อมูลที่มีขนาดเท่ากัน ชนิดเดียวกัน ที่มีกี่มิติก็ได้ เช่น รูปสี ความละเอียด 224 x 224 Pixel จำนวน 10 รูป จะมีข้อมูล 4 มิติ ดังนี้ (10, 3, 224, 224) และสมมติเรามีข้อมูล 10 รูปแบบนี้ จาก 12 เดือน ก็จะเป็น 5 มิติ (12, 10, 3, 224, 224)

เราสามารถออกแบบมิติข้อมูลเพื่อใช้งานได้อย่างอิสระ

Scheme of construction of hypercube up to 4D 0D is point 0D -> 1D : From point to segment 1D -> 2D : From segment to square 2D -> 3D : From square to cube 3D -> 4D : From cube to tesseract. Credit: https://en.wikipedia.org/wiki/Hypercube_internetwork_topology
Scheme of construction of hypercube up to 4D 0D is point 0D -> 1D : From point to segment 1D -> 2D : From segment to square 2D -> 3D : From square to cube 3D -> 4D : From cube to tesseract. Credit: https://en.wikipedia.org/wiki/Hypercube_internetwork_topology

หมายเหตุ

  1. NumPy Array จริง ๆ คือ N-Dimension (ndarray) ก็สามารถใช้บรรจุข้อมูล กี่มิติก็ได้เช่นกัน แต่นิยมใช้ 1-2 มิติ
  2. 3 ในรูปสี คือ 3 Channels RGB
  3. การเรียงมิติไหนก่อนหลัง ขึ้นกับแต่ละ API
  4. Rank Number หมายถึง จำนวนมิติ ของ Tensor เช่น ตัวอย่างด้านบน (10, 3, 224, 224) จะเรียกว่า Rank 4 Tensor

ข้อดีของ Tensor เปรียบเทียบกับ NumPy Array

Tensor นั้นมีข้อดีหลายอย่างกว่า NumPy Array คือ สามารถใช้ GPU และรองรับ Autograd สามารถคำนวน Diff หา Gradient ให้อัตโนมัติ ทำให้เหมาะที่จะนำมาใช้สร้าง Deep Neural Network ในปัจจุบัน ที่เทรนโมเดลด้วยอัลกอริทึม Gradient Descent และ Backpropagation

NVIDIA Tesla K40 GPU Accelerator. Credit https://nvidianews.nvidia.com/news/nvidia-launches-world-s-fastest-accelerator-for-supercomputing-and-big-data-analytics
NVIDIA Tesla K40 GPU Accelerator. Credit https://nvidianews.nvidia.com/news/nvidia-launches-world-s-fastest-accelerator-for-supercomputing-and-big-data-analytics

และเมื่อมี GPU แทนที่เราจะเขียนโปรแกรม วน Loop ทำ Operations ทีละ Element เราจึงต้องปรับ Mindset ออกแบบด้วยแนวคิด Vectorization คือ ทำทุกอย่างให้เป็น Vector, Array หรือ Tensor แล้วทำ Operation นั้น ๆ พร้อม ๆ กันทีเดียวหมด ขนานกันไปเลย ด้วย GPU ซึ่งจะช่วยเพิ่มความเร็วขึ้นกว่าวน Loop ทำทีละอัน หลายร้อย หลายพันเท่า

เรามาเริ่มกันเลย

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.