I was trying to write a custom SoftMax layer, but I had some problems writing buildNet at the end.The error message is as follows:The Abstract class cannot be instantiated.
1 回表示 (過去 30 日間)
古いコメントを表示
classdef mySoftmaxLayer < nnet.layer.Layer
% & nnet.layer.Acceleratable
properties (Learnable)
% Layer learnable parameters
% Scaling coefficients
Weights
end
methods
function layer = mySoftmaxLayer(numInputs,args)
% layer = preluLayer(numChannels) creates a PReLU layer
% with numChannels channels.
%
% layer = preluLayer(numChannels,Name=name) also specifies the
% layer name
%arguments
% numInputs
% args.Name = "";
%end
layer.NumInputs = numInputs;
layer.Name = name;
layer.Description = "My softmax layer";
layer.Weights =zeros;
...
end
%function Z = forward(layer,varargin)
%
%end
function varargout = predict(layer,varargin)
X = varargin;
%Z = varargout;
%X1 = X{1};
%sz = size(X1);
%for n =1:layer.NumInputs
% varargout{n} = zeros(sz,'like',X1);
%end
sz = size(X);
varargout = zeros(sz,'like',X);
for n=1:layer.NumInputs
layer.Weights = layer.Weight + exp(X{n})
end
for n =1:layer.NumInputs
varargout{n} = exp(varargin{n})/layer.Weights;
end
%varargout = Z;
end
end
end
0 件のコメント
回答 (1 件)
Abhaya
2024 年 12 月 18 日
編集済み: Abhaya
2024 年 12 月 29 日
The error ‘The Abstract class cannot be instantiated’ indicates that the 'mySoftmaxLayer' class, which is extending MATLAB nnnet.layer.Layer class, is abstract in nature and can not be instantiated.
In MATLAB R2018a, when creating a custom layer by extending the nnet.layer.Layer class, you are required to implement the two abstract methods: predict and backward. Without defining these methods, the class remains abstract and cannot be instantiated.
To resolve this issue, you need to define both the predict and backward functions within the 'mySoftmaxLayer' class.
Please refer to the example below, which demonstrates how to implement a custom layer.
classdef mySoftmaxLayer < nnet.layer.Layer
% Custom softmax layer that extends nnet.layer.Layer
methods
function layer = mySoftmaxLayer(name)
layer.Name = name;
layer.Description = 'Softmax Layer';
end
function Z = predict(layer, X)
Z = exp(X - max(X, [], 1));
Z = Z ./ sum(Z, 1);
end
function gradients = backward(layer, X, ~, state,)
S = predict(layer, X);
gradients = S .* (1 - S);
end
end
end
To create an instance of the 'mySoftmaxLayer' class, you can exceute following command:
layer = mySoftmaxLayer('softmax')
Feel free to refer to the following documentation for more information on creating a custom deep learning layer:
Hope this resolves the query.
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!