πŸ€– AI/ML

ReLU

Rectified Linear Unit

The most widely used activation function. Converts negative values to 0.

πŸ“– Detailed Explanation

ReLU (Rectified Linear Unit) is the most widely used activation function in deep learning. Its formula is f(x) = max(0, x): if the input is positive, it is output as is, and if it is negative, 0 is output. Simple yet effective, it has become a basic building block of modern neural networks.

ReLU was proposed by Nair and Hinton in 2010 and was widely adopted following the success of AlexNet in 2012. The previously used Sigmoid and Tanh functions had the vanishing gradient problem, but ReLU largely alleviated this problem because its gradient is a constant 1 in the positive region.

The key advantages of ReLU are computational efficiency and gradient propagation. The max(0, x) operation is very fast, and because the gradient is 1 in the positive region, training works well even in deep networks. It also induces sparsity, which increases the network's representational power.

In practice, besides the basic ReLU, variants such as Leaky ReLU, PReLU, ELU, and GELU are widely used. In particular, GELU has become the standard in Transformer-based models, while ReLU is still the default in CNNs. It is important to be aware of the Dying ReLU problem and to use appropriate initialization and normalization together.

πŸ’» Code Example

A PyTorch implementation and comparison of various activation functions.

import torch
import torch.nn as nn
import matplotlib.pyplot as plt

# μž…λ ₯ λ²”μœ„
x = torch.linspace(-3, 3, 100)

# ν™œμ„±ν™” ν•¨μˆ˜λ“€
activations = {
    "ReLU": nn.ReLU(),
    "LeakyReLU": nn.LeakyReLU(0.1),  # 음수 기울기 0.1
    "ELU": nn.ELU(alpha=1.0),
    "GELU": nn.GELU(),  # Transformerμ—μ„œ μ‚¬μš©
    "SiLU/Swish": nn.SiLU(),  # x * sigmoid(x)
}

# 각 ν™œμ„±ν™” ν•¨μˆ˜ 적용
results = {name: fn(x) for name, fn in activations.items()}

# μ‹ κ²½λ§μ—μ„œμ˜ μ‚¬μš© μ˜ˆμ‹œ
class ConvBlock(nn.Module):
    def __init__(self, in_ch, out_ch, activation="relu"):
        super().__init__()
        self.conv = nn.Conv2d(in_ch, out_ch, 3, padding=1)
        self.bn = nn.BatchNorm2d(out_ch)

        # ν™œμ„±ν™” ν•¨μˆ˜ 선택
        if activation == "relu":
            self.act = nn.ReLU(inplace=True)
        elif activation == "gelu":
            self.act = nn.GELU()
        elif activation == "leaky":
            self.act = nn.LeakyReLU(0.1, inplace=True)

    def forward(self, x):
        return self.act(self.bn(self.conv(x)))

# λͺ¨λΈμ—μ„œ ν™œμ„±ν™” ν•¨μˆ˜ 확인
model = nn.Sequential(
    ConvBlock(3, 64, "relu"),
    ConvBlock(64, 128, "relu"),
)
print(f"ReLU ν™œμ„±ν™” ν•¨μˆ˜ μ‚¬μš© 블둝: {model}")

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"ReLU is the default for CNNs, but Transformers use GELU. You need to choose the activation function that fits the model architecture."
πŸ’¬ In an Interview
"The advantages of ReLU are computational efficiency and alleviation of the vanishing gradient. The drawback is the Dying ReLU problem, which can be solved with Leaky ReLU or proper initialization."
πŸ’¬ In a Technical Discussion
"GELU has a smooth curve in the form of x * Phi(x), so it shows better performance in Transformers. Both GPT and BERT use GELU."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌
Ignoring Dying ReLU: If the learning rate is too high, neurons end up permanently outputting 0. Use He initialization and an appropriate learning rate.
❌
Using ReLU in the output layer: If negative predictions are needed in a regression problem, you must not use ReLU in the output layer. Choose the output layer according to the task.
βœ…
Using it together with BatchNorm: BatchNorm normalizes the input to prevent Dying ReLU. The Conv-BN-ReLU order is common.

πŸ”— Related Terms

πŸ“š Learn More