Snapchat VO 面试真题解析:PyTorch Sequential 中卷积与 BatchNorm 融合

15次阅读
没有评论

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.

这道题要求你在 PyTorch 的 nn.Sequential 中,把“卷积层 + 紧随其后的 BatchNorm2d”融合成一个等价的卷积层。核心思路是利用 BatchNorm 的 running_mean、running_var、weight、bias 和 eps,对卷积核权重与偏置做线性变换,从而让融合后的卷积在推理阶段输出与原来两层串联完全一致。实现时需要遍历序列模块,连续的 Conv2d 和 BatchNorm2d 组合,完成参数重标定后保留其他层(如激活、池化)顺序不变。

正文完
 0