TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement a Simple CNN Layer (NumPy)

Neural Networks
Medium

Implement one valid two-dimensional convolution layer using the cross-correlation convention common in neural networks. The input has shape (N,Cin,H,W)(N,C_{in},H,W)(N,Cin​,H,W), the weights have shape (Cout,Cin,KH,KW)(C_{out},C_{in},K_H,K_W)(Cout​,Cin​,KH​,KW​), and the bias has shape (Cout,)(C_{out},)(Cout​,).

For every output position, compute

yn,c,i,j=∑d=1Cin∑u=0KH−1∑v=0KW−1xn,d,i+u,j+vWc,d,u,v+bcy_{n,c,i,j} = \sum_{d=1}^{C_{in}} \sum_{u=0}^{K_H-1} \sum_{v=0}^{K_W-1} x_{n,d,i+u,j+v}W_{c,d,u,v} + b_cyn,c,i,j​=d=1∑Cin​​u=0∑KH​−1​v=0∑KW​−1​xn,d,i+u,j+v​Wc,d,u,v​+bc​

The output height is H−KH+1H-K_H+1H−KH​+1 and the output width is W−KW+1W-K_W+1W−KW​+1. Return a floating-point NumPy array with shape (N,Cout,H−KH+1,W−KW+1)(N,C_{out},H-K_H+1,W-K_W+1)(N,Cout​,H−KH​+1,W−KW​+1).

Loading visualization...

Examples

Input: x = [[[[1, 1, 1], [1, 1, 1], [1, 1, 1]]]], W = [[[[1, 1], [1, 1]]]], b = [0]

Output: [[[[4.0, 4.0], [4.0, 4.0]]]]

Explanation: Every 2 by 2 input patch and kernel product sums to 4.

Hint 1

patch = x[n, :, i:i + kernel_height, j:j + kernel_width] selects one receptive field.

Hint 2

np.sum(patch * W[output_channel]) combines all input channels and kernel positions.

Hint 3

Initialize the output with np.zeros((N, C_out, H_out, W_out), dtype=float).

Requirements

  • Use valid padding and stride 1
  • Sum over every input channel and kernel position
  • Add the corresponding output-channel bias
  • Support batches and multiple input and output channels
  • Return a floating-point NumPy array

Constraints

  • Batch size is at most 8
  • Input and output channel counts are at most 4
  • Input height and width are at most 10
  • Use NumPy only
Try Similar Problems
Conv2d Image FilteringMax Pooling 2dMaxpool ForwardGlobal Avg PoolingBatch Normalization

Sign in to take notes on this problem

Case 1

Accepts: array

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Implement a Simple CNN Layer (NumPy)

Neural Networks
Medium

Implement one valid two-dimensional convolution layer using the cross-correlation convention common in neural networks. The input has shape (N,Cin,H,W)(N,C_{in},H,W)(N,Cin​,H,W), the weights have shape (Cout,Cin,KH,KW)(C_{out},C_{in},K_H,K_W)(Cout​,Cin​,KH​,KW​), and the bias has shape (Cout,)(C_{out},)(Cout​,).

For every output position, compute

yn,c,i,j=∑d=1Cin∑u=0KH−1∑v=0KW−1xn,d,i+u,j+vWc,d,u,v+bcy_{n,c,i,j} = \sum_{d=1}^{C_{in}} \sum_{u=0}^{K_H-1} \sum_{v=0}^{K_W-1} x_{n,d,i+u,j+v}W_{c,d,u,v} + b_cyn,c,i,j​=d=1∑Cin​​u=0∑KH​−1​v=0∑KW​−1​xn,d,i+u,j+v​Wc,d,u,v​+bc​

The output height is H−KH+1H-K_H+1H−KH​+1 and the output width is W−KW+1W-K_W+1W−KW​+1. Return a floating-point NumPy array with shape (N,Cout,H−KH+1,W−KW+1)(N,C_{out},H-K_H+1,W-K_W+1)(N,Cout​,H−KH​+1,W−KW​+1).

Loading visualization...

Examples

Input: x = [[[[1, 1, 1], [1, 1, 1], [1, 1, 1]]]], W = [[[[1, 1], [1, 1]]]], b = [0]

Output: [[[[4.0, 4.0], [4.0, 4.0]]]]

Explanation: Every 2 by 2 input patch and kernel product sums to 4.

Hint 1

patch = x[n, :, i:i + kernel_height, j:j + kernel_width] selects one receptive field.

Hint 2

np.sum(patch * W[output_channel]) combines all input channels and kernel positions.

Hint 3

Initialize the output with np.zeros((N, C_out, H_out, W_out), dtype=float).

Requirements

  • Use valid padding and stride 1
  • Sum over every input channel and kernel position
  • Add the corresponding output-channel bias
  • Support batches and multiple input and output channels
  • Return a floating-point NumPy array

Constraints

  • Batch size is at most 8
  • Input and output channel counts are at most 4
  • Input height and width are at most 10
  • Use NumPy only
Try Similar Problems
Conv2d Image FilteringMax Pooling 2dMaxpool ForwardGlobal Avg PoolingBatch Normalization

Sign in to take notes on this problem

Case 1

Accepts: array

Accepts: array

Accepts: array

You must run your code first.