評價此頁

GroupNorm#

class torch.nn.GroupNorm(num_groups, num_channels, eps=1e-05, affine=True, device=None, dtype=None)[原始碼]#

對輸入的小批次應用組歸一化。

該層實現了論文 Group Normalization 中描述的操作。

y=xE[x]Var[x]+ϵγ+βy = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta

輸入通道被分成 num_groups 組,每組包含 num_channels / num_groups 個通道。 num_channels 必須能被 num_groups 整除。均值和標準差分別在每組內計算。 γ\gammaβ\beta 是可學習的、大小為 num_channels 的逐通道仿射變換引數向量(如果 affineTrue)。方差透過有偏估計量計算,等同於 torch.var(input, unbiased=False)

此層在訓練和評估模式下都使用從輸入資料計算的統計量。

引數
  • num_groups (int) – 將通道分成多少組的數量。

  • num_channels (int) – 輸入通道的數量。

  • eps (float) – 為了數值穩定性新增到分母中的值。預設值:1e-5

  • affine (bool) – 一個布林值,當設定為 True 時,該模組具有可學習的逐通道仿射引數,初始化為 1(權重)和 0(偏置)。預設值:True

形狀
  • 輸入:(N,C,)(N, C, *),其中 C=num_channelsC=\text{num\_channels}

  • 輸出: (N,C,)(N, C, *)(與輸入形狀相同)

示例

>>> input = torch.randn(20, 6, 10, 10)
>>> # Separate 6 channels into 3 groups
>>> m = nn.GroupNorm(3, 6)
>>> # Separate 6 channels into 6 groups (equivalent with InstanceNorm)
>>> m = nn.GroupNorm(6, 6)
>>> # Put all 6 channels into a single group (equivalent with LayerNorm)
>>> m = nn.GroupNorm(1, 6)
>>> # Activating the module
>>> output = m(input)