Clear Filters
Clear Filters

Define Custom Deep Learning Layer to compute the amplitude of a complex number

1 view (last 30 days)
Hi,
I want to define a deep learning layer to the amplitude of a complex number.
Assume that we have a complexed n x 1 vector A.
The input to this layer is a 2n x 1 vector [real(A); imag(A)]
I want to compute the amplitude (or more accurately the power) of each entry in A. So the output of this layer should a n x 1 vector.
classdef powerLayer < nnet.layer.Layer % & nnet.layer.Formattable (Optional)
properties
size;
end
methods
function layer = powerLayer(size)
layer.size = size;
end
function Z = predict(layer,X)
% Define layer predict function here.
% X = [real(a); imag(a)], we want to compute the power of every
% entry of a.
% Option 1
% Z = X(1:layer.size).^2 + X(layer.size+1: 2*layer.size).^2;
% Option 2
for i=1:1:layer.size
Z(i) = X(i)^2 + X(layer.size+i)^2;
end
end
end
end
I check the validity of custom layer as follows.
layer = powerLayer(10);
validInputSize = [20];
checkLayer(layer,validInputSize,ObservationDimension=2,CheckCodegenCompatibility=true)
However, I got the error message as follows. In particular, I do not understand what the batch size of output is.
Batch size of output:
[10]
Expected output batch size:
[1]
Running nnet.checklayer.TestLayerWithoutBackward
================================================================================
Verification failed in nnet.checklayer.TestLayerWithoutBackward/codegenPragmaDefinedInClassDef.
---------------------
Framework Diagnostic:
---------------------
Specify '%#codegen' in the class definition of custom layer: powerLayer.
------------------
Stack Information:
------------------
In C:\Program Files\MATLAB\R2022a\toolbox\nnet\cnn\+nnet\+checklayer\IntermediateLayerTestCase.m (IntermediateLayerTestCase.codegenPragmaDefinedInClassDef) at 74
================================================================================
...
================================================================================
Verification failed in nnet.checklayer.TestLayerWithoutBackward/predictSupportsCodegen(Observations=one).
----------------
Test Diagnostic:
----------------
For code generation, 'Z' must have the same batch size as the layer input.
---------------------
Framework Diagnostic:
---------------------
Batch size of generated input data:
[1]
Batch size of output:
[10]
Expected output batch size:
[1]
------------------
Stack Information:
------------------
In C:\Program Files\MATLAB\R2022a\toolbox\nnet\cnn\+nnet\+checklayer\IntermediateLayerTestCase.m (IntermediateLayerTestCase.verifySameBatchSizeForEachZ) at 183
In C:\Program Files\MATLAB\R2022a\toolbox\nnet\cnn\+nnet\+checklayer\TestLayerWithoutBackward.m (TestLayerWithoutBackward.predictSupportsCodegen) at 236
================================================================================
.
================================================================================
Verification failed in nnet.checklayer.TestLayerWithoutBackward/predictSupportsCodegen(Observations=multiple).
----------------
Test Diagnostic:
----------------
For code generation, 'Z' must have the same batch size as the layer input.
---------------------
Framework Diagnostic:
---------------------
Batch size of generated input data:
[2]
Batch size of output:
[10]
Expected output batch size:
[2]
------------------
Stack Information:
------------------
In C:\Program Files\MATLAB\R2022a\toolbox\nnet\cnn\+nnet\+checklayer\IntermediateLayerTestCase.m (IntermediateLayerTestCase.verifySameBatchSizeForEachZ) at 183
In C:\Program Files\MATLAB\R2022a\toolbox\nnet\cnn\+nnet\+checklayer\TestLayerWithoutBackward.m (TestLayerWithoutBackward.predictSupportsCodegen) at 236
================================================================================
...... .......... ...
Done nnet.checklayer.TestLayerWithoutBackward
__________
Failure Summary:
Name Failed Incomplete Reason(s)
=====================================================================================================================================
nnet.checklayer.TestLayerWithoutBackward/codegenPragmaDefinedInClassDef X Failed by verification.
-------------------------------------------------------------------------------------------------------------------------------------
nnet.checklayer.TestLayerWithoutBackward/predictSupportsCodegen(Observations=one) X Failed by verification.
-------------------------------------------------------------------------------------------------------------------------------------
nnet.checklayer.TestLayerWithoutBackward/predictSupportsCodegen(Observations=multiple) X Failed by verification.
Test Summary:
20 Passed, 3 Failed, 0 Incomplete, 5 Skipped.
Time elapsed: 0.20551 seconds.

Answers (1)

Ayush Aniket
Ayush Aniket on 2 Dec 2023
Edited: Ayush Aniket on 2 Dec 2023
Hi Mo,
As per my understanding, you are defining a custom Deep Learning (DL) layer to compute the amplitude of a complex number and getting errors while validating that layer.
The first error message suggests that you need to specify the '%#codegen' directive in the class definition of your custom layer. This directive indicates that the layer is intended to support code generation. You can modify the class definition line as follows:
classdef powerLayer < nnet.layer.Layer %#codegen
You can read more about the steps to define a custom DL layer compatible with Code Generation here:
The second and third error messages are related to the batch size of the output Z. In DL, the term "batch size" refers to the number of observations processed together in one forward pass through the network. The error indicates that the output Z must have the same batch size as the input X. In your current implementation, Z is initialized as an empty variable and then filled in the loop, which does not handle multiple observations (batch processing) correctly. You can modify your code as follows for the correct implementation:
function Z = predict(layer, X)
% Reshape X to a complex number representation
A = X(1:layer.size,:) + 1i * X(layer.size+1:end,:);
% Compute the power of each entry in A
Z = abs(A).^2;
% The batch size is maintained by operating along the second dimension
end
Hope it helps!

Categories

Find more on Deep Learning with GPU Coder in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!