%% Include Custom Classification Output Layer in Network
% You can use a custom output layer in the same way as any other output layer in Deep Learning Toolbox. This section shows how to create and train a network for classification using the custom classification output layer that you created earlier.
% Load the example training data.

[XTrain,YTrain] = digitTrain4DArrayData;

% Define a custom sum-of-squares error classification layer. To create this layer, save the file sseClassificationLayer.m in the current folder. Create an instance of the layer. Create a layer array including the custom classification output layer sseClassificationLayer.

layers = [
    imageInputLayer([28 28 1])
    convolution2dLayer(5,20)
    batchNormalizationLayer
    reluLayer
    fullyConnectedLayer(10)
    softmaxLayer
    sseClassificationLayer('sse')]

% Set the training options and train the network.

options = trainingOptions('sgdm');
net = trainNetwork(XTrain,YTrain,layers,options);

% Evaluate the network performance by making predictions on new data and calculating the accuracy.

[XTest,YTest] = digitTest4DArrayData;
YPred = classify(net, XTest);
accuracy = mean(YTest == YPred)

% Copyright 2018 The MathWorks, Inc.