An error occurred while Defining Custom Classification Output Layer:Error using ‘backwardLoss’ in Layer sseClassificationLayer. The function threw an error and could not be executed.
Here is my code:
classdef sseClassificationLayer < nnet.layer.ClassificationLayer
% Example custom classification layer with sum of squares error loss.
methods
function layer = sseClassificationLayer(name)
% layer = sseClassificationLayer(name) creates a sum of squares
% error classification layer and specifies the layer name.
% Set layer name.
layer.Name = name;
% Set layer description.
layer.Description = ‘Sum of squares error’;
end
function loss = forwardLoss(~, Y, T)
% loss = forwardLoss(layer, Y, T) returns the SSE loss between
% the predictions Y and the training targets T.
% Calculate sum of squares.
sumSquares = sum((Y-T).^2);
% Take mean over mini-batch.
N = size(Y,4);
loss = sum(sumSquares)/N;
end
function dLdY = backwardLoss(~, ~, ~)
% (Optional) Backward propagate the derivative of the loss
% function.
%
% Inputs:
% layer – Output layer
% Y – Predictions made by network
% T – Training targets
%
% Output:
% dLdY – Derivative of the loss with respect to the
% predictions Y
% Layer backward loss function goes here.
N = size(Y,4);
dLdY = 2*(Y-T)/N;
end
end
endHere is my code:
classdef sseClassificationLayer < nnet.layer.ClassificationLayer
% Example custom classification layer with sum of squares error loss.
methods
function layer = sseClassificationLayer(name)
% layer = sseClassificationLayer(name) creates a sum of squares
% error classification layer and specifies the layer name.
% Set layer name.
layer.Name = name;
% Set layer description.
layer.Description = ‘Sum of squares error’;
end
function loss = forwardLoss(~, Y, T)
% loss = forwardLoss(layer, Y, T) returns the SSE loss between
% the predictions Y and the training targets T.
% Calculate sum of squares.
sumSquares = sum((Y-T).^2);
% Take mean over mini-batch.
N = size(Y,4);
loss = sum(sumSquares)/N;
end
function dLdY = backwardLoss(~, ~, ~)
% (Optional) Backward propagate the derivative of the loss
% function.
%
% Inputs:
% layer – Output layer
% Y – Predictions made by network
% T – Training targets
%
% Output:
% dLdY – Derivative of the loss with respect to the
% predictions Y
% Layer backward loss function goes here.
N = size(Y,4);
dLdY = 2*(Y-T)/N;
end
end
end Here is my code:
classdef sseClassificationLayer < nnet.layer.ClassificationLayer
% Example custom classification layer with sum of squares error loss.
methods
function layer = sseClassificationLayer(name)
% layer = sseClassificationLayer(name) creates a sum of squares
% error classification layer and specifies the layer name.
% Set layer name.
layer.Name = name;
% Set layer description.
layer.Description = ‘Sum of squares error’;
end
function loss = forwardLoss(~, Y, T)
% loss = forwardLoss(layer, Y, T) returns the SSE loss between
% the predictions Y and the training targets T.
% Calculate sum of squares.
sumSquares = sum((Y-T).^2);
% Take mean over mini-batch.
N = size(Y,4);
loss = sum(sumSquares)/N;
end
function dLdY = backwardLoss(~, ~, ~)
% (Optional) Backward propagate the derivative of the loss
% function.
%
% Inputs:
% layer – Output layer
% Y – Predictions made by network
% T – Training targets
%
% Output:
% dLdY – Derivative of the loss with respect to the
% predictions Y
% Layer backward loss function goes here.
N = size(Y,4);
dLdY = 2*(Y-T)/N;
end
end
end define custom classification output layer MATLAB Answers — New Questions