TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

102. PyTorch: Two-Layer Conv

Medium

Build a small two-layer convolutional network as an nn.Module and run a forward pass.

Signature: def conv_forward(x, w1, b1, w2, b2, stride1=1, padding1=0, stride2=1, padding2=0)

  • x: input (N, C_in, H, W) (nested list)
  • w1: first conv weight (C_mid, C_in, kH1, kW1)
  • b1: first conv bias (C_mid,)
  • w2: second conv weight (C_out, C_mid, kH2, kW2)
  • b2: second conv bias (C_out,)
  • stride1/padding1: conv1 hyperparams
  • stride2/padding2: conv2 hyperparams
  • Returns: output of conv2 ∘ ReLU ∘ conv1 as a nested list

What you must do:

  1. Define an nn.Module subclass whose __init__ creates self.conv1 = nn.Conv2d(...), self.relu = nn.ReLU(), and self.conv2 = nn.Conv2d(...) with the right channel and kernel sizes inferred from the weight tensors.
  2. forward(x): return self.conv2(self.relu(self.conv1(x))).
  3. Instantiate it, copy in the supplied weights under torch.no_grad() with .copy_(), run the forward pass, return the output as a nested list.

This problem covers forward only — there is no backward computation required.

Math

out=Conv2​(ReLU(Conv1​(x)))

Asked at

NumPy

import numpy as np

 

def conv_forward(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?