Activation Function
Activation Function
A function that determines a neuron's output. ReLU, Sigmoid, Tanh, and Softmax are representative examples.
Activation Function
A function that determines a neuron's output. ReLU, Sigmoid, Tanh, and Softmax are representative examples.
An activation function is a nonlinear function that each neuron in a neural network uses to convert the signals it receives as input into an output signal. It applies the activation function to the weighted sum of the inputs to determine the output value passed on to the next layer. Without activation functions, a neural network would be just a sequence of simple linear transformations and could not learn complex patterns.
Inspired by biological neurons, it was first introduced in the McCulloch-Pitts model in 1943. Early on, the Step Function was used, and in the 1980s Sigmoid became mainstream along with the backpropagation algorithm. In the 2010s, the emergence of ReLU drove the rapid advancement of deep learning.
ReLU (Rectified Linear Unit) is computed as f(x) = max(0, x), which is simple to calculate and mitigates the vanishing gradient problem. Sigmoid compresses the output into the range 0 to 1, which is advantageous for probability interpretation, and Softmax produces a probability distribution at the output layer of multi-class classification. Tanh has a range of -1 to 1 and is centered closer to 0 than Sigmoid, so it trains faster.
In CNNs, ReLU is the default, and Leaky ReLU and PReLU, which also learn in the negative region, are alternatives. In Transformers, GELU (Gaussian Error Linear Unit) has become the standard. At the output layer, Sigmoid is used for binary classification, Softmax for multi-class classification, and no activation (Linear) for regression.
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import numpy as np
# 주요 활성화 함수 비교
x = torch.linspace(-5, 5, 100)
# 각 활성화 함수 적용
relu = nn.ReLU()(x)
sigmoid = torch.sigmoid(x)
tanh = torch.tanh(x)
leaky_relu = nn.LeakyReLU(0.1)(x)
gelu = nn.GELU()(x)
# 시각화
fig, axes = plt.subplots(2, 3, figsize=(12, 8))
functions = [
(relu, 'ReLU', 'max(0, x)'),
(sigmoid, 'Sigmoid', '1/(1+e^-x)'),
(tanh, 'Tanh', '(e^x-e^-x)/(e^x+e^-x)'),
(leaky_relu, 'Leaky ReLU', 'max(0.1x, x)'),
(gelu, 'GELU', 'x*CDF(x)'),
]
for ax, (y, name, formula) in zip(axes.flatten(), functions):
ax.plot(x.numpy(), y.detach().numpy(), linewidth=2)
ax.axhline(y=0, color='gray', linestyle='--', alpha=0.5)
ax.axvline(x=0, color='gray', linestyle='--', alpha=0.5)
ax.set_title(f'{name}: {formula}')
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# PyTorch 모델에서 활성화 함수 사용
class SimpleNN(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super().__init__()
self.fc1 = nn.Linear(input_dim, hidden_dim)
self.relu = nn.ReLU() # 은닉층 활성화
self.fc2 = nn.Linear(hidden_dim, output_dim)
# 출력층: 분류면 CrossEntropyLoss가 Softmax 포함
def forward(self, x):
x = self.fc1(x)
x = self.relu(x) # 비선형성 추가
x = self.fc2(x)
return x
model = SimpleNN(784, 256, 10)
print(model)
"For the hidden layers, we'll use ReLU by default, and for the output layer, we'll go with Softmax since it's multi-class classification. If negative inputs are also important features, we could switch to Leaky ReLU."
"Please explain ReLU's Dying ReLU problem. It's the problem where, once a neuron falls into the negative region, its gradient becomes 0 and it never activates again. To solve this, we use Leaky ReLU or ELU."
"You shouldn't use ReLU in the output layer. We need to output probabilities, but ReLU has a range of 0 to infinity. Use Sigmoid for binary classification and Softmax for multi-class classification."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
If the learning rate is too high, neurons get stuck in the negative region and are permanently deactivated. You can mitigate this by lowering the learning rate or by using Leaky ReLU, PReLU, ELU, and so on.
When the input is very large or very small, the gradient approaches 0, and training stalls in deep networks. Use the ReLU family in hidden layers, and use Sigmoid only in a limited way, at the output layer.
Using Sigmoid for a regression problem limits the output range, and using ReLU for a classification problem makes probability interpretation impossible. Choose the output layer activation function that matches the problem type.