Chapter 08
ফরোয়ার্ড প্রোপাগেশন
Forward Propagation
🎬 ধারণা
একটি input (যেমন ছবি) দিলে নেটওয়ার্ক একটি output (যেমন "এটা বিড়াল") কীভাবে বের করে — সেই প্রক্রিয়াকেই বলা হয় Forward Propagation। input layer থেকে শুরু করে output layer পর্যন্ত — তথ্য সামনের দিকে প্রবাহিত হয়।
একটি Layer-এর গণিত
z = W·x + b
a = f(z)
এখানে W হলো weight matrix, x input, b bias,f activation function, এবং a এই layer-এর output (যা পরের layer-এর input)।
একটি ২-Layer নেটওয়ার্ক
Input (3) → Hidden Layer (4) → Output Layer (2)
W₁: (3, 4) W₂: (4, 2)
ReLU Softmaximport numpy as np
# Input
x = np.array([0.5, 0.2, 0.9])
# Layer 1 (3 -> 4)
W1 = np.random.randn(3, 4) * 0.1
b1 = np.zeros(4)
# Layer 2 (4 -> 2)
W2 = np.random.randn(4, 2) * 0.1
b2 = np.zeros(2)
def relu(z): return np.maximum(0, z)
def softmax(z):
e = np.exp(z - np.max(z))
return e / e.sum()
# Forward pass
z1 = x @ W1 + b1 # shape (4,)
a1 = relu(z1)
z2 = a1 @ W2 + b2 # shape (2,)
a2 = softmax(z2) # probability output
print("Output probabilities:", a2)Batch Processing
একবারে একটি input না, একসাথে অনেকগুলো (যেমন ৩২টি ছবি) — এটাই batch। GPU-তে এটা অনেক দ্রুত।
# 32 input একসাথে
X = np.random.randn(32, 3) # (batch, features)
Z1 = X @ W1 + b1 # (32, 4)
A1 = relu(Z1)
Z2 = A1 @ W2 + b2 # (32, 2)
# softmax row-wise
A2 = np.exp(Z2) / np.exp(Z2).sum(axis=1, keepdims=True)🔑 Shape মনে রাখার নিয়ম
যদি input shape (batch, in) এবং weight shape (in, out), তাহলে output (batch, out)। Shape mismatch হলে error হবে — তাই matrix shape সবসময় চেক করুন।
একটি Class-ভিত্তিক MLP
class MLP:
def __init__(self, sizes):
self.W = [np.random.randn(a, b) * 0.1 for a, b in zip(sizes, sizes[1:])]
self.b = [np.zeros(s) for s in sizes[1:]]
def forward(self, X):
A = X
for i, (W, b) in enumerate(zip(self.W, self.b)):
Z = A @ W + b
A = relu(Z) if i < len(self.W) - 1 else softmax(Z)
return A
net = MLP([784, 128, 64, 10]) # MNIST-like
X = np.random.randn(5, 784)
print(net.forward(X).shape) # (5, 10)সাধারণ ভুল ধারণা
⚠️ যা এড়িয়ে চলবেন
- সব weight 0 দিয়ে initialize — সব নিউরন একই শিখবে।
- খুব বড় weight initialization — exploding values।
- Output layer-এর activation ভুল — classification-এ Softmax, regression-এ কোনোটা না।
অনুশীলন
১. একটি 3-layer MLP বানান (10 → 20 → 15 → 3) এবং random input দিয়ে forward pass চালান।
২. Batch size 1 ও 100 — দুটোতে forward pass চালিয়ে shape check করুন।
মিনি প্রজেক্ট
🚀 আজকের চ্যালেঞ্জ
MNIST-এর dummy data (784 input → 10 output) তৈরি করে শুধু forward pass চালান। Output-এর top class print করুন।
ইন্টারভিউ প্রশ্ন
- Forward propagation কী?
- Weight matrix-এর shape কীভাবে ঠিক করেন?
- Batch processing-এর সুবিধা কী?
সারসংক্ষেপ
✨ এই অধ্যায়ে যা শিখলাম
- Forward propagation = input থেকে output বের করা।
- প্রতিটি layer =
z = Wx + b,a = f(z)। - Batch processing GPU-তে দ্রুত।
- পরের অধ্যায়ে শিখব — output কতটা ভুল, সেটা মাপার উপায় (Loss Function)।