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

Skip connection 과 residual block

~8 min · functional

Level 0Keras 도제
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

아주 깊은 net 을 풀어준 패턴

Skip connection 의 코드 패턴: x = Conv2D(...)(input); x = Conv2D(...)(x); output = keras.layers.add([x, input]). 즉 두 layer 통과한 결과에 *원본 입력을 더해서* 다음으로 넘김. block 이 transformation 만이 아니라 *input + transformation* 을 계산하는 거야. ResNet 의 핵심 발상.

왜 입력을 다시 더하면 좋나

ResNet (2015) 이전엔 layer 를 수십 개 넘게 쌓으면 오히려 학습이 *더 어려워졌어* — 정확도가 떨어졌지. skip connection 이 두 갈래로 이걸 풀었어. (1) add 가 *gradient 고속도로* 를 만들어 — backprop 때 gradient 가 identity path 로 곧장 흘러서 중간 nonlinearity 다 안 거치고 앞 layer 까지 도달, vanishing 완화. (2) block 이 *residual* (잔차) 만 학습하면 돼 — identity 위에 작은 보정 — 전체 mapping 을 처음부터 다시 배우는 것보다 훨씬 쉬운 타깃. 이 둘이 50·152 layer net 을 학습 가능하게 만들었고, 이젠 깊이가 중요한 거의 모든 곳에 나와 — 현대 LLM 뒤의 transformer block 포함.

layers.add([x, shortcut]) 는 element-wise 덧셈이라 두 tensor shape 가 같아야 해. block 이 channel 수를 바꾸거나 downsample 하면 더 이상 안 맞아 — shortcut 에 1×1 conv (또는 Dense) 를 끼워서 맞는 shape 로 project 해. residual block 이 빌드 실패하는 1 위 원인이 바로 이거야.

Code

residual block 하나, model 에서 두 번 사용·python
# A simple residual block
def residual_block(x, filters):
    shortcut = x  # Save the input

    x = layers.Dense(filters, activation="relu")(x)
    x = layers.Dense(filters)(x)

    # Add the skip connection
    x = layers.add([x, shortcut])
    x = layers.Activation("relu")(x)
    return x

# Use it in a model
inputs = keras.Input(shape=(128,))
x = layers.Dense(128, activation="relu")(inputs)
x = residual_block(x, 128)
x = residual_block(x, 128)
outputs = layers.Dense(10, activation="softmax")(x)
model = keras.Model(inputs, outputs)

External links

Exercise

ResNet block 하나 짜 — input → Conv → BN → ReLU → Conv → BN → add(input) → ReLU. Functional API 로. output shape 가 input shape 과 일치 확인 (안 맞으면 shortcut 에 1×1 conv 추가).

Progress

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

댓글 0

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

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