%% Train Network in Parallel with Custom Training Loop
% This example shows how to set up a custom training loop to train a network in parallel. In this example, parallel workers train on portions of the overall mini-batch. If you have a GPU, then training happens on the GPU. During training, a DataQueue object sends training progress information back to the MATLAB client.
%% Load Data Set
% Load the digit data set and create an image datastore for the data set. Split the datastore into training and test datastores in a randomized way. Create an augmentedImageDatastore containing the training data.

digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
    'nndatasets','DigitDataset');
imds = imageDatastore(digitDatasetPath, ...
    'IncludeSubfolders',true, ...
    'LabelSource','foldernames');

[imdsTrain,imdsTest] = splitEachLabel(imds,0.9,"randomized");

inputSize = [28 28 1];
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);

% Determine the different classes in the training set.

classes = categories(imdsTrain.Labels);
numClasses = numel(classes);

%
%% Define Network
% Define your network architecture and make it into a layer graph by using the layerGraph function. This network architecture includes batch normalization layers, which track the mean and variance statistics of the data set. When training in parallel, combine the statistics from all of the workers at the end of each iteration step, to ensure the network state reflects the whole mini-batch. Otherwise, the network state can diverge across the workers. If you are training stateful recurrent neural networks (RNNs), for example, using sequence data that has been split into smaller sequences to train networks containing LSTM or GRU layers, you must also manage the state between the workers.

layers = [
    imageInputLayer([28 28 1],'Name','input','Normalization','none')
    convolution2dLayer(5,20,'Name','conv1')
    batchNormalizationLayer('Name','bn1')
    reluLayer('Name','relu1')
    convolution2dLayer(3,20,'Padding',1,'Name','conv2')
    batchNormalizationLayer('Name','bn2')
    reluLayer('Name','relu2')
    convolution2dLayer(3,20,'Padding',1,'Name','conv3')
    batchNormalizationLayer('Name','bn3')
    reluLayer('Name','relu3')
    fullyConnectedLayer(numClasses,'Name','fc')];

lgraph = layerGraph(layers);

%
% Create a dlnetwork object from the layer graph. dlnetwork objects allow for training with custom loops.

dlnet = dlnetwork(lgraph)

%
%% Set Up Parallel Environment
% Determine if GPUs are available for MATLAB to use with the canUseGPU function.
% If there are GPUs available, then train on the GPUs. Create a parallel pool with as many workers as GPUs.
% If there are no GPUs available, then train on the CPUs. Create a parallel pool with the default number of workers.

if canUseGPU
    executionEnvironment = "gpu";
    numberOfGPUs = gpuDeviceCount("available");
    pool = parpool(numberOfGPUs);
else
    executionEnvironment = "cpu";
    pool = parpool;
end

% Get the number of workers in the parallel pool. Later in this example, you divide the workload according to this number.

N = pool.NumWorkers;

%
%% Train Model
% Specify the training options.

numEpochs = 20;
miniBatchSize = 128;
velocity = [];

% For GPU training, a recommended practice is to scale up the mini-batch size linearly with the number of GPUs, in order to keep the workload on each GPU constant. For more related advice, see Deep Learning with MATLAB on Multiple GPUs.

if executionEnvironment == "gpu"
    miniBatchSize = miniBatchSize .* N
end

% Calculate the mini-batch size for each worker by dividing the overall mini-batch size evenly among the workers. Distribute the remainder across the first workers.

workerMiniBatchSize = floor(miniBatchSize ./ repmat(N,1,N));
remainder = miniBatchSize - sum(workerMiniBatchSize);
workerMiniBatchSize = workerMiniBatchSize + [ones(1,remainder) zeros(1,N-remainder)]

% This network contains batch normalization layers that keep track of the mean and variance of the data the network is trained on. Since each worker processes a portion of each mini-batch during each iteration, the mean and variance must be aggregated across all the workers. Find the indices of the mean and variance state parameters of the batch normalization layers in the network state property.

