Modify layer output in custom neural network
14 views (last 30 days)
Show older comments
I want to modify the output of one layer before sending it to the next layer. As an example you can think of output a2(k) in the custom neural network in https://se.mathworks.com/help/nnet/ug/create-and-train-custom-neural-network-architectures.html. Is it possible to apply some function on it before sending to layer 3. Or else is it possible to apply a new input p3(k) at layer 3 whose elements depend on the value of a2(k)?
0 Comments
Answers (1)
Shantanu Dixit
on 29 May 2025
Edited: Shantanu Dixit
on 29 May 2025
Hi Ilias,
If I understood the query correctly, you're trying to apply a custom function between layers in a neural network. You can achieve this using MATLAB's 'functionLayer': https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layer.functionlayer.html which can be used to apply specific function to the layer input.
You can refer to the below example script which defines a simple CNN with an intermediate 'functionLayer' that applies the softsign operation "f(x) = x / (1 + |x|)".
% Network with a custom softsign activation
layers = [
imageInputLayer([28 28 1], 'Name', 'input')
convolution2dLayer(5, 20, 'Name', 'conv')
%%
% custom function
functionLayer(@(X) X ./ (1 + abs(X)), 'Name', 'softsign', 'Description', 'softsign')
%%
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'maxpool')
fullyConnectedLayer(10, 'Name', 'fc')
softmaxLayer('Name', 'softmax')
];
net = dlnetwork(layers);
sampleInput = rand(28, 28, 1, 1);
dlX = dlarray(sampleInput, 'SSCB');
dlY = predict(net, dlX);
disp("Output of the network:");
disp(extractdata(dlY));
Hope this helps!
0 Comments
See Also
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!