matlab DRSN:how to cutom my layer?

1 回表示 (過去 30 日間)
BB
BB 2024 年 3 月 23 日
回答済み: Venu 2024 年 4 月 1 日
Has anyone ever built a DRSN with matlab?I have some trouble,I do not know how to customize the need for soft threshold network?I always get an error on my custom layer.
classdef softthresholdingLayer < nnet.layer.Layer % ...
% & nnet.layer.Formattable ... % (Optional)
% & nnet.layer.Acceleratable % (Optional)
properties
% (Optional) Layer properties.
% Declare layer properties here.
end
properties (Learnable)
% (Optional) Layer learnable parameters.
% Declare learnable parameters here.
% thresholding
end
properties (State)
% (Optional) Layer state parameters.
% Declare state parameters here.
end
properties (Learnable, State)
% (Optional) Nested dlnetwork objects with both learnable
% parameters and state parameters.
% Declare nested networks with learnable and state parameters here.
end
methods
function layer = softthresholdingLayer(numInputs,name)
% (Optional) Create a myLayer.
% This function must have the same name as the class.
% Define layer constructor function here.
layer.NumInputs = numInputs;
layer.Name = name;
% layer.Description = "soft thresholding";
% Initialize layer weights.
% layer.Weights = rand(1,1);
end
function Z = predict(layer,vargin)
X = vargin;
% Initialize output
X1 = X{1};
th = X{2};
% x = extractdata(gather(x));
% th = extractdata(gather(th));
Z = 1.*(X1>th) + 0.*(X1<=th & X1>=(-th)) + 1.*(X1<-th);
% Z = dlarray(Z,'SSCB');
end
end
end
softthresholdingLayer/predict:Too many input parameters
Is that because I can't use “predict”?

回答 (1 件)

Venu
Venu 2024 年 4 月 1 日
Hi @BB
The "predict" method in your custom layer should only take two arguments: layer and X. Your implementation attempts to take an unspecified number of input arguments with "vargin", which seems to be a typo for MATLAB's variable input argument "varargin". This is not the correct approach for layer methods in MATLAB's Deep Learning Toolbox. Instead, the input to the layer should be directly passed as the second argument.
function Z = predict(layer, X)
Hope this helps!

カテゴリ

Help Center および File ExchangeCustom Layers についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by