batchNormLayers = arrayfun(@(l)isa(l,'nnet.cnn.layer.BatchNormalizationLayer'),dlnet.Layers);
batchNormLayersNames = string({dlnet.Layers(batchNormLayers).Name});
state = dlnet.State;
isBatchNormalizationStateMean = ismember(state.Layer,batchNormLayersNames) & state.Parameter == "TrainedMean";
isBatchNormalizationStateVariance = ismember(state.Layer,batchNormLayersNames) & state.Parameter == "TrainedVariance";

% Initialize the training progress plot.

figure
lineLossTrain = animatedline('Color',[0.85 0.325 0.098]);
ylim([0 inf])
xlabel("Iteration")
ylabel("Loss")
grid on

% To send data back from the workers during training, create a DataQueue object. Use afterEach to set up a function, displayTrainingProgress, to call each time a worker sends data. displayTrainingProgress is a supporting function, defined at the end of this example, that displays the training progress information that comes from the workers.

Q = parallel.pool.DataQueue;
displayFcn = @(x) displayTrainingProgress(x,lineLossTrain);
afterEach(Q,displayFcn);

% Train the model using a custom parallel training loop, as detailed in the following steps. To execute the code simultaneously on all the workers, use an spmd block. Within the spmd block, labindex gives the index of the worker currently executing the code.
% Before training, partition the datastore for each worker by using the partition function. Use the partitioned datastore to create a minibatchqueue on each worker. For each mini-batch:
% Use the custom mini-batch preprocessing function preprocessMiniBatch (defined at the end of this example) to normalize the data, convert the labels to one-hot encoded variables, and determine the number of observations in the mini-batch.
% Format the image data with the dimension labels 'SSCB' (spatial, spatial, channel, batch). By default, the minibatchqueue object converts the data to dlarray objects with underlying type single. Do not add a format to the class labels or the number of observations.
% Train on a GPU if one is available. By default, the minibatchqueue object converts each output to a gpuArray if a GPU is available. Using a GPU requires Parallel Computing Toolbox™ and a supported GPU device. For information on supported devices, see GPU Support by Release (Parallel Computing Toolbox).
% For each epoch, reset and shuffle the datastore with the reset and shuffle functions. For each iteration in the epoch:
% Ensure that all workers have data available before beginning processing it in parallel, by performing a global and operation (gop) on the result of the hasdata function.
% Read a mini-batch from the minibatchqueue by using the next function.
% Compute the gradients and the loss of the network on each worker by calling dlfeval on the modelGradients function. The dlfeval function evaluates the helper function modelGradients with automatic differentiation enabled, so modelGradients can compute the gradients with respect to the loss in an automatic way. modelGradients is defined at the end of the example and returns loss and gradients given a network, mini-batch of data, and true labels.
% To obtain the overall loss, aggregate the losses on all workers. This example uses cross entropy for the loss function, and the aggregated loss is the sum of all losses. Before aggregating, normalize each loss by multiplying by the proportion of the overall mini-batch that the worker is working on. Use gplus to add all losses together and replicate the results across workers.
% To aggregate and update the gradients of all workers, use the dlupdate function with the aggregateGradients function. aggregateGradients is a supporting function defined at the end of this example. This function uses gplus to add together and replicate gradients across workers, following normalization according to the proportion of the overall mini-batch that each worker is working on.
% Aggregate the state of the network on all workers using the aggregateState function. aggregateState is a supporting function defined at the end of this example. The batch normalization layers in the network track the mean and variance of the data. Since the complete mini-batch is spread across multiple workers, aggregate the network state after each iteration to compute the mean and variance of the whole minibatch.
% After computing the final gradients, update the network learnable parameters with the sgdmupdate function.
% Send training progress information back to the client by using the send function with the DataQueue. Use only one worker to send data, because all workers have the same loss information. To ensure that data is on the CPU, so that a client machine without a GPU can access it, use gather on the dlarray before sending it.

