CH 35Phase 5 · Deep Learning Mathematics

CNN Mathematics

১৫–২৫ মিনিট বাংলা · Math · Python
📖 একটি ছোট গল্প

কল্পনা করুন একটি conveyor belt — input এক প্রান্তে যায়, layer-by-layer transform হয়ে অন্য প্রান্তে prediction বেরিয়ে আসে। এটাই Forward Propagation — neural network-এর "চিন্তা করা"-র গাণিতিক ধাপ।

Forward Flow

L-layer network-এ প্রতিটি layer ℓ-এর জন্য:

z⁽ℓ⁾ = W⁽ℓ⁾ a⁽ℓ⁻¹⁾ + b⁽ℓ⁾, a⁽ℓ⁾ = σ⁽ℓ⁾(z⁽ℓ⁾)

শুরুতে a⁽⁰⁾ = x, শেষে ŷ = a⁽ᴸ⁾

Shape Algebra

Batch size m, layer ℓ-এ nℓ neuron:

  • X: (m, n₀)
  • W⁽ℓ⁾: (nℓ₋₁, nℓ), b⁽ℓ⁾: (1, nℓ)
  • A⁽ℓ⁾: (m, nℓ)
⚠️ সতর্কতা
Shape mismatch = deep learning bug #1। হাতে লিখে check করুন।

Loss Computation

Forward শেষে loss:

L = (1/m) Σᵢ ℓ(ŷᵢ, yᵢ)

Regression → MSE, Classification → Cross-Entropy।

Python Implementation

pythonPython · NumPy
import numpy as np

def relu(z): return np.maximum(0, z)
def softmax(z):
    e = np.exp(z - z.max(axis=-1, keepdims=True))
    return e / e.sum(axis=-1, keepdims=True)

# 3-layer network: 784 -> 128 -> 64 -> 10
np.random.seed(0)
W1, b1 = np.random.randn(784,128)*0.01, np.zeros((1,128))
W2, b2 = np.random.randn(128, 64)*0.01, np.zeros((1, 64))
W3, b3 = np.random.randn( 64, 10)*0.01, np.zeros((1, 10))

X = np.random.randn(32, 784)   # batch of 32 images
y = np.random.randint(0, 10, 32)

# forward pass — store cache for backprop
Z1 = X @ W1 + b1;  A1 = relu(Z1)
Z2 = A1 @ W2 + b2; A2 = relu(Z2)
Z3 = A2 @ W3 + b3; A3 = softmax(Z3)

# cross-entropy loss
log_probs = -np.log(A3[np.arange(32), y] + 1e-12)
loss = log_probs.mean()
print("loss:", loss)

Practice

  1. Hidden sizes পরিবর্তন করুন, shape mismatch error trigger করুন এবং ঠিক করুন।
  2. Batch size 1 দিয়ে চালান — broadcasting ঠিক আছে কি?

Summary · সারসংক্ষেপ

  • Forward pass = layer-wise linear + activation।
  • Shape algebra না বুঝলে debugging অসম্ভব।
  • Cache (Z, A) backprop-এর জন্য সংরক্ষণ করতে হয়।