Implement a matrix-based ResNet identity block in which the main path and shortcut have the same feature width.
y=ReLU(ReLU(xW1T)W2T+x)Here, x is the unchanged shortcut and (W_1) and (W_2) are stored with output features by input features. Return y as a nested list rounded to four decimal places.
Examples
Input: x = [[-0.8749, 0.1713, 0.5765], [-0.1262, 0.4907, 0.2571]], W1 = [[0.1106, -0.535, -0.0947], [0.1275, -0.229, 0.2176], [-0.2918, 0.4084, 0.3364]], W2 = [[-0.0522, -0.2656, 0.5149], [-0.2191, -0.5592, 0.8095], [0.7708, -0.1259, -0.4212]]
Output: [[0.0, 0.5916, 0.3578], [0.0405, 0.7527, 0.1208]]
Explanation: ReLU after the skip add clips negative values to zero. Without the final ReLU, the first entry would be -0.6076, but the residual connection followed by ReLU ensures non-negative output.
Input: x = [[0.0923, 0.4685, 0.3655]], W1 = [[0.6808, -0.1631, 0.0278], [0.1112, -0.7216, -0.3782], [0.4082, 0.3752, -0.228]], W2 = [[0.5948, -0.8453, -0.6782], [-0.6162, -0.2722, -0.3341], [0.0037, -0.3065, 0.6499]]
Output: [[0.004, 0.425, 0.4501]]
Input: x = [[-0.8665, -0.4917, 0.1788, -0.8068], [0.7354, -0.594, -0.2749, -0.47]], W1 = [[-0.414, 0.0544, 0.2539, -0.4311], [0.6247, -0.0398, -0.4449, -0.4409], [0.0093, 0.1189, 0.0068, -0.8178], [-0.5221, 0.3065, 0.3681, 0.5135]], W2 = [[-0.7161, -0.9206, 0.183, -0.1659], [-0.3446, 1.0173, -0.2754, 0.3752], [-0.6535, 0.2903, -0.5523, 0.3451], [0.3434, -0.7833, 0.4525, 0.3894]]
Output: [[0.0, 0.0, 0.0, 0.0], [0.0457, 0.1449, 0.0, 0.0]]
Hint 1
Copy x before evaluating the main path.
Hint 2
Use x @ W1.T for the first transformation.
Hint 3
Add the untouched shortcut before the final ReLU.
Requirements
- Use NumPy float64 arrays for all inputs.
- Multiply by the transpose of each stored weight matrix.
- Apply ReLU after the first transformation and after residual addition.
- Round the nested-list output to four decimal places.
Constraints
- x is rank 2.
- W1 and W2 are square rank-2 arrays matching x's feature width.
- Valid inputs are guaranteed.
All steps in ResNet
Accepts: array
Accepts: array
Accepts: array
Identity Block
Implement a matrix-based ResNet identity block in which the main path and shortcut have the same feature width.
y=ReLU(ReLU(xW1T)W2T+x)Here, x is the unchanged shortcut and (W_1) and (W_2) are stored with output features by input features. Return y as a nested list rounded to four decimal places.
Examples
Input: x = [[-0.8749, 0.1713, 0.5765], [-0.1262, 0.4907, 0.2571]], W1 = [[0.1106, -0.535, -0.0947], [0.1275, -0.229, 0.2176], [-0.2918, 0.4084, 0.3364]], W2 = [[-0.0522, -0.2656, 0.5149], [-0.2191, -0.5592, 0.8095], [0.7708, -0.1259, -0.4212]]
Output: [[0.0, 0.5916, 0.3578], [0.0405, 0.7527, 0.1208]]
Explanation: ReLU after the skip add clips negative values to zero. Without the final ReLU, the first entry would be -0.6076, but the residual connection followed by ReLU ensures non-negative output.
Input: x = [[0.0923, 0.4685, 0.3655]], W1 = [[0.6808, -0.1631, 0.0278], [0.1112, -0.7216, -0.3782], [0.4082, 0.3752, -0.228]], W2 = [[0.5948, -0.8453, -0.6782], [-0.6162, -0.2722, -0.3341], [0.0037, -0.3065, 0.6499]]
Output: [[0.004, 0.425, 0.4501]]
Input: x = [[-0.8665, -0.4917, 0.1788, -0.8068], [0.7354, -0.594, -0.2749, -0.47]], W1 = [[-0.414, 0.0544, 0.2539, -0.4311], [0.6247, -0.0398, -0.4449, -0.4409], [0.0093, 0.1189, 0.0068, -0.8178], [-0.5221, 0.3065, 0.3681, 0.5135]], W2 = [[-0.7161, -0.9206, 0.183, -0.1659], [-0.3446, 1.0173, -0.2754, 0.3752], [-0.6535, 0.2903, -0.5523, 0.3451], [0.3434, -0.7833, 0.4525, 0.3894]]
Output: [[0.0, 0.0, 0.0, 0.0], [0.0457, 0.1449, 0.0, 0.0]]
Hint 1
Copy x before evaluating the main path.
Hint 2
Use x @ W1.T for the first transformation.
Hint 3
Add the untouched shortcut before the final ReLU.
Requirements
- Use NumPy float64 arrays for all inputs.
- Multiply by the transpose of each stored weight matrix.
- Apply ReLU after the first transformation and after residual addition.
- Round the nested-list output to four decimal places.
Constraints
- x is rank 2.
- W1 and W2 are square rank-2 arrays matching x's feature width.
- Valid inputs are guaranteed.
All steps in ResNet
Accepts: array
Accepts: array
Accepts: array