Neural Mastery
← Practice
ResNet Basic Residual Block Forward Passmedium

ResNet Basic Residual Block Forward Pass

medium Neural Networks by Hand 20 min

NumPy allowed · plain Python earns 10 bonus points

Overcome degradation and vanishing gradients in deep networks by learning residual mapping functions F(x) + x.

Task

Implement resnet_residual_block(...):\n1. Layer 1: h1 = relu(w1 * x) elementwise.\n2. Layer 2: h2 = w2 * h1 elementwise.\n3. Shortcut: res = shortcut_weight * x if shortcut_weight is not None else x.\n4. Output: relu(h2 + res) elementwise. Round each element to 4 decimal places.

Function Signature

resnet_residual_block(x: list[list[float]], w1: float, w2: float, shortcut_weight: float | None = None) -> list[list[float]]

Examples

Example 1: Identity shortcut block
Input: x = [[1, 2], [3, 4]] w1 = 0.5 w2 = 2 shortcut_weight = None
Output: [[2, 4], [6, 8]]
Example 2: Projection shortcut block
Input: x = [[2, -1], [0, 4]] w1 = 1 w2 = 1 shortcut_weight = 0.5
Output: [[3, 0], [0, 6]]
Example 3: Negative activations suppressed by post-addition ReLU
Input: x = [[-3, -2], [-1, 0]] w1 = 1 w2 = 1 shortcut_weight = None
Output: [[0, 0], [0, 0]]

Constraints

  • 1 <= H, W <= 256
  • w1, w2 are finite floats
Python3Saved ✓
def resnet_residual_block(x: list[list[float]], w1: float, w2: float, shortcut_weight: float | None = None) -> list[list[float]]:
"""Execute forward pass of a basic residual block with additive shortcut.

Args:
x: 2D feature map of shape (H, W).
w1: First layer linear scalar weight.
w2: Second layer linear scalar weight.
shortcut_weight: Projection weight for shortcut if present, else identity if None.

Returns:
2D list of output activations after residual addition and final ReLU.
"""
pass

Case 1: Identity shortcut block
Input: x = [[1, 2], [3, 4]] w1 = 0.5 w2 = 2 shortcut_weight = None
Expected: [[2, 4], [6, 8]]
Case 2: Projection shortcut block
Input: x = [[2, -1], [0, 4]] w1 = 1 w2 = 1 shortcut_weight = 0.5
Expected: [[3, 0], [0, 6]]
Case 3: Negative activations suppressed by post-addition ReLU
Input: x = [[-3, -2], [-1, 0]] w1 = 1 w2 = 1 shortcut_weight = None
Expected: [[0, 0], [0, 0]]