Fold BatchNorm into the Convolution
Implement both functions. has shape (C_out, K) with one row per output channel; every other argument is a length-C_out vector.
fuse_bn_weight(W, gamma, var, eps)returns W′.fuse_bn_bias(b, gamma, beta, mean, var, eps)returns b′.
Examples
Identity BN (gamma 1, var 1, eps 0) leaves the weights alone
- Input
- fuse_bn_weight([[1, 2], [3, 4]], [1, 1], [1, 1], 0)
- Output
- [[1, 2], [3, 4]]
gamma 4 over sqrt(3 + 1) is a scale of 2 on the weights
- Input
- fuse_bn_weight([[1, 2]], [4], [3], 1)
- Output
- [[2, 4]]
The same scale on the bias, after subtracting the running mean
- Input
- fuse_bn_weight([4], [3], 1, b=[1], beta=[0], mean=[0.5])
- Output
- [1]
Hints
Hint 1
Take the square root at the end, not inside the sum.
Hint 2
Watch for this: scaled by gamma without dividing by the std.
Requirements
: (C_out, K) weights, one row per output channel
gamma: (C_out,) BN scale: (C_out,) BN running variance
eps: BN epsilonReturn (C_out, K) fused weights
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
8 employers weight this skill
4 autonomy companies, 1 defense company, 1 health and bio company, 1 enterprise vendor, 1 AI product company. Top match scores 78.
import numpy as np
def fuse_bn_weight(W, gamma, var, eps):
"""
Fold the BatchNorm scale into the conv weights.
Args:
W: (C_out, K) weights, one row per output channel
gamma: (C_out,) BN scale
var: (C_out,) BN running variance
eps: BN epsilon
Returns:
(C_out, K) fused weights
"""
# YOUR CODE HERE
pass
def fuse_bn_bias(b, gamma, beta, mean, var, eps):
"""
Fold the BatchNorm shift into the conv bias.
Args:
b: (C_out,) conv bias
gamma: (C_out,) BN scale
beta: (C_out,) BN shift
mean: (C_out,) BN running mean
var: (C_out,) BN running variance
eps: BN epsilon
Returns:
(C_out,) fused bias
"""
# YOUR CODE HERE
pass