You need to implement a function that fuses batch normalization layers within a PyTorch Sequential module.
To be more precise, the objective is to merge each pair of convolution layer and its subsequent batch normalization layer into a single convolution layer.
The fused convolution layer should be constructed in such a way that it produces the same results as the original combination of the convolution and batch normalization layers.
You can access the parameters of the batch normalization layer through the following attributes: eps, weight, bias, running_mean, and running_var.
The parameters for the convolution layer are found in weight.data and bias.data.
The relevant PyTorch classes are nn.Conv2d for the convolution layer and nn.BatchNorm2d for the batch normalization layer.
Example:
[Conv1, BN1, Activ, Conv2, Conv3, BN3, Activ, Pool] => [Conv1f, Activ, Conv2, Conv3f, Activ, Pool]
Use any standard Python modules, types, structures, and functions, but do not use numpy.
If needed, ask about specific function signature; for example, random.uniform(low, high) might be useful.
Don’t worry about the code structure and variable names.
Focus on properly solving the task without bugs and in time.
Estimated 20 minutes.
This problem asks you to fuse each Conv2d + BatchNorm2d pair inside a PyTorch Sequential model into a single equivalent convolution layer. The key is to use the batch norm parameters, especially running_mean, running_var, weight, bias, and eps, to rescale the convolution weights and adjust the bias so the fused layer matches the original inference-time behavior. You need to scan the module list, replace eligible pairs, and keep all other layers unchanged.