Implement a matrix-based projection residual block. Matrix multiplication stands in for convolution so the exercise isolates the dimension-changing shortcut used by ResNet.
y=ReLU(ReLU(xW1)W2+xWs)Here, (W_1) and (W_2) form the main path, while (W_s) projects the shortcut to the output width. Return y as a nested list rounded to four decimal places.
Examples
Input: x = [[-0.7255, 0.9555, 0.3559], [-0.1239, 0.1807, -0.0165]], W1 = [[-0.1107, 0.2386, -0.346, 0.396], [0.0366, 0.6516, 0.1067, 0.5087], [0.9559, -0.2648, 0.9211, -0.5286]], W2 = [[-0.4315, 0.1188, -0.5771, 0.6075], [-0.6469, 0.4114, -0.1661, -0.6407], [0.1093, 1.0417, -0.1241, 0.1337], [0.3563, -0.3268, 0.0811, 1.1067]], Ws = [[-0.3328, -0.5045, 1.174, 0.3022], [0.3219, -0.1659, 0.0349, -0.0074], [0.8555, -0.1782, 0.3216, -0.1204]]
Output: [[0.8535, 1.05, -0.7039, -0.1173], [0.0853, 0.1061, -0.1445, -0.03]]
Explanation: Main path: relu(relu(x @ W1) @ W2). Shortcut: x @ Ws. Output = main + shortcut. The projection Ws maps input channels to output channels so the addition is valid.
Input: x = [[-1.279, -0.9778]], W1 = [[0.0129, 0.3866, 1.093], [0.8893, 0.1701, -0.1068]], W2 = [[0.5589, 0.4735, 0.1315], [-0.934, -0.1958, -0.5757], [0.0487, 0.791, -0.2414]], Ws = [[0.1, -0.1473, 0.4891], [-0.015, -0.3433, 0.0249]]
Output: [[-0.1132, 0.5241, -0.6499]]
Input: x = [[0.6688, -0.1164, 0.2769, -0.76], [0.5964, 0.2626, -0.4232, -0.0984]], W1 = [[-0.4315, 0.4927], [-0.0157, -0.4923], [1.1235, -0.0268], [0.2782, -1.0033]], W2 = [[-0.3416, 0.2316], [-0.282, -0.202]], Ws = [[-0.819, -0.0756], [0.4537, 0.0337], [0.3452, -0.035], [0.3737, -0.1532]]
Output: [[-0.789, 0.0523], [-0.5522, -0.0064]]
Hint 1
Compute shortcut = x @ Ws separately.
Hint 2
Use np.maximum(0, value) for ReLU.
Hint 3
Add the paths before the final activation.
Requirements
- Use NumPy float64 arrays for all inputs.
- Apply ReLU after W1.
- Add x @ Ws to the second main-path transformation.
- Apply ReLU after the addition and round the nested-list output to four decimal places.
Constraints
- x, W1, W2, and Ws are rank-2 arrays with compatible dimensions.
- W2 and Ws produce the same output width.
- Valid inputs are guaranteed.
All steps in ResNet
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Convolutional Block
Implement a matrix-based projection residual block. Matrix multiplication stands in for convolution so the exercise isolates the dimension-changing shortcut used by ResNet.
y=ReLU(ReLU(xW1)W2+xWs)Here, (W_1) and (W_2) form the main path, while (W_s) projects the shortcut to the output width. Return y as a nested list rounded to four decimal places.
Examples
Input: x = [[-0.7255, 0.9555, 0.3559], [-0.1239, 0.1807, -0.0165]], W1 = [[-0.1107, 0.2386, -0.346, 0.396], [0.0366, 0.6516, 0.1067, 0.5087], [0.9559, -0.2648, 0.9211, -0.5286]], W2 = [[-0.4315, 0.1188, -0.5771, 0.6075], [-0.6469, 0.4114, -0.1661, -0.6407], [0.1093, 1.0417, -0.1241, 0.1337], [0.3563, -0.3268, 0.0811, 1.1067]], Ws = [[-0.3328, -0.5045, 1.174, 0.3022], [0.3219, -0.1659, 0.0349, -0.0074], [0.8555, -0.1782, 0.3216, -0.1204]]
Output: [[0.8535, 1.05, -0.7039, -0.1173], [0.0853, 0.1061, -0.1445, -0.03]]
Explanation: Main path: relu(relu(x @ W1) @ W2). Shortcut: x @ Ws. Output = main + shortcut. The projection Ws maps input channels to output channels so the addition is valid.
Input: x = [[-1.279, -0.9778]], W1 = [[0.0129, 0.3866, 1.093], [0.8893, 0.1701, -0.1068]], W2 = [[0.5589, 0.4735, 0.1315], [-0.934, -0.1958, -0.5757], [0.0487, 0.791, -0.2414]], Ws = [[0.1, -0.1473, 0.4891], [-0.015, -0.3433, 0.0249]]
Output: [[-0.1132, 0.5241, -0.6499]]
Input: x = [[0.6688, -0.1164, 0.2769, -0.76], [0.5964, 0.2626, -0.4232, -0.0984]], W1 = [[-0.4315, 0.4927], [-0.0157, -0.4923], [1.1235, -0.0268], [0.2782, -1.0033]], W2 = [[-0.3416, 0.2316], [-0.282, -0.202]], Ws = [[-0.819, -0.0756], [0.4537, 0.0337], [0.3452, -0.035], [0.3737, -0.1532]]
Output: [[-0.789, 0.0523], [-0.5522, -0.0064]]
Hint 1
Compute shortcut = x @ Ws separately.
Hint 2
Use np.maximum(0, value) for ReLU.
Hint 3
Add the paths before the final activation.
Requirements
- Use NumPy float64 arrays for all inputs.
- Apply ReLU after W1.
- Add x @ Ws to the second main-path transformation.
- Apply ReLU after the addition and round the nested-list output to four decimal places.
Constraints
- x, W1, W2, and Ws are rank-2 arrays with compatible dimensions.
- W2 and Ws produce the same output width.
- Valid inputs are guaranteed.
All steps in ResNet
Accepts: array
Accepts: array
Accepts: array
Accepts: array