Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Deep Learning with PyTorch

Open In Colab Python PyTorch Jupyter

โน้ตบุ๊กบันทึกการเรียนรู้ (learning notes) พื้นฐาน Deep Learning ด้วย PyTorch ตั้งแต่การสร้างและจัดการ Tensor ไปจนถึงการสร้าง Neural Network ด้วย torch.nn.Module และการคำนวณ Gradient อัตโนมัติด้วย Autograd เนื้อหาอธิบายเป็นภาษาไทยปนอังกฤษ พร้อมโค้ดตัวอย่างที่รันได้จริงทีละเซลล์บน Google Colab / Jupyter Notebook

สารบัญ

ภาพรวม

โปรเจกต์นี้เป็นโน้ตบุ๊กเดี่ยว (DeepLearningWithPyTorch.ipynb) ที่รวบรวมพื้นฐานของ Deep Learning โดยเน้นการ "ลงมือรันโค้ดจริง" ทีละแนวคิด แทนที่จะอธิบายเชิงทฤษฎีล้วน ๆ เนื้อหาถูกจัดวางในลักษณะบทเรียนต่อเนื่องและมีการ Recap (ทบทวน) เป็นระยะ เพื่อค่อย ๆ ปูพื้นฐานจาก Tensor พื้นฐาน ไปจนถึงการสร้างโครงข่ายประสาทเทียมและกลไกการเรียนรู้ (Autograd + Gradient Descent) ด้วยตัวเอง

เนื้อหา / Workflow การเรียนรู้

เนื้อหาในโน้ตบุ๊กถูกจัดลำดับเป็น 4 ช่วงหลัก ไล่ตามความยากที่เพิ่มขึ้น:

1. Introduction to Deep Learning — ปูพื้นฐาน Torch

  • Torch: การสร้าง Tensor และความสัมพันธ์กับ NumPy
  • torch.nn: แนะนำโมดูลสำหรับสร้าง Neural Network
  • torch.nn.functional: ฟังก์ชัน activation — ReLU, Sigmoid, Tanh
  • Softmax: การแปลงค่า output เป็นความน่าจะเป็น และการหา class ที่มีความน่าจะเป็นสูงสุด
  • Loss Function: MSE Loss และ Cross Entropy Loss

2. Introduction to PyTorch — เจาะลึก Tensor

  • Tensors: การสร้าง Tensor ในรูปแบบต่าง ๆ และคุณสมบัติของ Tensor
  • Tensor Operations:
    • Math operations: บวก ลบ คูณ หาร คูณเมทริกซ์ (matmul) ยกกำลัง
    • Reduction operations (sum, mean, max, min ฯลฯ)
    • Dimensions: การจัดการมิติของ Tensor (view, reshape)
    • Casting: แปลงระหว่าง NumPy array และ Tensor
    • Copy Tensor และ .detach()

3. Recap: Torch for Neural Network — สร้างโครงข่ายประสาทเทียม

  • ทบทวน torch.nn และ torch.nn.functional
  • ทบทวนการสร้าง Class ใน Python (class, inheritance) เพื่อปูพื้นก่อนสร้างโมเดล
  • torch.nn.Module: สร้างคลาส Net ของตัวเองโดยกำหนดเลเยอร์ (nn.Linear) และเขียนเมธอด forward()
  • การใส่ non-linear layer (เช่น ReLU) ระหว่างชั้นของโครงข่าย

4. Recap: Autograd & Neural Network — กลไกการเรียนรู้

  • การหาจุดต่ำสุดของฟังก์ชันด้วย Gradient Descent (คำนวณ gradient เอง)
  • Autograd: การคำนวณ gradient อัตโนมัติด้วย requires_grad=True, .backward(), .grad
  • การผสาน Autograd เข้ากับ Gradient Descent เพื่อ optimize ฟังก์ชันแบบวนซ้ำ (iterative update)
