Activation function — ReLU, sigmoid
এই পাঠে যা শিখবেন
- Activation function কেন প্রয়োজন — non-linearity-র গাণিতিক ভিত্তি
- Sigmoid, tanh, ReLU, Leaky ReLU, GELU, Softmax — কোনটি কী
- প্রতিটির derivative — backprop-এর জন্য জরুরি
- Vanishing gradient সমস্যা ও ReLU কেন solution
- PyTorch-এ activation comparison — কোডে দেখা
১ · Activation কেন প্রয়োজন
ভাবুন একটি MLP — দু'টি linear layer পাশাপাশি, কোনো activation নেই।
$$\mathbf{y} = \mathbf{W}_2 (\mathbf{W}_1 \mathbf{x} + \mathbf{b}_1) + \mathbf{b}_2 = (\mathbf{W}_2 \mathbf{W}_1) \mathbf{x} + (\mathbf{W}_2 \mathbf{b}_1 + \mathbf{b}_2)$$
$\mathbf{W}_2 \mathbf{W}_1$ — একটি নতুন matrix। অর্থাৎ পুরো network = একটি single linear layer। যত স্তর যোগ করুন — কাজে লাগবে না।
Linear + Linear = Linear। Non-linear activation না থাকলে — depth অর্থহীন। প্রতিটি hidden layer-এর পর একটি non-linear function অপরিহার্য।
২ · Sigmoid — ক্লাসিক activation
$$\sigma(z) = \frac{1}{1 + e^{-z}}$$
- Range: $(0, 1)$ — output সবসময় ০ ও ১-এর মধ্যে।
- Smooth: সর্বত্র differentiable।
- Probability interpretation: binary classification-এ output = প্রবাবিলিটি।
- Derivative: $\sigma'(z) = \sigma(z)(1 - \sigma(z))$ — সরল।
সমস্যা:
- Vanishing gradient: $|z|$ বড় হলে — $\sigma'(z) \approx 0$। Deep network-এ gradient শূন্যে নামে।
- Not zero-centered: output সবসময় positive — gradient এক দিকেই বড়।
- Computationally expensive: exponential calculation।
৩ · Tanh — Sigmoid-এর zero-centered ভাই
$$\tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}}$$
- Range: $(-1, 1)$ — zero-centered।
- Derivative: $1 - \tanh^2(z)$।
- RNN-এ ক্লাসিক।
- কিন্তু — vanishing gradient সমস্যা একই।
৪ · ReLU — DL Revolution-এর চাবি
$$\text{ReLU}(z) = \max(0, z)$$
- সরল: $z > 0$ হলে $z$, না হলে $0$।
- দ্রুত: exponential নেই — হার্ডওয়্যারে এক comparison।
- Gradient: $z > 0$-এ $1$, $z < 0$-এ $0$। Vanishing সমস্যা সমাধান positive region-এ।
- Sparse activation: অর্ধেক neuron $0$ output — energy-efficient।
সমস্যা — Dying ReLU:
- একটি neuron যদি সবসময় negative input পায় — gradient সবসময় $0$, কখনো recover করে না।
- মৃত neuron — useless parameter।
৫ · Leaky ReLU — Dying সমস্যার সমাধান
$$\text{LeakyReLU}(z) = \begin{cases} z & z > 0 \\ \alpha z & z \leq 0 \end{cases}$$
সাধারণত $\alpha = 0.01$। Negative region-এও ছোট gradient — neuron মরে না।
৬ · GELU, Swish — Modern activation
GELU (Gaussian Error Linear Unit):
$$\text{GELU}(z) = z \cdot \Phi(z)$$
যেখানে $\Phi$ = standard normal CDF। Smoother, probabilistic interpretation। BERT, GPT-2/3-এ ব্যবহৃত।
Swish (SiLU):
$$\text{Swish}(z) = z \cdot \sigma(z)$$
Google-এর ২০১৭-এর paper, EfficientNet-এ। GELU-র অনুরূপ behavior।
৭ · Output layer-এর Activation
Output-এর activation task-নির্ভর:
- Binary classification: Sigmoid → একটি সংখ্যা (০-১)।
- Multi-class classification: Softmax → প্রতিটি class-এর probability, যোগফল ১।
- Regression: কোনো activation না — সরাসরি real number।
- Multi-label: sigmoid প্রতিটি class-এর জন্য আলাদা।
Softmax: $$\text{softmax}(z_i) = \frac{e^{z_i}}{\sum_j e^{z_j}}$$
৮ · PyTorch-এ Activation comparison
import torch
import torch.nn.functional as F
z = torch.tensor([-2.0, -0.5, 0.0, 0.5, 2.0])
print("Input z =", z.tolist())
print(f"Sigmoid: {torch.sigmoid(z).tolist()}")
print(f"Tanh: {torch.tanh(z).tolist()}")
print(f"ReLU: {F.relu(z).tolist()}")
print(f"LeakyReLU:{F.leaky_relu(z, 0.1).tolist()}")
print(f"GELU: {F.gelu(z).tolist()}")
print(f"Softmax: {F.softmax(z, dim=0).tolist()}")
৯ · কোনটি কখন বাছবেন
| Layer | প্রস্তাবিত | কেন |
|---|---|---|
| Hidden (CNN/MLP) | ReLU | দ্রুত, vanishing-free, default |
| Hidden (Transformer) | GELU | smoother, BERT/GPT default |
| Hidden (RNN/LSTM) | tanh + sigmoid (gate) | bounded, stable recurrence |
| Output (binary) | sigmoid | probability |
| Output (multi-class) | softmax | normalized probability |
| Output (regression) | none | unconstrained real value |
ভাবনার প্রশ্ন
প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।
প্র ০১ ReLU-এর "Dying ReLU" সমস্যা কী, কেন এটি বিশেষ ক্ষতিকর? Leaky ReLU, ELU, PReLU — এগুলো কীভাবে সমাধান দেয়, এবং কেন তবু ReLU এখনো default?
ReLU-এর সবচেয়ে discussed সমস্যা — যা সব ML practitioner-কে জানতে হয়।
Dying ReLU কী:
- একটি neuron যদি সব training example-এ negative input পায় — output সবসময় $0$।
- Backprop-এ gradient = $0$ (negative region-এ ReLU-এর derivative ০)।
- Weight কখনো update হয় না — neuron permanently dead।
- Network-এর capacity কমে — কিছু parameter wasted।
কখন ঘটে:
- Learning rate বেশি হলে — bias বড় negative-এ ঠেকে যায়।
- Bad initialization — random weight সব input-কে negative-এ ঠেলে।
- Internal covariate shift — distribution বদলায়, neuron pre-saturates।
সমাধানগুলো:
- Leaky ReLU: negative region-এ small slope ($0.01z$) — gradient never zero।
- PReLU (Parametric ReLU): Leaky-র $\alpha$ — learnable parameter। Best fit data-নির্ভর।
- ELU (Exponential LU): negative region smooth ($\alpha(e^z - 1)$)। Mean activation closer to zero।
- SELU (Scaled ELU): self-normalizing — special initialization-এ batch norm-এর কাজ করে।
- GELU: negative region-এ smooth probabilistic decay।
তবু কেন ReLU default:
- Speed: ReLU-র চেয়ে দ্রুত activation নেই। Hardware-friendly।
- Sparsity: অর্ধেক neuron $0$ — natural regularization।
- Practical: সঠিক init (He) + batch norm-এ dying সমস্যা rare।
- Empirical: ResNet, EfficientNet — ReLU-তেই SOTA।
কখন বিকল্প choose:
- Transformer — GELU বা SwiGLU।
- Very deep network without batch norm — Leaky/ELU।
- Smaller dataset — সাধারণ ReLU-এর dying risk বেশি।
মূল উপলব্ধি: ReLU "perfect না কিন্তু practical সেরা"। ML-এর অনেক design choice এমন — theoretical perfection নয়, empirical reliability।
প্র ০২ "Vanishing gradient" কী এবং deep network-এ কেন এটি critical সমস্যা? Sigmoid কেন গভীর network-এর জন্য বিপজ্জনক?
DL-এর central technical challenge — যা সমাধানে কয়েক দশক লেগেছে।
Vanishing gradient — গাণিতিকভাবে:
- Backprop = chain rule — প্রতিটি স্তরের gradient গুণ হয়।
- Sigmoid-এর max derivative = $0.25$।
- ১০ স্তরে: $0.25^{10} \approx 10^{-6}$ — অর্থাৎ প্রথম স্তরের ওজন practically zero gradient।
- মূল layer-গুলো শেখে না — শুধু last few layers train হয়।
লক্ষণ:
- Loss একটা পর্যায়ে stuck — কমে না।
- Early layers-এর weight initialization-এর কাছাকাছি থাকে।
- Deep network shallow-এর চেয়ে খারাপ — counter-intuitive।
- Gradient norm exponentially decreasing।
Sigmoid-এর বিপদ:
- $|z|$ বড় হলে — derivative ~$0$।
- Forward pass-এ saturation = backward-এ death।
- Random init-এও — activation দ্রুত saturated region-এ চলে যায়।
সমাধানগুলো (ঐতিহাসিক ক্রমে):
- Better init (Glorot/Xavier ২০১০): activation distribution stable।
- ReLU (২০১১, ২০১২): gradient never saturates in positive region।
- Batch normalization (২০১৫): internal activation normalize, gradient flow stable।
- Skip connection / ResNet (২০১৫): gradient highway — সরাসরি deep থেকে shallow।
- Layer normalization, GroupNorm: Transformer-এ batch-free alternative।
Exploding gradient — opposite problem:
- Gradient exponentially grow — NaN, Inf।
- Gradient clipping সমাধান।
- RNN-এ বিশেষ সমস্যা।
আজকের practice:
- ReLU/GELU + BatchNorm/LayerNorm + ResNet/skip = trainable 100+ layer network।
- GPT-3-এর 96 layer — এই combo-র সাফল্য।
মূল উপলব্ধি: Vanishing gradient — DL winter-এর মূল কারণ। আজ এটি "solved" — কিন্তু পুরোপুরি না। Trillion-parameter model-এ এখনো subtle issue।
প্র ০৩ Softmax কেন binary classification-এ ব্যবহার হয় না, sigmoid-ই কেন? Multi-class-এ কেন উল্টো?
Output activation-এর সঠিক বাছাই — common confusion। গাণিতিকভাবে দু'টোই related।
Sigmoid (binary):
- একটি output neuron — class ১-এর probability।
- Class ০-এর probability = $1 - \sigma(z)$।
- সরাসরি BCE loss-এর সাথে কাজ করে।
Softmax (multi-class):
- $K$ class-এর জন্য $K$ output neuron।
- Output ভেক্টর — প্রতিটি class-এর probability, যোগফল ১।
- CrossEntropy loss-এর সাথে কাজ করে।
গাণিতিক সম্পর্ক:
- Sigmoid = ২-class softmax-এর special case।
- $\sigma(z) = \text{softmax}([z, 0])_0$ — gauge symmetry।
Binary-তে softmax কি ব্যবহার করা যায়:
- হ্যাঁ — ২ output neuron + softmax + CrossEntropy।
- কিন্তু wasteful — ১ output + sigmoid + BCE-এর সমান।
- Convention-এ — binary-তে sigmoid, multi-তে softmax।
Multi-class-এ sigmoid কখন:
- Multi-label classification: একটি image-এ multiple class থাকতে পারে।
- প্রতিটি class-এর জন্য আলাদা sigmoid + BCE।
- Probability-গুলো independent — যোগফল ১ না।
- উদাহরণ: "image-এ কি বিড়াল আছে? কুকুর আছে?" — উভয়ই হতে পারে।
Numerical stability:
- Softmax-এ exponential — overflow risk।
- Solution: log-softmax + NLLLoss (PyTorch)।
- বা CrossEntropyLoss — internally combined।
- Manual softmax + log = numerical disaster।
আধুনিক LLM-এ:
- Vocabulary 50K+ token — large softmax।
- Temperature — softmax-এর parameter, sampling control।
- Top-k, top-p — softmax distribution থেকে sample।
মূল উপলব্ধি: Output activation = task-এর mathematical encoding। ভুল choice = wrong loss = bad model। Binary → sigmoid + BCE; multi-class → softmax + CrossEntropy; multi-label → multiple sigmoid + BCE।
প্র ০৪ "Activation function research" কি settled, না কি এখনও active research area? GELU, Swish, Mish, SwiGLU — এগুলোর performance gain কি practical, না marginal?
Activation function — DL-এর সবচেয়ে "polished" কিন্তু এখনো কিছু gain বাকি।
Activation evolution timeline:
- 1958: Step (Perceptron)
- 1980s-2000s: Sigmoid, Tanh
- 2011-2012: ReLU (Glorot, AlexNet) — DL revolution
- 2013-2015: Leaky ReLU, ELU, PReLU
- 2016: GELU (Hendrycks) — BERT-এ adopted
- 2017: Swish/SiLU (Google) — searched activation
- 2019: Mish (Misra) — slight improvement
- 2022: SwiGLU — gated variant, LLaMA-এ ব্যবহৃত
SwiGLU — modern LLM-এর চয়েস:
- $\text{SwiGLU}(x) = \text{Swish}(xW_1) \odot (xW_2)$
- Gating mechanism — adaptive feature selection।
- LLaMA, PaLM-এ SOTA।
- Parameter বেশি — কিন্তু performance gain উল্লেখযোগ্য।
Performance gain — কতটা practical:
- ReLU → GELU: ImageNet-এ ~০.৫% accuracy, BERT-এ subtle improvement।
- GELU → Swish: marginal, prompt-specific।
- Swish → SwiGLU: ১-২% downstream task improvement (LLM)।
- Effect compound হয় scale-এ — billion-parameter model-এ matter।
Active research area:
- Learned activation: network নিজেই activation শিখে (Maxout, PReLU)।
- Activation search: Neural Architecture Search (NAS) for activations।
- Task-specific activation: NLP, vision, speech — different optimal।
- Quantization-friendly: ReLU INT8-এ ভাল, GELU কঠিন।
- Energy-efficient activation: mobile/edge device-এ।
Settled vs. open:
- Settled: hidden layer-এ ReLU/GELU/SwiGLU; output-এ sigmoid/softmax।
- Open: trillion-parameter scale-এ optimal activation; multi-modal; spiking neural networks।
Practical advice (2024+):
- CNN, MLP, RNN — ReLU default।
- Transformer (BERT, GPT) — GELU default।
- Latest LLM (LLaMA, Mistral) — SwiGLU।
- Custom domain — empirical comparison।
মূল উপলব্ধি: Activation gain marginal কিন্তু compound। ML scaling-এর era-তে — ০.৫% improvement একটি model worth millions। Research থেমে নেই।
অনুশীলন
-
Compute: $z = 2$-এ sigmoid, tanh, ReLU, GELU-র approximate value কত?
- $\sigma(2) = 1/(1+e^{-2}) \approx 0.881$
- $\tanh(2) \approx 0.964$
- $\text{ReLU}(2) = 2$
- $\text{GELU}(2) \approx 1.954$
-
Derivative: ReLU-র derivative কত $z = -1$ ও $z = 1$-এ?
- $z = -1$: derivative = $0$।
- $z = 1$: derivative = $1$।
- $z = 0$: technically undefined; PyTorch convention-এ $0$।
-
Choose: নিচের প্রতিটি task-এর জন্য কোন output activation:
- (ক) Email spam কিনা
- (খ) ছবিতে digit ০-৯
- (গ) কাল-এর বৃষ্টির পরিমাণ (mm)
- (ঘ) ছবিতে কি কি object আছে (multiple)
- (ক) sigmoid (binary)
- (খ) softmax (multi-class)
- (গ) none (regression)
- (ঘ) multiple sigmoid (multi-label)
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ০৫ · Universal approximation theorem পরবর্তী পাঠ কেন non-linear activation = universal approximator।
- পাঠ ০৩ · MLP আগের পাঠ যেখানে activation-এর প্রয়োজন প্রথম প্রকাশ পায়।
- পাঠ ১৪ · Vanishing/Exploding gradient সম্পর্কিত Activation choice-এর সরাসরি প্রভাব।
- সব AI Courses দেখুন ABCL TECH Python, ML, DL, NLP, CV, GenAI, RL, MLOps — সব AI কোর্স একসাথে।