Clear Filters
Clear Filters

How to view the output of each and every hidden layer in a deep neural network

17 views (last 30 days)
I am using deep neural network with 10 hidden layers and I want to view the output of each and every hidden layer.
When I execute the model I can see only the final result but I am unable to see the output of each and every hidden layer.
could anyone help me to view the output of each hidden layer.

Answers (1)

Shantanu Dixit
Shantanu Dixit on 17 Jul 2024
Hi Prabha,
It is my understanding that you want to view the output of each and every hidden layer in the deep neural network. To view the output at every layer you can use ‘activations’ or minibatchpredict’.
‘activations’ takes in three input arguments namely network (‘SeriesNetwork’), image, layername or number.
net = alexnet; % 'SeriesNetwork'
I = imread('peppers.png'); % Read the image
I = imresize(I, [227 227]);
plot(net)
actLayerPool1 = activations(net,I,"pool1"); %% output corresponding to pool1
disp(size(actLayerPool1));
27 27 96
actLayerConv1 = activations(net,I,"conv1"); %% output corresponding to layer 2 or conv1
disp(size(actLayerConv1));
55 55 96
Alternatively one can obtain the intermediate output maps using ‘minibatchpredict’. Here the network should be initialised be as a ‘dlnetwork’ and the input should include batch dimension (‘SSCB’)
% Define the layers of the network
layers = [
imageInputLayer([28 28 1], Normalization="none", Name="input")
convolution2dLayer(3, 8, Padding="same", Name="conv_1")
reluLayer(Name="relu_1")
fullyConnectedLayer(10, Name="fc")
softmaxLayer(Name="softmax")
];
net = dlnetwork(layers); %% dlnetwork
disp(net);
dlnetwork with properties: Layers: [5x1 nnet.cnn.layer.Layer] Connections: [4x2 table] Learnables: [4x3 table] State: [0x3 table] InputNames: {'input'} OutputNames: {'softmax'} Initialized: 1 View summary with summary.
sampleInput = rand(28, 28, 1);
dlSampleInput = dlarray(sampleInput, 'SSCB');
activations_conv_layer = minibatchpredict(net,dlSampleInput,Outputs="conv_1"); %% corresponding to conv_1
activations_fc = minibatchpredict(net,dlSampleInput,Outputs="fc"); %% corresponding to fully connected layer
Refer to the following MathWorks documentation for better understanding
  1. activations: www.mathworks.com/help/deeplearning/ref/seriesnetwork.activations.html
  2. 'minibatchpredict’: www.mathworks.com/help/deeplearning/ref/minibatchpredict.html

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!