Neural Networks คืออะไร? เครือข่ายประสาทเทียมอธิบายง่ายๆ
เบื้องหลังความสามารถของ ChatGPT, DALL-E, และ AI สมัยใหม่ทั้งหมด คือเทคโนโลยีที่เรียกว่า Neural Networks หรือ เครือข่ายประสาทเทียม ที่ได้แรงบันดาลใจจากการทำงานของสมองมนุษย์
Neural Networks คืออะไร?
Neural Network คือโมเดลทางคณิตศาสตร์ที่จำลองการทำงานของเซลล์ประสาทในสมอง เพื่อให้คอมพิวเตอร์สามารถเรียนรู้จากข้อมูลและตัดสินใจได้
เปรียบเทียบกับสมองมนุษย์
สมองมนุษย์ Neural Network
──────────── ──────────────
Neuron (เซลล์ประสาท) → Node (โหนด)
Synapse (ไซแนปส์) → Weight (น้ำหนัก)
สัญญาณไฟฟ้า → ตัวเลข (Numbers)
การเรียนรู้ → Training
โครงสร้างพื้นฐาน
1. Neuron (โหนด)
หน่วยประมวลผลพื้นฐาน ทำหน้าที่:
- รับ Input
- คำนวณผลรวม
- ส่งต่อ Output
x1 ──┐
│
x2 ──┼──→ [Σ] ──→ [f] ──→ Output
│
x3 ──┘
x = Input
Σ = ผลรวมถ่วงน้ำหนัก
f = Activation Function
2. Layer (ชั้น)
กลุ่มของ Neurons ที่ทำงานในระดับเดียวกัน:
┌─────────────────────────────────────────────┐
│ Input Hidden Hidden Output │
│ Layer Layer 1 Layer 2 Layer │
│ │
│ (○) (○) (○) (○) │
│ (○) →→→ (○) →→→ (○) →→→ (○) │
│ (○) (○) (○) (○) │
│ (○) (○) (○) │
│ │
└─────────────────────────────────────────────┘
3. Weight และ Bias
- Weight (น้ำหนัก): ความสำคัญของแต่ละ connection
- Bias: ค่าปรับแต่งเพิ่มเติม
Output = f(w1*x1 + w2*x2 + w3*x3 + bias)
w = weight
x = input
f = activation function
การทำงานของ Neural Network
Forward Propagation
ข้อมูลไหลจาก Input ไปยัง Output:
ตัวอย่าง: จำแนกภาพแมว
Input: [ค่า pixel 784 ตัว]
↓
Hidden 1: [128 nodes]
↓
Hidden 2: [64 nodes]
↓
Output: [แมว: 0.95, สุนัข: 0.05]
Backpropagation
การเรียนรู้จากความผิดพลาด:
1. คำนวณ Error (ความผิดพลาด)
Error = คำตอบจริง - คำตอบที่ทำนาย
2. ส่ง Error ย้อนกลับ
Output → Hidden 2 → Hidden 1 → Input
3. ปรับ Weights
weight_ใหม่ = weight_เดิม - (learning_rate × gradient)
4. ทำซ้ำจนกว่า Error จะน้อยพอ
Activation Functions
ฟังก์ชันที่ตัดสินว่า Neuron จะ "fire" หรือไม่:
ReLU (Rectified Linear Unit)
f(x) = max(0, x)
ถ้า x > 0: ส่งค่า x ต่อไป
ถ้า x ≤ 0: ส่ง 0
ข้อดี: คำนวณเร็ว, ไม่มีปัญหา vanishing gradient
Sigmoid
f(x) = 1 / (1 + e^(-x))
Output: 0 ถึง 1
ใช้งาน: Binary classification (ใช่/ไม่ใช่)
Softmax
f(xi) = e^xi / Σ(e^xj)
Output: ความน่าจะเป็นที่รวมกันได้ 1
ใช้งาน: Multi-class classification
ตัวอย่าง:
[แมว: 0.7, สุนัข: 0.2, นก: 0.1] = 1.0
Tanh
f(x) = (e^x - e^(-x)) / (e^x + e^(-x))
Output: -1 ถึง 1
ใช้งาน: Hidden layers
ประเภทของ Neural Networks
1. Feedforward Neural Network (FNN)
โครงสร้างพื้นฐานที่สุด ข้อมูลไหลทางเดียว:
Input → Hidden → Output
ใช้งาน: งาน Classification และ Regression ทั่วไป
2. Convolutional Neural Network (CNN)
เชี่ยวชาญด้านภาพ:
ภาพ → [Conv] → [Pool] → [Conv] → [Pool] → [FC] → Output
ใช้งาน:
- Image Classification
- Object Detection
- Face Recognition
3. Recurrent Neural Network (RNN)
จำข้อมูลลำดับ มี "ความจำ":
x1 → [RNN] → h1
↓
x2 → [RNN] → h2
↓
x3 → [RNN] → h3 → Output
ใช้งาน:
- การแปลภาษา
- Speech Recognition
- Time Series
4. Long Short-Term Memory (LSTM)
RNN ที่จำได้นานขึ้น:
มี Gates ควบคุม:
- Forget Gate: ลืมอะไร
- Input Gate: จำอะไรใหม่
- Output Gate: ส่งอะไรออก
ใช้งาน:
- Text Generation
- Music Composition
- Stock Prediction
5. Transformer
สถาปัตยกรรมใหม่ที่อยู่เบื้องหลัง GPT, BERT:
ใช้ Attention Mechanism:
"The cat sat on the mat"
↑
Attention จะโฟกัสที่คำที่เกี่ยวข้อง
ใช้งาน:
- ChatGPT, Claude
- การแปลภาษา
- Summarization
6. Generative Adversarial Network (GAN)
สองโมเดลแข่งกัน:
Generator: สร้างภาพปลอม
↓
Discriminator: แยกของจริง/ปลอม
↓
Generator พัฒนาขึ้นเรื่อยๆ
ใช้งาน:
- สร้างภาพใบหน้า
- Image Enhancement
- Style Transfer
ตัวอย่างโค้ด Python
ด้วย PyTorch
import torch
import torch.nn as nn
class SimpleNN(nn.Module):
def __init__(self):
super().__init__()
self.layer1 = nn.Linear(784, 128) # Input → Hidden
self.layer2 = nn.Linear(128, 64) # Hidden → Hidden
self.layer3 = nn.Linear(64, 10) # Hidden → Output
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.layer1(x))
x = self.relu(self.layer2(x))
x = self.layer3(x)
return x
# สร้างโมเดล
model = SimpleNN()
# Training Loop
for epoch in range(100):
output = model(input_data)
loss = criterion(output, labels)
optimizer.zero_grad()
loss.backward() # Backpropagation
optimizer.step() # Update weights
ด้วย TensorFlow/Keras
from tensorflow import keras
model = keras.Sequential([
keras.layers.Dense(128, activation='relu', input_shape=(784,)),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
model.fit(X_train, y_train, epochs=100)
การ Train Neural Network
1. เตรียมข้อมูล
# แบ่งข้อมูล
train_data: 80%
validation_data: 10%
test_data: 10%
# Normalize
X = (X - mean) / std
2. กำหนด Hyperparameters
learning_rate = 0.001 # อัตราการเรียนรู้
batch_size = 32 # จำนวนข้อมูลต่อรอบ
epochs = 100 # จำนวนรอบการ train
3. Training Loop
for แต่ละ epoch:
for แต่ละ batch:
1. Forward Pass
2. คำนวณ Loss
3. Backward Pass
4. Update Weights
ตรวจสอบกับ Validation Data
4. ประเมินผล
# Metrics
- Accuracy: ความแม่นยำ
- Precision: ความแม่นในการทำนาย Positive
- Recall: ครอบคลุม Positive จริงแค่ไหน
- F1-Score: ค่าเฉลี่ยของ Precision และ Recall
ปัญหาที่พบบ่อย
1. Overfitting
โมเดลจำข้อมูล train ดีเกินไป แต่ทำนายข้อมูลใหม่ไม่ได้
แก้ไข:
- เพิ่มข้อมูล
- Dropout
- Regularization
- Early Stopping
2. Underfitting
โมเดลเรียนรู้ไม่เพียงพอ
แก้ไข:
- เพิ่มจำนวน layers/neurons
- Train นานขึ้น
- ลด Regularization
3. Vanishing Gradient
Gradient เล็กมากจน weights ไม่เปลี่ยน
แก้ไข:
- ใช้ ReLU แทน Sigmoid
- Batch Normalization
- Skip Connections (ResNet)
4. Exploding Gradient
Gradient ใหญ่เกินไปจน weights ระเบิด
แก้ไข:
- Gradient Clipping
- Weight Initialization ที่ดี
- ลด Learning Rate
เริ่มต้นเรียน Neural Networks
ลำดับการเรียน
- คณิตศาสตร์พื้นฐาน: Linear Algebra, Calculus
- Python & NumPy: การจัดการ arrays
- Machine Learning พื้นฐาน: Regression, Classification
- Deep Learning Framework: PyTorch หรือ TensorFlow
- ทำโปรเจค: MNIST, CIFAR-10
แหล่งเรียนรู้
- 3Blue1Brown: Neural Networks series (YouTube)
- Fast.ai Course: ปฏิบัติจริง
- Andrew Ng's Deep Learning: Coursera
- PyTorch Tutorials: pytorch.org
สรุป
Neural Networks คือโมเดลที่จำลองการทำงานของสมอง:
- Neuron: หน่วยประมวลผล
- Layer: กลุ่มของ Neurons
- Weight: ความสำคัญของ connection
- Activation: ฟังก์ชันที่ตัดสินการ fire
ประเภทหลักๆ:
- FNN: พื้นฐาน
- CNN: สำหรับภาพ
- RNN/LSTM: สำหรับลำดับ
- Transformer: สำหรับภาษา (GPT, BERT)
อ่านเพิ่มเติม:
เขียนโดย
AI Unlocked Team
บทความอื่นๆ ที่น่าสนใจ
วิธีติดตั้ง FFmpeg บน Windows และ Mac: คู่มือฉบับสมบูรณ์
เรียนรู้วิธีติดตั้ง FFmpeg บน Windows และ macOS พร้อมการตั้งค่า PATH อย่างละเอียด เพื่อใช้งานโปรแกรมตัดต่อวิดีโอและเสียงระดับมืออาชีพ
สร้าง AI-Powered SaaS: จากไอเดียสู่ผลิตภัณฑ์
คู่มือครบวงจรในการสร้าง AI-Powered SaaS ตั้งแต่การวางแผน พัฒนา ไปจนถึง launch และ scale รวมถึง tech stack, pricing และ business model
AI Security: วิธีใช้ AI อย่างปลอดภัย
เรียนรู้แนวทางการใช้ AI อย่างปลอดภัย ครอบคลุม prompt injection, data privacy, API security และ best practices สำหรับองค์กร