Residual block with shortcutEasynumpyneural-networkresnetresidualcnn
Residual block with shortcut
Background
A residual block is the building block of ResNets. Instead of asking a stack of layers to learn a full mapping , it learns a residual and adds back the input: . This shortcut (skip) connection lets gradients flow directly to earlier layers, making very deep networks trainable and largely curing the degradation problem.
Problem statement
Implement residual_block(x, w1, w2) for a two-layer block with a ReLU between the weight layers, an additive shortcut, and a final ReLU:
Input
x— input vector,np.ndarrayof shape(n,).w1— first weight matrix, shape(m, n).w2— second weight matrix, shape(n, m)(so matchesx's shape).
Output
An np.ndarray of shape (n,): the block output .
Examples
Example 1
Input: x = [1.0, 2.0], w1 = [[1,0],[0,1]], w2 = [[0.5,0],[0,0.5]]
Output: [1.5, 3.0]
Explanation: , ReLU keeps it; ; add the shortcut to get ; the final ReLU leaves it unchanged.
Constraints
- ReLU is , applied after the first weight layer and again after the addition.
- The shortcut adds the original
x(identity mapping), so must have the same shape asx. - Use matrix-vector products (
np.dot/@).
Notes
- When the optimal mapping is close to identity, the block only has to drive — far easier than learning identity through nonlinear layers.
- If the block changes the channel/spatial dimensions, a real ResNet projects the shortcut (e.g. a 1×1 conv) so the shapes line up before adding.
Python
Loading...
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example
- •Zero weights reduce to ReLU of the shortcut
- •Final ReLU clamps negative sums to zero
- •Output shape matches the input