ReLU
Rectified Linear Unit
The most widely used activation function. Converts negative values to 0.
Rectified Linear Unit
The most widely used activation function. Converts negative values to 0.
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.
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}")
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free