Bird
Raised Fist0

Activation Functions - ReLU, Sigmoid, GELU, Why ReLU Dominates

THEORYintermediateInterview freq: high
Choose your preparation mode6 modes available
▶
Execution Trace
Step 1/5
Input Neuron

Input value is 0.5, ready to be processed by activation functions.

🎯 Understanding the initial input value before activation is crucial.
input_value = 0.5
ReLU

ReLU processes input 0.5, output remains 0.5 as it is positive.

🎯 ReLU outputs the input directly if it is positive, otherwise zero.
relu_output = max(0, input_value)
Sigmoid

Sigmoid processes input 0.5, output is approximately 0.622.

🎯 Sigmoid squashes input to a range between 0 and 1.
sigmoid_output = 1 / (1 + np.exp(-input_value))
GELU

GELU processes input 0.5, output is approximately 0.345.

🎯 GELU combines linear and non-linear transformations.
gelu_output = input_value * norm.cdf(input_value)
Output Neuron

Outputs from ReLU, Sigmoid, and GELU are 0.5, 0.622, and 0.345 respectively.

🎯 Different activation functions produce different outputs for the same input.
outputs = {'ReLU': relu_output, 'Sigmoid': sigmoid_output, 'GELU': gelu_output}
📊
Diagram
Diagram
          Activation Functions Overview
          ┌───────────────────────────┐
          │                           │
          │       Input Neuron        │
          │                           │
          └────────────┬──────────────┘
                       │
                       ▼
          ┌───────────────────────────┐
          │       Activation          │
          │       Functions           │
          ├──────────┬───────────────┤
          │ ReLU     │ Sigmoid       │
          │ f(x)=max(0,x) │ f(x)=1/(1+e^-x) │
          │ GELU     │                │
          │ f(x)=x*Φ(x) │                │
          └──────────┴───────────────┘
                       │
                       ▼
          ┌───────────────────────────┐
          │       Output Neuron       │
          └───────────────────────────┘
  • Input Neuron: The initial input value to be transformed.
  • ReLU: Activation function defined as f(x)=max(0,x).
  • Sigmoid: Activation function defined as f(x)=1/(1+e^-x).
  • GELU: Activation function defined as f(x)=x*Φ(x), where Φ(x) is the Gaussian CDF.
  • Output Neuron: The final output after activation.
🔑
Key Moments
▲
Q: Why does ReLU output zero for negative inputs?
A: ReLU is defined as f(x)=max(0,x), so any negative input results in zero output.
Q: How does the Sigmoid function affect gradient flow?
A: Sigmoid can cause vanishing gradients for large positive or negative inputs due to its asymptotic nature.
Q: What advantage does GELU have over ReLU?
A: GELU can model more complex relationships due to its combination of linear and non-linear transformations.

This diagram explains the mechanisms of ReLU, Sigmoid, and GELU activation functions. It shows how each function transforms an input value from a neuron and produces an output. ReLU outputs the input directly if positive, otherwise zero, making it computationally efficient and widely used. Sigmoid squashes inputs to a range between 0 and 1, which can lead to vanishing gradients. GELU, a more complex function, combines linear and non-linear transformations, offering advantages in modeling complex relationships. Understanding these functions is crucial for selecting the right activation function in neural network design.

Primary Focus

✓ Understanding the mechanisms and differences between ReLU, Sigmoid, and GELU activation functions, and why ReLU is often preferred.

Practice

(1/5)
1.
import torch
import torch.nn.functional as F

query = torch.tensor([[1.0, 0.0]])
key = torch.tensor([[0.5, 0.5], [1.0, 0.0]])
value = torch.tensor([[1.0, 2.0], [3.0, 4.0]])

attention_scores = torch.matmul(query, key.T)
attention_weights = F.softmax(attention_scores, dim=-1)
output = torch.matmul(attention_weights, value)
print(output)
What does this code output?
easy
A. tensor([[1.5, 3.0]])
B. tensor([[2.0, 3.0]])
C. tensor([[3.0, 4.0]])
D. tensor([[1.0, 2.0]])

Solution

  1. Step 1: Calculate attention scores.

    Query-key dot product gives scores: [[0.5, 1.0]].
  2. Step 2: Apply softmax to scores.

    Softmax gives weights: [[0.2689, 0.7311]].
  3. Step 3: Compute weighted sum of values.

    Output = [[0.2689*1.0 + 0.7311*3.0, 0.2689*2.0 + 0.7311*4.0]] = [[1.5, 3.0]].
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Softmax weights applied to values. [OK]
Hint: Softmax weights applied to values. [OK]
Common Mistakes:
2.
import numpy as np

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

result = np.dot(x, y)
print(result)
What does this code output?
easy
A. An error due to shape mismatch
B. A (3, 3) matrix
C. 32
D. A (3,) array

Solution

  1. Step 1: Understand np.dot behavior

    np.dot for 1D arrays computes the dot product, which is a scalar.
  2. Step 2: Calculate the dot product

    1*4 + 2*5 + 3*6 = 32.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    np.dot 1D arrays = scalar [OK]
Hint: np.dot 1D arrays = scalar [OK]
Common Mistakes:
3. In the context of deep learning, why might a deep network be preferred over a shallow network for complex tasks?
easy
A. Deep networks can represent any function with fewer parameters.
B. Deep networks can capture hierarchical features more effectively.
C. Shallow networks are always faster to train.
D. Shallow networks generalize better on all tasks.

Solution

  1. Step 1: Understand the advantage of deep networks

    Deep networks can capture hierarchical features, which is crucial for complex tasks.
  2. Step 2: Eliminate wrong options

    Deep networks can represent any function with fewer parameters. is incorrect because deep networks do not always use fewer parameters. Shallow networks are always faster to train. is incorrect as shallow networks are not always faster. Shallow networks generalize better on all tasks. is incorrect because shallow networks do not always generalize better.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Deep networks capture hierarchies. [OK]
Hint: Deep networks capture hierarchies. [OK]
Common Mistakes:
4. When designing a CNN for image classification, why might you choose to use a smaller receptive field in the initial layers?
medium
A. To capture more global features early on
B. To reduce the computational cost of the network
C. To capture fine-grained local features
D. To increase the depth of the network without increasing parameters

Solution

  1. Step 1: Understand receptive fields

    Smaller receptive fields in initial layers help capture fine-grained local features, which are crucial for understanding detailed patterns.
  2. Step 2: Eliminate wrong options

    To capture more global features early on is incorrect because global features are captured in deeper layers. To reduce the computational cost of the network is incorrect as smaller receptive fields don't necessarily reduce computational cost. To increase the depth of the network without increasing parameters is incorrect because depth and parameters are not directly related to receptive field size.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Smaller fields capture local details. [OK]
Hint: Smaller fields capture local details. [OK]
Common Mistakes:
5. How would you adapt a Random Forest model to improve its robustness to adversarial attacks?
hard
A. Use adversarial training with perturbed data
B. Increase the number of trees significantly
C. Reduce the depth of each tree
D. Increase the max_features parameter

Solution

  1. Step 1: Understand adversarial robustness

    Adversarial training involves training the model with perturbed data to improve robustness.
  2. Step 2: Evaluate adaptation strategies

    Use adversarial training with perturbed data is correct because adversarial training directly addresses robustness. Increase the number of trees significantly is incorrect as more trees don't inherently improve adversarial robustness. Reduce the depth of each tree is incorrect because reducing depth may not address adversarial examples. Increase the max_features parameter is incorrect as increasing max_features doesn't target adversarial robustness.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Adversarial training improves robustness. [OK]
Hint: Adversarial training improves robustness. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard