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.
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Ayush Aniket
2024년 5월 15일
The error occurs due to missing input parameters of the 'backwardLoss' method in your custom classification layer. The 'backwardLoss' method should accept the same input arguments as 'forwardLoss', specifically the layer itself, the predictions 'Y', and the training targets 'T'. However, in your definition of backwardLoss, you've omitted 'Y' and 'T' from the parameter list.
The correct definition would be as shown below:
function dLdY = backwardLoss(~, Y, T)
% Backward propagate the derivative of the loss function.
%
% Inputs:
% Y – Predictions made by network
% T – Training targets
%
% Output:
% dLdY - Derivative of the loss with respect to the
% predictions Y
% Calculate the number of observations (assuming the last dimension of Y is the batch size)
N = size(Y, 4);
% Compute the gradient of the loss function
dLdY = 2 * (Y - T) / N;
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Data Workflows에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!