我要评分
获取效率
正确性
完整性
易理解

GroupNorm

Scenario Description

Normalize tensors via the mean square error. Currently, KuDNN supports the torch.float16 and torch.float32 data types. For other data types, see the open-source branch.

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import torch
import torch.nn as nn

# Enable KuDNN.
torch._C._set_kdnn_enabled(True)

# Input data: (batch_size, channels, height, width)
input = torch.randn(2, 6, 32, 32) # 2 images, 6 channels, 32 × 32 resolution, default data type: fp32

# Define GroupNorm (3 groups, 2 channels in each group).
group_norm = nn.GroupNorm(num_groups=3, num_channels=6) # 6 channels are divided into 3 groups. The default data type is fp32.

# Forward computation
output = group_norm(input)

# Verification: normalization within each group
print("GroupNorm output shape: ", output.shape # Retain the original shape.