이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Is there any layer defined in matlab for sine activation function? Or else can we define the layer using functionLayer?
조회 수: 2 (최근 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 (한국어)