Is there any layer defined in matlab for sine activation function? Or else can we define the layer using functionLayer?
3 次查看(过去 30 天)
显示 更早的评论
How we can use a layer with sine activation function? Is it possible to use the functionLayer to define a sine activation function or should I define a class for creating the layer as shown in https://in.mathworks.com/help/deeplearning/ug/define-custom-deep-learning-intermediate-layers.html?
采纳的回答
Matt J
2023-12-1
If it will not have any learnable parameters, you can use a functionLayer.
8 个评论
BIPIN SAMUEL
2023-12-8
I be created a customized layer based on attention mechanism for deep learning application using https://in.mathworks.com/help/deeplearning/ug/define-custom-deep-learning-layer.html. But while checking the validity of layer using the following code:
layer=CoAtten(Name="atten");
validInputSize = [1 14 1024];
layout = networkDataLayout(validInputSize,"CBT");
layer = initialize(layer,layout);
checkLayer(layer,validInputSize,ObservationDimension=3)
The last line (checkLayer) shows the error like this- ""The value of 'Layer' is invalid. Layers that require formatted dlarray inputs are not supported."
What this error shows? Is the custom layer is invalid? I have used stripdims inside the predict fuction while creating the layer. Is it happening due to this?
Matt J
2023-12-8
And why ObservationDimension=3 when you have a Batch dimension that is the second dimension?
BIPIN SAMUEL
2023-12-8
Thank You @Matt J, I have used checkLayer from https://in.mathworks.com/help/deeplearning/ref/checklayer.html for the checking the validity of the layer. I just want to know whether the layer that I have created is correct or not. I have given ObservationDimension=3 to show the size of dlarray input but I am not sure whether it is correct or not.
While using that command "The value of 'Layer' is invalid. Layers that require formatted dlarray inputs are not supported." - this error is showing, but I am not sure that what it represents whether the error is with the layer or with the way I have used the checkLayer(layer,layout,ObservationDimension=3) command. Is there any other way to check the customized layer is working or not?
BIPIN SAMUEL
2023-12-8
编辑:Matt J
2023-12-8
classdef CoAtten < nnet.layer.Layer ...
& nnet.layer.Formattable ...
% & nnet.layer.Acceleratable
% properties
% % (Optional) Layer properties.
%
% % Declare layer properties here.
% end
properties (Learnable)
% (Optional) Layer learnable parameters.
alpha
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 = CoAtten(NameValueArgs)
% (Optional) Create a myLayer.
% This function must have the same name as the class.
arguments
NameValueArgs.Name = '';
end
name=NameValueArgs.Name;
layer.Name=name;
layer.Description="Attention Mechanism based on correlation";
layer.Type="Correlation Attention";
end
function layer = initialize(layer,layout)
% (Optional) Initialize layer learnable and state parameters.
if isempty(layer.alpha)
idx=finddim(layout,"C");
numChannels = layout.Size(idx);
layer.alpha=dlarray(zeros(numChannels));
end
end
function Z = predict(layer,varargin)
% Forward input data through the layer at prediction time and
% output the result and updated state.
%
X=varargin;
filtersize=ones(1);
nc=ones(1);
numfilters=ones(1);
sz=[filtersize,nc,numfilters];
numout=prod(filtersize)*numfilters;
numin=prod(filtersize)*numfilters;
weights=initializeGlorot(sz,numout,numin);
bias=dlarray(zeros(nc));
query=dlconv(X,weights,bias,WeightsFormat='CUT');
weights=initializeGlorot(sz,numout,numin);
key=dlconv(X,weights,bias,WeightsFormat='CUT');
weights=initializeGlorot(sz,numout,numin);
value=dlconv(X,weights,bias,WeightsFormat='CUT');
X=stripdims(X);
query=stripdims(query);
key=stripdims(key);
value=stripdims(value);
X_zscore=zscore(X);
X_transpose=permute(X_zscore,[1 3 2]);
X_flatten=permute(X_transpose,[2 3 1]);
query_flatten=permute(query,[2 4 1 3]);
query_energy=pagemtimes(X_flatten,query_flatten);
query_energy=permute(query_energy, [3 1 4 2]);
key_flatten=permute(key,[2 4 1 3]);
key_energy=pagemtimes(X_flatten,key_flatten);
key_energy=permute(key_energy,[3 1 4 2]);
query_energy=permute(query_energy,[2 3 1]);
key_energy=permute(key_energy,[2 4 1 3]);
energy=pagemtimes(query_energy,key_energy);
energy=permute(energy,[3 1 4 2]);
energy=permute(energy, [2 1 3]);
attention=softmax(energy,'DataFormat',"UCU");
value_flatten=permute(value,[2 3 1]);
out=pagemtimes(value_flatten,attention);
out=permute(out,[2 1 3]);
Alpha=layer.alpha;
Z=Alpha*out+X;
Z=dlarray(Z,"CBT");
end
end
end
This is the layer that I have used for the validity check using "checkLayer".
Matt J
2023-12-8
Does this make any more sense?
layer=CoAtten(Name="atten");
validInputSize = [1 14 1024];
layout = networkDataLayout(validInputSize,"CBT");
layer = initialize(layer,layout);
checkLayer(layer,layout,ObservationDimension=2)
Skipping GPU tests. No compatible GPU device found.
Skipping code generation compatibility tests. To check validity of the layer for code generation, specify the CheckCodegenCompatibility and ObservationDimension options.
Running nnet.checklayer.TestLayerWithoutBackward
..
================================================================================
nnet.checklayer.TestLayerWithoutBackward/formattableLayerPredictIsFormatted(Observations=one) was filtered.
Test Diagnostic: Test did not run because 'predict' threw an error.
================================================================================
.
================================================================================
nnet.checklayer.TestLayerWithoutBackward/formattableLayerPredictIsFormatted(Observations=multiple) was filtered.
Test Diagnostic: Test did not run because 'predict' threw an error.
================================================================================
....
================================================================================
Verification failed in nnet.checklayer.TestLayerWithoutBackward/predictDoesNotError(Observations=one).
----------------
Test Diagnostic:
----------------
Test failure may be due to the layer not being initialized. If the layer is not initialized, then initialize it by calling its initialize method.
---------------------
Framework Diagnostic:
---------------------
The function 'predict' threw an error:
Undefined function 'initializeGlorot' for input arguments of type 'double'.
Error in CoAtten/predict (line 57)
weights=initializeGlorot(sz,numout,numin);
------------------
Stack Information:
------------------
In /MATLAB/toolbox/nnet/cnn/+nnet/+checklayer/TestLayerWithoutBackward.m (TestLayerWithoutBackward.predictDoesNotError) at 26
================================================================================
.
================================================================================
Verification failed in nnet.checklayer.TestLayerWithoutBackward/predictDoesNotError(Observations=multiple).
----------------
Test Diagnostic:
----------------
Test failure may be due to the layer not being initialized. If the layer is not initialized, then initialize it by calling its initialize method.
---------------------
Framework Diagnostic:
---------------------
The function 'predict' threw an error:
Undefined function 'initializeGlorot' for input arguments of type 'double'.
Error in CoAtten/predict (line 57)
weights=initializeGlorot(sz,numout,numin);
------------------
Stack Information:
------------------
In /MATLAB/toolbox/nnet/cnn/+nnet/+checklayer/TestLayerWithoutBackward.m (TestLayerWithoutBackward.predictDoesNotError) at 26
================================================================================
.. ....
================================================================================
nnet.checklayer.TestLayerWithoutBackward/predictIsConsistentInType(Precision=single,Device=cpu) was filtered.
Test Diagnostic: Test did not run because 'predict' threw an error.
================================================================================
.
================================================================================
nnet.checklayer.TestLayerWithoutBackward/predictIsConsistentInType(Precision=double,Device=cpu) was filtered.
Test Diagnostic: Test did not run because 'predict' threw an error.
================================================================================
..... ..
================================================================================
nnet.checklayer.TestLayerWithoutBackward/backwardPropagationDoesNotError(Observations=one) was filtered.
Test Diagnostic: Test did not run because 'predict' threw an error.
================================================================================
.
================================================================================
nnet.checklayer.TestLayerWithoutBackward/backwardPropagationDoesNotError(Observations=multiple) was filtered.
Test Diagnostic: Test did not run because 'predict' threw an error.
================================================================================
.
Done nnet.checklayer.TestLayerWithoutBackward
__________
Failure Summary:
Name Failed Incomplete Reason(s)
=================================================================================================================================================
nnet.checklayer.TestLayerWithoutBackward/formattableLayerPredictIsFormatted(Observations=one) X Filtered by assumption.
-------------------------------------------------------------------------------------------------------------------------------------------------
nnet.checklayer.TestLayerWithoutBackward/formattableLayerPredictIsFormatted(Observations=multiple) X Filtered by assumption.
-------------------------------------------------------------------------------------------------------------------------------------------------
nnet.checklayer.TestLayerWithoutBackward/predictDoesNotError(Observations=one) X Failed by verification.
-------------------------------------------------------------------------------------------------------------------------------------------------
nnet.checklayer.TestLayerWithoutBackward/predictDoesNotError(Observations=multiple) X Failed by verification.
-------------------------------------------------------------------------------------------------------------------------------------------------
nnet.checklayer.TestLayerWithoutBackward/predictIsConsistentInType(Precision=single,Device=cpu) X Filtered by assumption.
-------------------------------------------------------------------------------------------------------------------------------------------------
nnet.checklayer.TestLayerWithoutBackward/predictIsConsistentInType(Precision=double,Device=cpu) X Filtered by assumption.
-------------------------------------------------------------------------------------------------------------------------------------------------
nnet.checklayer.TestLayerWithoutBackward/backwardPropagationDoesNotError(Observations=one) X Filtered by assumption.
-------------------------------------------------------------------------------------------------------------------------------------------------
nnet.checklayer.TestLayerWithoutBackward/backwardPropagationDoesNotError(Observations=multiple) X Filtered by assumption.
Test Summary:
16 Passed, 2 Failed, 6 Incomplete, 10 Skipped.
Time elapsed: 1.3986 seconds.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Build Deep Neural Networks 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)