start = tic;
spmd
    % Reset and shuffle the datastore.
    reset(augimdsTrain);
    augimdsTrain = shuffle(augimdsTrain);

    % Partition datastore.
    workerImds = partition(augimdsTrain,N,labindex);

    % Create minibatchqueue using partitioned datastore on each worker
    workerMbq = minibatchqueue(workerImds,3,...
        "MiniBatchSize",workerMiniBatchSize(labindex),...
        "MiniBatchFcn",@preprocessMiniBatch,...
        "MiniBatchFormat",{'SSCB','',''});

    workerVelocity = velocity;
   
    iteration = 0;
    
    for epoch = 1:numEpochs
        shuffle(workerMbq);
        
        % Loop over mini-batches.
        while gop(@and,hasdata(workerMbq))
            iteration = iteration + 1;
            
            % Read a mini-batch of data.
            [dlworkerX,workerY,workerNumObservations] = next(workerMbq);
            
            % Evaluate the model gradients and loss on the worker.
            [workerGradients,dlworkerLoss,workerState] = dlfeval(@modelGradients,dlnet,dlworkerX,workerY);
            
            % Aggregate the losses on all workers.
            workerNormalizationFactor = workerMiniBatchSize(labindex)./miniBatchSize;
            loss = gplus(workerNormalizationFactor*extractdata(dlworkerLoss));
            
            % Aggregate the network state on all workers
            dlnet.State = aggregateState(workerState,workerNormalizationFactor,...
                isBatchNormalizationStateMean,isBatchNormalizationStateVariance);
            
            % Aggregate the gradients on all workers.
            workerGradients.Value = dlupdate(@aggregateGradients,workerGradients.Value,{workerNormalizationFactor});
            
            % Update the network parameters using the SGDM optimizer.
            [dlnet.Learnables,workerVelocity] = sgdmupdate(dlnet.Learnables,workerGradients,workerVelocity);
        end
        
       % Display training progress information.
       if labindex == 1
           data = [epoch loss iteration toc(start)];
           send(Q,gather(data)); 
       end
    end
end

%
%% Test Model
% After you train the network, you can test its accuracy.
% Load the test images into memory by using readall on the test datastore, concatenate them, and normalize them.

XTest = readall(imdsTest);
XTest = cat(4,XTest{:});
XTest = single(XTest) ./ 255;
YTest = imdsTest.Labels;

% After the training is complete, all workers have the same complete trained network. Retrieve any of them.

dlnetFinal = dlnet{1};

% To classify images using a dlnetwork object, use the predict function on a dlarray.

dlYPredScores = predict(dlnetFinal,dlarray(XTest,'SSCB'));

% From the predicted scores, find the class with the highest score with the max function. Before you do that, extract the data from the dlarray with the extractdata function.

[~,idx] = max(extractdata(dlYPredScores),[],1);
YPred = classes(idx);

% To obtain the classification accuracy of the model, compare the predictions on the test set against the true labels.

accuracy = mean(YPred==YTest)

%
%% Mini Batch Preprocessing Function
% The preprocessMiniBatch function preprocesses a mini-batch of predictors and labels using the following steps:
% Determine the number of observations in the mini-batch
% Preprocess the images using the preprocessMiniBatchPredictors function.
% Extract the label data from the incoming cell array and concatenate into a categorical array along the second dimension.
% One-hot encode the categorical labels into numeric arrays. Encoding into the first dimension produces an encoded array that matches the shape of the network output.

function [X,Y,numObs] = preprocessMiniBatch(XCell,YCell)

numObs = numel(YCell);

% Preprocess predictors.
X = preprocessMiniBatchPredictors(XCell);

% Extract label data from cell and concatenate.
Y = cat(2,YCell{1:end});

