MLCrack

    DEEP LEARNING · INTERVIEW QUESTION

    What are activation functions? Compare ReLU, GELU, and Swish.

    🟡 intermediate · Neural Network Fundamentals

    Hint: Non-linearity between layers. Think about dead neurons and smooth gradients.

    Answer

    Activation functions introduce non-linearity, enabling neural networks to learn complex, non-linear relationships.

    Comparison

    FunctionFormulaProsCons
    ReLUmax⁡(0,x)\max(0, x)Simple, efficientDead neurons
    Leaky ReLUmax⁡(αx,x)\max(\alpha x, x)No dead neuronsExtra hyperparameter
    GELUx⋅Φ(x)x \cdot \Phi(x)Smooth, probabilisticSlightly slower
    Swish/SiLUx⋅σ(x)x \cdot \sigma(x)Smooth, non-monotonicSlightly slower

    When to Use

    • GELU: Transformers (BERT, GPT, ViT) — standard choice
    • ReLU/LeakyReLU: CNNs — simple and effective
    • Swish/SiLU: EfficientNet, modern architectures
    • Sigmoid/Tanh: Only for gates (LSTM) or output layers
    python
    import torch.nn as nn # Common activations relu = nn.ReLU() gelu = nn.GELU() silu = nn.SiLU() # Swish with β=1 # In a model model = nn.Sequential( nn.Linear(768, 3072), nn.GELU(), # standard for transformers nn.Linear(3072, 768), )

    Key issue with ReLU: 'dead neurons' — once a neuron outputs 0, it may never activate again. GELU and Swish are smooth and avoid ReLU's entire flat negative half-line, but each still has isolated points with zero derivative. One zero output on an input does not by itself mean a ReLU unit is permanently dead.