Implement one numerical U-Net encoder block in NHWC layout. Apply two same-padded convolutions with ReLU, preserve the result as the skip tensor, then apply non-overlapping 2 by 2 max pooling.
S=ReLU(ConvK2(ReLU(ConvK1(X)))) P=MaxPool2×2(S)Here, X is the input, K1 and K2 are supplied odd-sized convolution kernels, S is the skip tensor, and P is the pooled tensor. Convolution uses zero padding so height and width are preserved before pooling. Return a Python dictionary with exactly pooled and skip, both float64 NumPy arrays.
Examples
Input: x = [[[[1],[-1]],[[2],[0.5]]]], kernel1 = [[[[2,-1]]]], bias1 = [0,0.5], kernel2 = [[[[0.5],[1]]]], bias2 = [0.1]
Output: {"pooled":[[[[2.1]]]],"skip":[[[[1.1],[1.6]],[[2.1],[0.6]]]]}
Explanation: The two convolution stages build the skip features before max pooling halves their spatial dimensions.
Input: x = [[[[1],[2]],[[3],[4]]]], kernel1 = [[[[1,-1]]]], bias1 = [0,1], kernel2 = [[[[0.5,0.2],[1,-0.5]]]], bias2 = [0,0.1]
Output: {"pooled":[[[[2,0.9]]]],"skip":[[[[0.5,0.3],[1,0.5]],[[1.5,0.7],[2,0.9]]]]}
Input: x = [[[[1],[0]],[[-1],[2]]],[[[0.5],[1.5]],[[2.5],[-0.5]]]], kernel1 = [[[[1]]]], bias1 = [0], kernel2 = [[[[2]]]], bias2 = [-0.5]
Output: {"pooled":[[[[3.5]]],[[[4.5]]]],"skip":[[[[1.5],[0]],[[0],[3.5]]],[[[0.5],[2.5]],[[4.5],[0]]]]}
Hint 1
Pad only the height and width axes by kernel_size // 2.
Hint 2
Use np.tensordot to contract a spatial patch with a convolution kernel.
Hint 3
Reshape paired spatial positions before reducing them with max.
Requirements
- Use NumPy.
- Apply each supplied convolution, bias, and ReLU in order.
- Use same zero padding for every odd kernel size.
- Apply non-overlapping 2 by 2 max pooling to the skip tensor.
- Return exactly the documented float64 arrays.
Constraints
- x has shape (B, H, W, C) and dtype float64.
- H and W are positive even integers.
- Both kernels are square with the same positive odd spatial size.
- Kernel input and output channels match adjacent tensors and biases.
- All kernels and biases have dtype float64.
All steps in U-Net
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
U-Net Encoder Block
Implement one numerical U-Net encoder block in NHWC layout. Apply two same-padded convolutions with ReLU, preserve the result as the skip tensor, then apply non-overlapping 2 by 2 max pooling.
S=ReLU(ConvK2(ReLU(ConvK1(X)))) P=MaxPool2×2(S)Here, X is the input, K1 and K2 are supplied odd-sized convolution kernels, S is the skip tensor, and P is the pooled tensor. Convolution uses zero padding so height and width are preserved before pooling. Return a Python dictionary with exactly pooled and skip, both float64 NumPy arrays.
Examples
Input: x = [[[[1],[-1]],[[2],[0.5]]]], kernel1 = [[[[2,-1]]]], bias1 = [0,0.5], kernel2 = [[[[0.5],[1]]]], bias2 = [0.1]
Output: {"pooled":[[[[2.1]]]],"skip":[[[[1.1],[1.6]],[[2.1],[0.6]]]]}
Explanation: The two convolution stages build the skip features before max pooling halves their spatial dimensions.
Input: x = [[[[1],[2]],[[3],[4]]]], kernel1 = [[[[1,-1]]]], bias1 = [0,1], kernel2 = [[[[0.5,0.2],[1,-0.5]]]], bias2 = [0,0.1]
Output: {"pooled":[[[[2,0.9]]]],"skip":[[[[0.5,0.3],[1,0.5]],[[1.5,0.7],[2,0.9]]]]}
Input: x = [[[[1],[0]],[[-1],[2]]],[[[0.5],[1.5]],[[2.5],[-0.5]]]], kernel1 = [[[[1]]]], bias1 = [0], kernel2 = [[[[2]]]], bias2 = [-0.5]
Output: {"pooled":[[[[3.5]]],[[[4.5]]]],"skip":[[[[1.5],[0]],[[0],[3.5]]],[[[0.5],[2.5]],[[4.5],[0]]]]}
Hint 1
Pad only the height and width axes by kernel_size // 2.
Hint 2
Use np.tensordot to contract a spatial patch with a convolution kernel.
Hint 3
Reshape paired spatial positions before reducing them with max.
Requirements
- Use NumPy.
- Apply each supplied convolution, bias, and ReLU in order.
- Use same zero padding for every odd kernel size.
- Apply non-overlapping 2 by 2 max pooling to the skip tensor.
- Return exactly the documented float64 arrays.
Constraints
- x has shape (B, H, W, C) and dtype float64.
- H and W are positive even integers.
- Both kernels are square with the same positive odd spatial size.
- Kernel input and output channels match adjacent tensors and biases.
- All kernels and biases have dtype float64.
All steps in U-Net
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array