Modify layer output in custom neural network

조회 수: 3 (최근 30일)
Ilias
Ilias 2018년 5월 8일
편집: Shantanu Dixit 2025년 5월 29일
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)?

답변 (1개)

Shantanu Dixit
Shantanu Dixit 2025년 5월 29일
편집: Shantanu Dixit 2025년 5월 29일
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:");
Output of the network:
disp(extractdata(dlY));
0.1185 0.0762 0.0980 0.1176 0.1115 0.0760 0.0982 0.0896 0.1035 0.1108
Hope this helps!

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by