% One-hot encode labels.
Y = onehotencode(Y,1);

end

%% Mini-Batch Predictors Preprocessing Function
% The preprocessMiniBatchPredictors function preprocesses a mini-batch of predictors by extracting the image data from the input cell array and concatenate into a numeric array. For grayscale input, concatenating over the fourth dimension adds a third dimension to each image, to use as a singleton channel dimension. The data are then normalized.

function X = preprocessMiniBatchPredictors(XCell)

% Concatenate.
X = cat(4,XCell{1:end});

% Normalize.
X =  X ./ 255;

end

%% Model Gradients Functions
% Define a function, modelGradients, to compute the gradients of the loss with respect to the learnable parameters of the network. This function computes the network outputs for a mini-batch X with forward and softmax and calculates the loss, given the true outputs, using cross entropy. When you call this function with dlfeval, automatic differentiation is enabled, and dlgradient can compute the gradients of the loss with respect to the learnables automatically.

function [dlgradients,dlloss,state] = modelGradients(dlnet,dlX,dlY)
    [dlYPred,state] = forward(dlnet,dlX);
    dlYPred = softmax(dlYPred);
    
    dlloss = crossentropy(dlYPred,dlY);
    dlgradients = dlgradient(dlloss,dlnet.Learnables);
end

%% Display Training Progress Function
% Define a function to display training progress information that comes from the workers. The DataQueue in this example calls this function every time a worker sends data.

function displayTrainingProgress (data,line)
     addpoints(line,double(data(3)),double(data(2)))
     D = duration(0,0,data(4),'Format','hh:mm:ss');
     title("Epoch: " + data(1) + ", Elapsed: " + string(D))
     drawnow
end

%% Aggregate Gradients Function
% Define a function that aggregates the gradients on all workers by adding them together. gplus adds together and replicates all the gradients on the workers. Before adding them together, normalize them by multiplying them by a factor that represents the proportion of the overall mini-batch that the worker is working on. To retrieve the contents of a dlarray, use extractdata.

function gradients = aggregateGradients(dlgradients,factor)
    gradients = extractdata(dlgradients);
    gradients = gplus(factor*gradients);
end

%% Aggregate State Function
% Define a function that aggregates the network state on all workers. The network state contains the trained batch normalization statistics of the data set. Since each worker only sees a portion of the mini-batch, aggregate the network state so that the statistics are representative of the statistics across all the data. For each mini-batch, the combined mean is calculated as a weighted average of the mean across the workers for each iteration. The combined variance is calculated according to the following formula:
% s_c^2 =\frac{1}{M}\sum_{j=1}^N m_j \left\lbrack s_j^2 +{\left({\bar{x} }_j -{\bar{x} }_c \right)}^2 \right\rbrack
% where Nis the total number of workers, Mis the total number of observations in a mini-batch, m_j is the number of observations processed on the jth worker, {\bar{x} }_j and s_j^2 are the mean and variance statistics calculated on that worker, and {\bar{x} }_c is the combined mean across all workers.

function state = aggregateState(state,factor,...
    isBatchNormalizationStateMean,isBatchNormalizationStateVariance)

    stateMeans = state.Value(isBatchNormalizationStateMean);
    stateVariances = state.Value(isBatchNormalizationStateVariance);

    for j = 1:numel(stateMeans)
        meanVal = stateMeans{j};
        varVal = stateVariances{j};
        
        % Calculate combined mean
        combinedMean = gplus(factor*meanVal);
               
        % Calculate combined variance terms to sum
        varTerm = factor.*(varVal + (meanVal - combinedMean).^2);        
        
        % Update state
        stateMeans{j} = combinedMean;
        stateVariances{j} = gplus(varTerm);
    end

    state.Value(isBatchNormalizationStateMean) = stateMeans;
    state.Value(isBatchNormalizationStateVariance) = stateVariances;
end

% Copyright 2021 The MathWorks, Inc.