C.W.K.
Stream
Lesson 07 of 10 · published

Residual Connection

~22 min · residual, skip, resnet

Level 0Curious
0 XP0/73 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

지난 10 년의 가장 중요한 architecture trick

Residual connection 은 y = x + f(x): block 의 output 이 input 더하기 block transformation. 'Skip' 이 gradient 를 block 의 local Jacobian 곱 없이 depth 통해 흐르게 해. 100-layer network (ResNet) 와 175-billion-parameter transformer (GPT-3 이상) 가 실제 trainable 한 이유.

Residual 전에는 layer 20-30 넘게 추가하면 training 이 나빠졌어. Residual 후에는 depth 가 끝까지 보답. Regularization trick 아니라 — depth-enabler 야.

팁: 코드에 x + sublayer(x) 보이면 residual connection. Shape 가 일치하는지 spot-check (skip + transform 이 같은 shape 여야). 안 맞으면 skip 에 projection (nn.Linear 또는 stride 있는 nn.Conv2d) 필요.

Residual 이 어디 나타나는가

Modern 한 곳 어디나. ResNet (CNN), Transformer (모든 block 에 residual 두 개 — attention 과 FFN), DenseNet (add 대신 concat), U-Net (encoder-to-decoder skip), Mamba/SSM block. Pattern 이 architecture family 초월.

간단한 math

y = x + f(x) 면, dy/dx = 1 + f'(x). f'(x) 가 작아도 (vanishing gradient), +1 이 gradient 살아있게 유지. Chain rule 의 곱이 sum-of-product 가 돼서 numerically 훨씬 forgiving.

원칙: Residual connection 이 deep learning 이 가진 'free' architectural improvement 에 가장 가까운 것. 본인이 design 하거나 빌려오는 모든 architecture 가 처음부터 residual 있어야 해, 나중에 bolt on 아냐.

Code

ResNet basic block·python
import torch.nn as nn

class BasicBlock(nn.Module):
    def __init__(self, in_ch, out_ch, stride=1):
        super().__init__()
        self.conv1 = nn.Conv2d(in_ch, out_ch, 3, stride=stride, padding=1, bias=False)
        self.bn1   = nn.BatchNorm2d(out_ch)
        self.conv2 = nn.Conv2d(out_ch, out_ch, 3, stride=1, padding=1, bias=False)
        self.bn2   = nn.BatchNorm2d(out_ch)
        self.relu  = nn.ReLU(inplace=True)
        # Projection if shapes don't match
        if stride != 1 or in_ch != out_ch:
            self.skip = nn.Sequential(
                nn.Conv2d(in_ch, out_ch, 1, stride=stride, bias=False),
                nn.BatchNorm2d(out_ch),
            )
        else:
            self.skip = nn.Identity()

    def forward(self, x):
        identity = self.skip(x)
        out = self.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        return self.relu(out + identity)

External links

Exercise

20 layer plain CNN 과 20 layer ResNet 같은 task 에 train. Plain CNN 이 struggle (vanishing gradient), ResNet 이 sail 해야. Gap 이 residual 이 중요하다는 직접 증거.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.