What is ResNet?
ResNet (Residual Neural Network) is a deep learning architecture introduced in 2015 that adds identity skip connections bypassing one or more layers. Instead of fitting a direct mapping H(x) from scratch, each block learns a residual mapping F(x) = H(x) - x so the block outputs F(x) + x, making identity transformations trivial when extra layers are unneeded. This resolves the deep network degradation problem and creates an unobstructed additive gradient highway during backpropagation, enabling networks with hundreds of layers.
Study Card: ResNet (Residual Neural Network)
Direct Answer
ResNet (Residual Neural Network) revolutionized deep learning by introducing residual blocks equipped with identity skip connections. Prior to ResNet, simply stacking more convolutional layers—even with Batch Normalization preventing classic vanishing gradients—led to the degradation problem: training accuracy saturated and then degraded rapidly because plain nonlinear stacks struggle to approximate even the identity mapping.
ResNet reframes each block’s target mapping as a residual relative to its input :
If an identity mapping is optimal, driving residual weights toward zero is far easier for SGD than fitting through multiple ReLU layers. Furthermore, during backpropagation:
the identity term ensures gradient signals flow directly to earlier layers without exponential decay, making architectures like ResNet-50, ResNet-152, and modern Transformer residual streams trainable.
Key Terms
- Skip (Identity) Connection: Shortcut adding the block input directly to the residual output (using a projection when spatial dimensions or channel counts change).
- Residual Mapping (): The perturbation learned relative to the identity function.
- Degradation Problem: Empirical phenomenon where deeper plain networks exhibit higher training error than shallower counterparts (not caused by overfitting).
- Bottleneck Residual Block: Used in ResNet-50/101/152 (
1x1reduce3x31x1expand) to cut FLOPs in deep, wide networks.
Example
Consider training a 100-layer CNN on ImageNet. In a plain CNN, if layer 40 already extracted near-optimal representations for a sub-task, layers 41–100 must learn near-identity mappings through stacked convolutions and ReLUs—a poorly conditioned optimization target—and gradients weaken on the way back. With ResNet blocks, unneeded layers can simply drive so features pass intact along , while useful blocks refine features incrementally and pass gradients cleanly back through the additive shortcut.
Code Implementation
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import (
Activation,
Add,
BatchNormalization,
Conv2D,
Dense,
GlobalAveragePooling2D,
Input,
)
def residual_block(x, filters, kernel_size=3, strides=1):
"""Standard ResNet basic block with optional 1x1 projection shortcut."""
shortcut = x
y = Conv2D(filters, kernel_size, strides=strides, padding="same", use_bias=False)(x)
y = BatchNormalization()(y)
y = Activation("relu")(y)
y = Conv2D(filters, kernel_size, strides=1, padding="same", use_bias=False)(y)
y = BatchNormalization()(y)
if strides != 1 or x.shape[-1] != filters:
shortcut = Conv2D(filters, (1, 1), strides=strides, padding="same", use_bias=False)(x)
shortcut = BatchNormalization()(shortcut)
y = Add()([shortcut, y])
return Activation("relu")(y)
def create_cifar_resnet(input_shape=(32, 32, 3), num_classes=10):
"""Compact ResNet tailored for 32x32 CIFAR-10 images (3x3 stem, stride=1)."""
inputs = Input(shape=input_shape)
x = Conv2D(64, 3, strides=1, padding="same", use_bias=False)(inputs)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = residual_block(x, 64)
x = residual_block(x, 64)
x = residual_block(x, 128, strides=2)
x = residual_block(x, 128)
x = residual_block(x, 256, strides=2)
x = residual_block(x, 256)
x = GlobalAveragePooling2D()(x)
outputs = Dense(num_classes, activation="softmax")(x)
return keras.Model(inputs=inputs, outputs=outputs)
model = create_cifar_resnet()
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"],
)
# Smoke-test forward & training step on synthetic CIFAR-shaped batch
x_dummy = tf.random.uniform((16, 32, 32, 3))
y_dummy = tf.random.uniform((16,), maxval=10, dtype=tf.int32)
history = model.fit(x_dummy, y_dummy, epochs=1, batch_size=8, verbose=0)
print(f"ResNet params: {model.count_params():,}, 1-step loss: {history.history['loss'][0]:.4f}")
Related Concepts
- Vanishing Gradients vs. Degradation: Even when BatchNorm mitigates vanishing gradients, optimization degradation occurs in very deep plain networks; identity skips address both.
- Pre-Activation ResNet (ResNet-v2): Moves BatchNorm and ReLU before the convolutions (
BN -> ReLU -> Conv), making the skip path a completely clean identity connection (also the standard Pre-LN residual structure in modern Transformers). - Highway Networks: Precursor using learned gating weights instead of ResNet’s parameter-free identity addition.
- DenseNet: Concatenates outputs from all prior layers within a block rather than adding only the preceding block’s input.