Residual block with shortcutEasy

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 H(x)H(x), it learns a residual F(x)F(x) and adds back the input: H(x)=F(x)+xH(x)=F(x)+x. 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:

F(x)=W2ReLU(W1x),y=ReLU(F(x)+x)F(x) = W_2\,\operatorname{ReLU}(W_1 x), \qquad y = \operatorname{ReLU}\big(F(x) + x\big)

Input

  • x — input vector, np.ndarray of shape (n,).
  • w1 — first weight matrix, shape (m, n).
  • w2 — second weight matrix, shape (n, m) (so F(x)F(x) matches x's shape).

Output

An np.ndarray of shape (n,): the block output yy.

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: W1x=[1,2]W_1 x=[1,2], ReLU keeps it; W2[1,2]=[0.5,1.0]W_2[1,2]=[0.5,1.0]; add the shortcut xx to get [1.5,3.0][1.5,3.0]; the final ReLU leaves it unchanged.

Constraints

  • ReLU is max(0,)\max(0,\cdot), applied after the first weight layer and again after the addition.
  • The shortcut adds the original x (identity mapping), so F(x)F(x) must have the same shape as x.
  • Use matrix-vector products (np.dot / @).

Notes

  • When the optimal mapping is close to identity, the block only has to drive F(x)0F(x)\to 0 — 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