如何在PyTorch中將卷積神經網路的Batch Normalization改成Layer Normalization?
Mar 15, 2022
A ConvNet for the 2020s這篇研究當中,提出了ConvNeXt架構,介紹了許多提升卷積神經網路性能的方法,其中一個特別的點在於:將CNN常用的Batch Normalization改成Layer Normalization。
PyTorch官方雖然有提供一個torch.nn.LayerNorm
的API,但是該API要求的輸入維度(batch_size, height, width, channels)與一般CNN的輸入維度(batch_size, channels, height, width)不同,因此需要額外的調整Tensor的shape
如下圖所示,根據ConvNext釋出的程式碼,可以透過data_format="channels_first"的選項,使傳統(batch_size, channels, height, width)的Tensor shape,能夠正常進入LayerNorm當中進行運算。
參考資料