%% Apply Group Normalization
% Create a formatted dlarray object containing a batch of 128 28-by-28 images with 6 channels. Specify the format 'SSCB' (spatial, spatial, channel, batch).

miniBatchSize = 128;
inputSize = [28 28];
numChannels = 6;
X = rand(inputSize(1),inputSize(2),numChannels,miniBatchSize);
dlX = dlarray(X,'SSCB');

% View the size and format of the input data.

size(dlX)
dims(dlX)

% Initialize the scale and offset for group normalization. For the scale, specify a vector of ones. For the offset, specify a vector of zeros.

scaleFactor = ones(numChannels,1);
offset = zeros(numChannels,1);

% Apply the group normalization operation with three groups using the groupnorm function.

numGroups = 3;
dlY = groupnorm(dlX,numGroups,offset,scaleFactor);

% View the size and format of the output dlY.

size(dlY)
dims(dlY)

% Copyright 2021 The MathWorks, Inc.