快捷方式

GroupNorm

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

對輸入的小批次資料應用 Group Normalization。

此層實現論文 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 是按通道學習的仿射變換引數向量,如果 affineTrue,它們的大小為 num_channels。方差是使用有偏估計器計算的,相當於 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)

文件

查閱 PyTorch 全面的開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並解答您的問題

檢視資源