We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← CNNs from Scratch step 7 of 15
Skip Connection Block
A skip connection adds an input directly to a learned transformation of that input. Here, the transformation has two dense layers with a ReLU between them, and the block applies another ReLU after the addition.
Your task
Implement residual_block(x, W1, b1, W2, b2):
xhas shape(batch, dim), with one example per row.W1andW2each have shape(dim, dim).b1andb2each have shape(dim,)and are shared across the batch.- Return shape
(batch, dim), preserving x's floating-point dtype.
The supplied tensors have matching dtype and device. Compute these operations in order, using the matrices in their supplied orientation:
h = ReLU(x @ W1 + b1)
branch = h @ W2 + b2
output = ReLU(x + branch)
ReLU(z) replaces negative entries with zero. Multiply row vectors on the
right by W1 and W2; do not transpose either matrix. There is no activation
between the second projection and skip addition. Leave all inputs unchanged.
For x=[[1, -2]], identity W1 and W2, b1=[0, 0] and b2=[-2, 3]:
h = [[1, 0]]
branch = [[-1, 3]]
x + branch = [[0, 1]]
output = [[0, 1]]
The skip path adds the original x, not h. If the branch is zero, the result
is ReLU(x), so negative inputs still become zero. This block's final ReLU
means a zero branch is not an identity mapping for arbitrary inputs.
This is a small dense residual block, not a complete convolutional ResNet block. There is no normalization, dropout or projection on the skip path.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Add a learned change to the input
A residual connection computes y = x + F(x). The branch F learns a change,
while the shortcut carries x directly to the sum. If F(x) is zero, the sum
is just x. An activation applied after the sum can still change it.
For example, x = [2, 3] and F(x) = [-1, 0.5] give y = [1, 3.5].
The two branches need matching shapes for the intended elementwise addition.
A projection can change the shortcut's width when necessary.
The derivative is I + dF/dx, so there is a gradient path through the shortcut
as well as through the learned branch. This can help optimization, but it does
not guarantee stable training. Scale, activations, and normalization placement
still matter.
Use the whole image to weight each channel
A squeeze-and-excitation block computes one gate per channel in three steps:
- Average each channel over its spatial positions. A channel containing
[1, 3]has average2. - Pass the channel averages through a small network to produce gates, commonly using a final sigmoid.
- Multiply every spatial position in each channel by that channel's gate.
The squeeze step reduces spatial axes while preserving channels. The excitation network combines information from different channels to choose their gates. Applying the gates preserves the original spatial shape.
Check the task's equations for biases, intermediate activations, and any residual connection around the block. The block's name alone does not specify all of these choices.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.