CH 34Phase 5 · Deep Learning Mathematics

Activation Function Mathematics

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

Linear layers স্তূপ করলেও পুরো network linear-ই থাকে — কারণ linear-এর composition linear। মাঝখানে একটু "বাঁক" না দিলে neural net আসলে শুধুই একটা matrix। সেই বাঁকটাই হলো Activation Function — non-linearity-এর spark।

কেন দরকার?

Activation না থাকলে: W₂(W₁x) = (W₂W₁)x = W'x → এক layer-এর সমান।

Common Activations

Sigmoid

σ(z) = 1 / (1 + e⁻ᶻ), σ'(z) = σ(z)(1−σ(z))

সমস্যা: saturate করে → vanishing gradient।

Tanh

tanh(z) = (eᶻ − e⁻ᶻ)/(eᶻ + e⁻ᶻ), range (−1, 1)

ReLU

ReLU(z) = max(0, z), ReLU'(z) = 1 if z > 0 else 0

সবচেয়ে জনপ্রিয় — দ্রুত, sparse, gradient vanish হয় না। সমস্যা: "dying ReLU"।

Leaky ReLU / GELU / SiLU

LeakyReLU(z) = max(αz, z), GELU(z) ≈ 0.5z(1 + tanh(√(2/π)(z + 0.044715z³)))

GELU/SiLU — modern Transformers (GPT, LLaMA)-এ default।

Softmax (output layer)

softmax(zᵢ) = eᶻⁱ / Σⱼ eᶻʲ

Multi-class probability distribution।

Python Implementation

pythonPython · NumPy
import numpy as np

def sigmoid(z): return 1/(1+np.exp(-z))
def tanh(z):    return np.tanh(z)
def relu(z):    return np.maximum(0, z)
def gelu(z):    return 0.5*z*(1+np.tanh(np.sqrt(2/np.pi)*(z+0.044715*z**3)))
def softmax(z):
    e = np.exp(z - z.max(axis=-1, keepdims=True))  # stable
    return e / e.sum(axis=-1, keepdims=True)

z = np.array([-2, -0.5, 0, 0.5, 2.0])
for name, fn in [("sigmoid",sigmoid),("tanh",tanh),("relu",relu),("gelu",gelu)]:
    print(f"{name:8s}", fn(z).round(3))

কোনটি কখন?

  • Hidden layers (CNN/MLP) → ReLU
  • Transformers → GELU / SiLU
  • Binary output → Sigmoid
  • Multi-class output → Softmax
  • RNN gates → Sigmoid + Tanh

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

  • Activation = non-linearity — deep network-এর প্রাণ।
  • ReLU = default; GELU/SiLU = modern; Softmax = output classifier।
  • Saturation → vanishing gradient; ReLU সেটা ভাঙে।