Problems
Loading...
Step 5 of 6
Transition Layer
Medium

Implement the transition between two DenseNet blocks. Apply evaluation-mode batch normalization, ReLU, a bias-free 1 by 1 convolution, and 2 by 2 average pooling with stride 2.

z=Conv⁡1×1 ⁣(ReLU⁡(BN⁡(x)))z=\operatorname{Conv}_{1\times1}\!\left(\operatorname{ReLU}(\operatorname{BN}(x))\right) yn,c,i,j=14∑a=01∑b=01zn,c,2i+a,2j+by_{n,c,i,j}=\frac{1}{4}\sum_{a=0}^{1}\sum_{b=0}^{1}z_{n,c,2i+a,2j+b}

Here, the supplied convolution weight determines the compressed output width CoutC_{\mathrm{out}}. Return y as a float64 PyTorch tensor with shape (N,Cout,H/2,W/2)(N,C_{\mathrm{out}},H/2,W/2).

Loading visualization...

Examples

Input: x = [[[[1,2],[3,4]],[[-1,0],[1,2]]]], bn_gamma = [1,0.5], bn_beta = [0,0.1], bn_mean = [0,0], bn_var = [1,1], conv_weight = [[[[0.5]],[[-0.25]]]], eps = 0.00001

Output: [[[[1.137494]]]]

Explanation: The 1 by 1 convolution compresses channels before average pooling halves both spatial dimensions.

Input: x.shape = (1, 3, 4, 4), bn_gamma.shape = (3), bn_beta.shape = (3), bn_mean.shape = (3), bn_var.shape = (3), conv_weight.shape = (2, 3, 1, 1), eps = 0.00001

Output: [[[[0.003041,0.042495],[0.043089,0.028172]],[[-0.157591,0.026717],[-0.047066,-0.01502]]]]

Input: x.shape = (2, 2, 2, 4), bn_gamma.shape = (2), bn_beta.shape = (2), bn_mean.shape = (2), bn_var.shape = (2), conv_weight.shape = (1, 2, 1, 1), eps = 0.00001

Output: [[[[-0.218039,-0.293813]]],[[[-0.231119,-0.523215]]]]

Hint 1

Broadcast each batch-normalization vector to (1, C, 1, 1).

Hint 2

Use F.avg_pool2d with kernel_size=2 and stride=2.

Requirements

  • Use PyTorch.
  • Apply BN-ReLU-Conv before average pooling.
  • Use the supplied 1 by 1 convolution without a bias.
  • Use 2 by 2 average pooling with stride 2.
  • Return the documented float64 tensor.

Constraints

  • x has shape (N, C, H, W) and dtype torch.float64.
  • H and W are positive even integers.
  • Each batch-normalization vector has shape (C).
  • conv_weight has shape (C_out, C, 1, 1) and dtype torch.float64.
  • eps is positive.