Tensor พื้นฐาน → Tensor Operations → Activation & Loss Function
        → torch.nn.Module (สร้างโมเดล) → Autograd → Gradient Descent

สิ่งที่จะได้เรียนรู้

  • เข้าใจโครงสร้างและการดำเนินการพื้นฐานของ Tensor ใน PyTorch
  • เข้าใจและเลือกใช้ฟังก์ชัน Activation (ReLU, Sigmoid, Tanh, Softmax) ได้อย่างเหมาะสม
  • เข้าใจ Loss Function พื้นฐาน (MSE, Cross Entropy) และการนำไปใช้
  • สร้างโครงข่ายประสาทเทียมของตัวเองด้วย torch.nn.Module
  • เข้าใจกลไกเบื้องหลังการเทรนโมเดล ได้แก่ Autograd (automatic differentiation) และ Gradient Descent

Tech Stack

หมวดหมู่ เทคโนโลยี
ภาษา Python 3.x
Deep Learning Framework PyTorch (torch, torch.nn, torch.nn.functional)
คำนวณเชิงตัวเลข NumPy
การแสดงผลกราฟ Matplotlib
สภาพแวดล้อมการรัน Jupyter Notebook / Google Colab

โครงสร้างโปรเจกต์

DeepLearning-with-pytorch/
├── DeepLearningWithPyTorch.ipynb   # โน้ตบุ๊กหลัก รวมเนื้อหาและโค้ดตัวอย่างทั้งหมด
└── README.md                       # เอกสารประกอบโปรเจกต์ (ไฟล์นี้)

วิธีใช้งาน

ตัวเลือกที่ 1: เปิดผ่าน Google Colab (แนะนำ)

กดปุ่ม Open In Colab ด้านบนของ README เพื่อเปิดโน้ตบุ๊กและรันโค้ดได้ทันทีโดยไม่ต้องติดตั้งอะไรเพิ่ม

ตัวเลือกที่ 2: รันแบบ Local

  1. Clone โปรเจกต์ (หรือดาวน์โหลดไฟล์ .ipynb)
  2. ติดตั้งไลบรารีที่จำเป็น
    pip install torch numpy matplotlib notebook
  3. เปิด Jupyter Notebook แล้วรันไฟล์
    jupyter notebook DeepLearningWithPyTorch.ipynb
  4. รันเซลล์ตามลำดับตั้งแต่ต้นจนจบ เพื่อให้ตัวแปรและโมเดลถูกสร้างขึ้นตามลำดับ

ตัวอย่างโค้ดที่น่าสนใจ

การสร้างโครงข่ายประสาทเทียมด้วย torch.nn.Module:

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(100, 50)
        self.fc2 = nn.Linear(50, 10)

    def forward(self, x):
        x = self.fc1(x)
        x = F.relu(x)
        x = self.fc2(x)
        return x

net = Net()

Gradient Descent ด้วย Autograd:

x = torch.tensor(10., dtype=torch.float, requires_grad=True)
alpha = 0.1

for i in range(1000):
    f = x ** 2 - 4 * x + 4          # ฟังก์ชันที่ต้องการหาจุดต่ำสุด
    f.backward(retain_graph=True)   # คำนวณ gradient ด้วย autograd
    x.data.sub_(alpha * x.grad)     # อัปเดตค่า x
    x.grad.data.zero_()             # รีเซ็ต gradient ก่อนรอบถัดไป

หมายเหตุ

  • โน้ตบุ๊กนี้เป็นบันทึกการเรียนรู้ส่วนตัว (personal learning notes) เพื่อฝึกฝนพื้นฐาน PyTorch จึงเน้นความเข้าใจแนวคิดผ่านตัวอย่างเล็ก ๆ มากกว่าการเทรนโมเดลบนชุดข้อมูลจริงขนาดใหญ่
  • คำอธิบายในโน้ตบุ๊กเขียนเป็นภาษาไทยปนอังกฤษเพื่อให้เข้าใจง่ายสำหรับผู้เริ่มต้น

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages