classdef sseClassificationLayer < nnet.layer.ClassificationLayer
    
    methods
        function layer = sseClassificationLayer(name)
            % layer = sseClassificationLayer creates a sum of squares error
            % classification layer.
            %
            % layer = sseClassificationLayer(name) additionally specifies
            % the layer name.
            
            % Set layer name.
            if nargin == 1
                layer.Name = name;
            end
            
            % Set layer description.
            layer.Description = 'Sum of squares error';
        end
        
        function loss = forwardLoss(layer, Y, T)
            % loss = forwardLoss(layer, Y, T) returns the SSE loss between
            % the predictions Y and the training targets T.
            
            % Calculate sum of sqaures.
            sumSquares = sum((Y-T).^2);
            
            % Take mean over mini-batch.
            N = size(Y,4);
            loss = sum(sumSquares)/N;
        end
    end
end