MATLAB: How to pass the extra arguments to the custom layer

Deep Learning Toolbox

I am using the Deep Learning toolbox and I made a custom layer to use a custom loss function.
However, this loss requires 3 arguments: the output of the network, the true values and an extra parameter.
I managed to declare this on the layer but I cannot find a way to pass the third argument when using "trainNetwork".
I have tried:
>> trainNetwork(X,[Y,A]))
and also switching dimensions:
>> trainNetwork(X,[Y;A]) % where A is the extra argument.
In both cases I get an assertion error where in the first case it complains about the length of input and the output must be equal, and when switching dimensions it's the same but now about the size. What can I do?

Best Answer

The parameters of any layer (in this case you custom layer) should be defined when you create the layer array and stored as the layer's property.
For example, if we look at the "examplePreluLayer" example from the following documentation page:
>> edit examplePreluLayer.m
You might notice that there are 2 input arguments that define the layer:
1. numChannel
2. name (an optional 2nd input)
Also notice that "examplePreluLayer" stores a property"Alpha" in a structure:
>> layer.Alpha = rand([1 1 numChannels]);
Now, if we want to use this "examplePreluLayer" in our network, we would define:
>> layers = [ ...
imageInputLayer([28 28 1])
convolution2dLayer(5,20)
batchNormalizationLayer
examplePreluLayer(20)
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
Notice that the line of code above is the step where we would define our layers and pass in any extra parameters to them when necessary.
(e.g. above, I defined "examplePreluLayer" with numChannel = 20)
On the other hand, the "trainNetwork" function would only take the data ("X" & "Y"), array of layers ("layers") that was already defined and options:
>> trainedNet = trainNetwork(X,Y,layers,options)