Implement a matrix-based ResNet bottleneck block. The three transformations reduce the feature width, process the narrow representation, and expand it again. This mirrors the 1 by 1, 3 by 3, 1 by 1 channel pattern in the original bottleneck architecture while omitting spatial convolution.
y=ReLU(W3ReLU(W2ReLU(xW1))+s(x))Here, (W_1) reduces the width, (W_2) operates in the bottleneck width, (W_3) expands it, and (s(x)) is x when Ws is null or xWs otherwise. Return the result as a nested list rounded to four decimal places.
Examples
Input: x = [[-0.7426, -0.1301, -0.7123, -0.4457], [0.3806, -0.3494, -0.1983, 0.6219]], W1 = [[0.0373, -0.1615], [0.7307, 0.6209], [0.1297, 0.5358], [-0.0651, 0.9429]], W2 = [[-0.3527, 0.1678], [0.2844, -0.4584]], W3 = [[-0.5299, -0.9595, 0.8399, -0.5658], [-0.5844, -0.1505, 1.6222, 0.8047]], Ws = null
Output: [[-0.7426, -0.1301, -0.7123, -0.4457], [0.3806, -0.3494, -0.1501, 0.6219]]
Explanation: The bottleneck reduces channels via W1, processes in the compressed space via W2, then expands back via W3. When in_channels != out_channels, a projection Ws is applied to the skip path.
Input: x = [[-0.6618, -0.508, -0.4297]], W1 = [[0.5457, -0.0239], [0.0839, 0.8292], [-0.0259, -0.535]], W2 = [[0.0063, -0.2333], [0.5687, 0.3594]], W3 = [[-0.2986, -0.3825, -0.4176], [0.6148, -0.1181, 0.677]], Ws = null
Output: [[-0.6618, -0.508, -0.4297]]
Input: x = [[0.445, 0.4881, 0.3688, 0.6601], [-0.5888, 0.7906, 0.4086, 1.1758]], W1 = [[-0.0572, 0.1029], [-1.2073, -0.2187], [0.3029, 0.0361], [-0.5501, -0.4557]], W2 = [[0.5818, -0.1294], [1.5347, -0.2352]], W3 = [[0.8051, 0.2357, 0.303, -0.1986, -0.1059, -0.5624], [0.2734, 0.6371, 0.2357, -0.1436, -0.1602, 0.0571]], Ws = [[0.5644, -0.1447, 1.179, 0.9419, -0.7793, 0.4806], [-0.3896, -0.0531, -0.3855, -0.6134, -0.9262, -0.6544], [-0.261, 0.5411, -0.4857, -0.5554, -0.0268, 0.4359], [-0.2159, 0.1356, 0.4377, -0.0175, -0.391, -0.0503]]
Output: [[-0.1778, 0.1988, 0.4463, -0.0966, -1.0668, 0.022], [-1.0008, 0.4238, -0.6828, -1.2871, -0.7441, -0.6814]]
Hint 1
Compute the shortcut before changing the main path.
Hint 2
The main path is ReLU(x @ W1), then ReLU(out @ W2), then out @ W3.
Hint 3
Apply the last ReLU after adding the shortcut.
Requirements
- Use NumPy float64 arrays for the numeric inputs.
- Apply ReLU after W1 and W2, but add the shortcut before the final ReLU.
- Use x directly when Ws is null and x @ Ws otherwise.
- Round the returned nested-list values to four decimal places.
Constraints
- All inputs are rank-2 arrays with compatible matrix dimensions.
- W3 output width matches the shortcut output width.
- Valid inputs are guaranteed.
All steps in ResNet
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Bottleneck Architecture
Implement a matrix-based ResNet bottleneck block. The three transformations reduce the feature width, process the narrow representation, and expand it again. This mirrors the 1 by 1, 3 by 3, 1 by 1 channel pattern in the original bottleneck architecture while omitting spatial convolution.
y=ReLU(W3ReLU(W2ReLU(xW1))+s(x))Here, (W_1) reduces the width, (W_2) operates in the bottleneck width, (W_3) expands it, and (s(x)) is x when Ws is null or xWs otherwise. Return the result as a nested list rounded to four decimal places.
Examples
Input: x = [[-0.7426, -0.1301, -0.7123, -0.4457], [0.3806, -0.3494, -0.1983, 0.6219]], W1 = [[0.0373, -0.1615], [0.7307, 0.6209], [0.1297, 0.5358], [-0.0651, 0.9429]], W2 = [[-0.3527, 0.1678], [0.2844, -0.4584]], W3 = [[-0.5299, -0.9595, 0.8399, -0.5658], [-0.5844, -0.1505, 1.6222, 0.8047]], Ws = null
Output: [[-0.7426, -0.1301, -0.7123, -0.4457], [0.3806, -0.3494, -0.1501, 0.6219]]
Explanation: The bottleneck reduces channels via W1, processes in the compressed space via W2, then expands back via W3. When in_channels != out_channels, a projection Ws is applied to the skip path.
Input: x = [[-0.6618, -0.508, -0.4297]], W1 = [[0.5457, -0.0239], [0.0839, 0.8292], [-0.0259, -0.535]], W2 = [[0.0063, -0.2333], [0.5687, 0.3594]], W3 = [[-0.2986, -0.3825, -0.4176], [0.6148, -0.1181, 0.677]], Ws = null
Output: [[-0.6618, -0.508, -0.4297]]
Input: x = [[0.445, 0.4881, 0.3688, 0.6601], [-0.5888, 0.7906, 0.4086, 1.1758]], W1 = [[-0.0572, 0.1029], [-1.2073, -0.2187], [0.3029, 0.0361], [-0.5501, -0.4557]], W2 = [[0.5818, -0.1294], [1.5347, -0.2352]], W3 = [[0.8051, 0.2357, 0.303, -0.1986, -0.1059, -0.5624], [0.2734, 0.6371, 0.2357, -0.1436, -0.1602, 0.0571]], Ws = [[0.5644, -0.1447, 1.179, 0.9419, -0.7793, 0.4806], [-0.3896, -0.0531, -0.3855, -0.6134, -0.9262, -0.6544], [-0.261, 0.5411, -0.4857, -0.5554, -0.0268, 0.4359], [-0.2159, 0.1356, 0.4377, -0.0175, -0.391, -0.0503]]
Output: [[-0.1778, 0.1988, 0.4463, -0.0966, -1.0668, 0.022], [-1.0008, 0.4238, -0.6828, -1.2871, -0.7441, -0.6814]]
Hint 1
Compute the shortcut before changing the main path.
Hint 2
The main path is ReLU(x @ W1), then ReLU(out @ W2), then out @ W3.
Hint 3
Apply the last ReLU after adding the shortcut.
Requirements
- Use NumPy float64 arrays for the numeric inputs.
- Apply ReLU after W1 and W2, but add the shortcut before the final ReLU.
- Use x directly when Ws is null and x @ Ws otherwise.
- Round the returned nested-list values to four decimal places.
Constraints
- All inputs are rank-2 arrays with compatible matrix dimensions.
- W3 output width matches the shortcut output width.
- Valid inputs are guaranteed.
All steps in ResNet
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array