Problems
Loading...
Step 1 of 6
Convolutional Layers
Easy

Implement the cross-correlation used by an AlexNet-style convolution layer for an NHWC image batch. For image XX, kernel KK, bias bb, stride ss, and zero-padding pp, each output value is

Yn,i,j,o=bo+∑u=0Kh−1∑v=0Kw−1∑c=0C−1Xn,is+u,js+v,c(p)Ku,v,c,o.Y_{n,i,j,o}=b_o+\sum_{u=0}^{K_h-1}\sum_{v=0}^{K_w-1}\sum_{c=0}^{C-1}X^{(p)}_{n,is+u,js+v,c}K_{u,v,c,o}.

Here, nn selects a batch item, (i,j)(i,j) selects an output location, cc is an input channel, oo is an output channel, and X(p)X^{(p)} is the zero-padded image. Return the convolution result as a float64 NumPy array with shape (B,Hout,Wout,Cout)(B,H_{out},W_{out},C_{out}).

Loading visualization...

Examples

Input: image = [[[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]]], kernel = [[[[1]],[[0]]],[[[0]],[[-1]]]], bias = [0], stride = 1, padding = 0

Output: [[[[-4],[-4]],[[-4],[-4]]]]

Explanation: Each kernel window is multiplied elementwise with the image patch, summed, and shifted by the output-channel bias.

Input: image = [[[[1,2],[3,4]],[[5,6],[7,8]]]], kernel = [[[[2],[-1]]]], bias = [0.5], stride = 1, padding = 0

Output: [[[[0.5],[2.5]],[[4.5],[6.5]]]]

Input: image = [[[[1],[2]],[[3],[4]]]], kernel = [[[[1]],[[1]]],[[[1]],[[1]]]], bias = [0], stride = 2, padding = 1

Output: [[[[1],[2]],[[3],[4]]]]

Hint 1

Use np.pad only on the height and width axes.

Hint 2

np.tensordot can contract a patch over its height, width, and input-channel axes.

Hint 3

The output sizes are floor((size + 2 * padding - kernel_size) / stride) + 1.

Requirements

  • Compute every output value from the supplied image, kernel, bias, stride, and padding.
  • Use NHWC image layout and (Kh,Kw,Cin,Cout)(K_h,K_w,C_{in},C_{out}) kernel layout.
  • Apply zero-padding equally to the height and width dimensions.
  • Return a new float64 NumPy array.

Constraints

  • image has shape (B,H,W,Cin)(B,H,W,C_{in}).
  • kernel has shape (Kh,Kw,Cin,Cout)(K_h,K_w,C_{in},C_{out}).
  • bias has shape (Cout,)(C_{out},).
  • stride is a positive integer and padding is a nonnegative integer.
  • H+2p≥KhH+2p\ge K_h and W+2p≥KwW+2p\ge K_w.