if หูแหลม: return "แมว" elif หูตก: return "สุนัข" elif เห่า: return "สุนัข" # ... อีกนับพันกฎ
# ให้ตัวอย่างเยอะๆ model.fit(รูปภาพ, ป้ายกำกับ) # ระบบเรียนรู้กฎเอง! prediction = model.predict(รูปใหม่)
สมองมนุษย์ Dendrites (รับสัญญาณ) → Cell Body (ประมวลผล) → Axon (ส่งสัญญาณ) Perceptron Inputs (x₁, x₂) → Weighted Sum + Bias → Activation → Output
การตัดสินใจ = (อากาศดี × 0.7) + (มีเพื่อน × 0.5) + (-0.8) ถ้าผลรวม > 0 → ไปเที่ยว ✓ ถ้าผลรวม ≤ 0 → ไม่ไป ✗
y = f(w₁x₁ + w₂x₂ + b)
= (1 × 0.7) + (1 × 0.5) + (-0.8) = 0.7 + 0.5 - 0.8 = 0.4 > 0 → ไปเที่ยว ✓
= (0 × 0.7) + (1 × 0.5) + (-0.8) = 0 + 0.5 - 0.8 = -0.3 < 0 → ไม่ไป ✗
w_new = w_old + α × (y_true - y_pred) × x
w₁_new = 0.5 + 0.1 × 1 × 1 = 0.5 + 0.1 = 0.6 (ปรับเพิ่มขึ้น)
ความหมาย: "อากาศดี" สำคัญมากขึ้น (จาก 50% เป็น 60%)
Layer 1 สกัดคุณสมบัติพื้นฐาน
Layer 2 รวมคุณสมบัติ
Layer 1: h₁ = W₁x + b₁ Layer 2: h₂ = W₂h₁ + b₂ = W₂(W₁x + b₁) + b₂ = W₂W₁x + W₂b₁ + b₂ = Wx + b (ยังคงเป็น linear!)
ปัญหา ต่อ layers กี่ชั้นก็ยังเป็นเส้นตรง!
Layer 1: h₁ = W₁x + b₁ Layer 2: h₂ = W₂h₁ + b₂ = W₂(W₁x + b₁) + b₂ = W₂W₁x + W₂b₁ + b₂ = Wx + b → ยังเป็นเส้นตรง → XOR แก้ไม่ได้
Layer 1: h₁ = σ(W₁x + b₁) Layer 2: h₂ = σ(W₂h₁ + b₂) → ไม่สามารถย่อเป็น Wx + b ได้ → เกิดการโค้งของ decision boundary → XOR แก้ได้!
"MLP ที่มี hidden layer เพียง 1 ชั้น สามารถประมาณฟังก์ชันต่อเนื่องใดๆ ได้ ถ้ามี neurons เพียงพอ"
sigmoid 1 ตัว ___/‾‾‾ sigmoid 2 ตัว ___/‾‾\___ sigmoid หลายตัว ___/‾\/‾\___ (ประมาณเส้นโค้งได้)
ระวัง: neurons มาก = อาจ overfitting! มองอีกมุมได้ว่า Sigmoid ช่วยแปลง XOR จากปัญหา 2D ที่แก้ไม่ได้ → ปัญหาใน higher dimension ที่แก้ได้!
σ(x) = 1/(1+e^(-x))
σ(0.44) = 1/(1+e^(-0.44)) = 1/(1+0.64) = 0.61 ความหมาย Neuron นี้ "active" 61%
for neuron in neurons: for input in inputs: sum += weight * input add bias apply activation
ถ้ามี 100 นิวรอน และแต่ละนิวรอนมี 1,000 อินพุต จะมี 100 × 1,000 = 100,000 operations
Output = activation( Input × Weights + Bias )
1 operation ใหญ่ (GPU ทำพร้อมกันได้!)
[0.8, 0.3] × [[0.5, -0.3], [0.4, 0.7]] = [(0.8×0.5 + 0.3×0.4), (0.8×-0.3 + 0.3×0.7)] = [0.52, -0.03]
ถ้า z = f(g(x)) แล้ว dz/dx = dz/dg × dg/dx
ราคาขนม = ราคาแป้ง × 2 + 10 - ถ้าแป้งแพงขึ้น 1 บาท - ขนมแพงขึ้น 2 บาท - Gradient = 2 (ราคาขนมเปลี่ยนเร็วแต่ไหนเมื่อราคาแป้งเปลี่ยน)
"อัตราการเปลี่ยนแปลง (Gradient)" - บอกว่าถ้า input เปลี่ยน output จะเปลี่ยนเท่าไหร่
ความเร็ว = อนุพันธ์ของ ระยะทาง/เวลา - ขับรถเร็ว 60 km/hr (Gradient) - หมายความว่า ทุกๆ 1 ชั่วโมง ระยะทางเปลี่ยน 60 km
Loss → Output Layer → Hidden Layer → Input ตัวอย่าง 3 ชั้น Loss หรือ Error = (y_pred - y_true)² y_pred = σ(z) z = wx + b ∂ คือ อนุพันธ์ย่อย ∂Loss/∂w = ∂Loss/∂y_pred × ∂y_pred/∂z × ∂z/∂w = 2(y_pred - y_true) × σ'(z) × x
∂Loss/∂w = 2(0.7 - 1) × 0.21 × 0.8 = 2(-0.3) × 0.21 × 0.8 = -0.1008
ความหมาย ควรลด weight ลง 0.1008 × learning_rate
θ_new = θ_old - α∇J(θ)
Epoch 1: w = 5.0, gradient = 3.0, lr = 0.1 w_new = 5.0 - (0.1 × 3.0) = 4.7 Epoch 2: w = 4.7, gradient = 2.1, lr = 0.1 w_new = 4.7 - (0.1 × 2.1) = 4.49 Epoch 3: w = 4.49, gradient = 1.5, lr = 0.1 w_new = 4.49 - (0.1 × 1.5) = 4.34
→ ค่า weight ค่อยๆ ลดลงจนถึงจุดต่ำสุด (minimum)
Layer 1: y = W₁x Layer 2: z = W₂y = W₂(W₁x) = (W₂W₁)x Result: ยังคงเป็น linear!
Layer 1: y = σ(W₁x) Layer 2: z = σ(W₂σ(W₁x)) Result: Non-linear! ✓
σ'(x) = σ(x) × (1 - σ(x))
ดี Output เป็น probability แย่ Vanishing gradient ใช้ที่ Output layer (binary classification)
ReLU(x) = max(0, x)
if x > 0: return x else: return 0
ReLU'(x) = 1 if x>0, else 0
ดี คำนวณเร็ว, ไม่ vanishing gradient แย่ Dying ReLU (neurons ตาย) ใช้ที่ Hidden layers ทั่วไป
tanh(x) = (e^x - e^(-x))/(e^x + e^(-x))
tanh(x) = 2σ(2x) - 1
tanh'(x) = 1 - tanh²(x)
ดี Zero-centered แย่ ยัง vanishing gradient ใช้ที่ RNN, LSTM
10 layers, แต่ละ layer มี gradient = 0.25 Layer 1: 0.25 Layer 5: 0.25^5 = 0.00098 Layer 10: 0.25^10 = 0.0000009 → Layers แรกๆ แทบไม่เรียนรู้!
∂L/∂w₁ = ∂L/∂a₁₀ × ∂a₁₀/∂a₉ × ... × ∂a₂/∂a₁ × ∂a₁/∂w₁
Sigmoid: 0.25 × 0.25 × 0.25 = 0.015625 (ลดเร็ว!) ReLU: 1 × 1 × 1 = 1 (คงที่ ✓)
ใช้ ReLU แทน Sigmoid
Batch Normalization
Residual Connections (ResNet)
H(x) = F(x) + x
Better Initialization
W ~ N(0, √(2/n_in))
W ~ N(0, √(2/(n_in+n_out)))
ทุก neuron: z = 0×x + 0 = 0 → output เหมือนกัน → gradient เหมือนกัน → เหมือนมี neuron เดียว!
Layer 1: variance = 100 Layer 2: variance = 10,000 Layer 3: variance = 1,000,000 → Exploding!
Var(output) = Var(input)
Var(y) = n × Var(w) × Var(x) ถ้าต้องการ Var(y) = Var(x): n × Var(w) × Var(x) = Var(x) → Var(w) = 1/n → std(w) = √(1/n)
std(w) = √(2/(n_in + n_out))
std(w) = √(2/n_in)
from tensorflow import keras from tensorflow.keras import layers # สร้าง model model = keras.Sequential([ layers.Dense(16, activation='relu', input_shape=(2,)), layers.Dropout(0.2), # ป้องกัน overfitting layers.Dense(1, activation='sigmoid') ]) # Compile model.compile( optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'] )
"หยุดเมื่อไม่ดีขึ้น" - ป้องกัน overfitting
"ลด learning rate เมื่อติด" - ปรับละเอียดขึ้น
"บันทึก model ที่ดีที่สุด" - ไม่ใช่ model สุดท้าย
Traditional ML Interpretability: ████████░░ (80%) Speed: ████████░░ (80%) Accuracy: ██████░░░░ (60%) Data Needed: ███░░░░░░░ (30%) Neural Network Interpretability: ██░░░░░░░░ (20%) Speed: ████░░░░░░ (40%) Accuracy: █████████░ (90%) Data Needed: ████████░░ (80%)
□ 1. Overfit small dataset ก่อน (sanity check) → ถ้าไม่ได้ = bug ใน code □ 2. เริ่มจาก model ง่ายๆ → 2 layers → 3 layers → ... □ 3. Monitor metrics → Loss ลดลงไหม? → Train vs Val แตกต่างมากไหม? → Gradients ปกติไหม? □ 4. Visualize everything → Loss curves, Weight distributions
Always normalize inputs
อายุ (20-60) vs เงินเดือน (20,000-100,000) ถ้าไม่ normalize → gradient ต่างกัน 1000 เท่า!
Start simple - Occam's Razor
Use ReLU for hidden layers (default)
Batch size 32 - memory alignment
Learning rate 0.001 for Adam
Monitor validation ตลอดเวลา
Save best model ไม่ใช่ model สุดท้าย
Ensemble สำหรับความแม่นยำสูงสุด
Data > Algorithm
Reproducibility - set random seeds
"Neural Networks are like cooking - you need the right ingredients (data), proper preparation (preprocessing), correct temperature (learning rate), and patience (epochs). Master the basics before attempting the fancy dishes!"
Prepare Review matrix operations และ image processing basics!
_style