Hi, How can i perform sensitivity analysis using my trained neural network model?

4 次查看(过去 30 天)
I have already created my neural network, now I want to know the relative significance of each input factors on the output.

回答(1 个)

Ayush Aniket
Ayush Aniket 2025-6-2
There are three ways you can perform sensitvity analysis on the trained neural network:
1. For each input factor, you slightly perturb the input and observe the change in the network’s output. This change gives you an estimate of the local derivative (or sensitivity) of the output with respect to that input. Refer to the following MATLAB answer -https://www.mathworks.com/matlabcentral/answers/194647-how-to-compute-sensitivity-analysis-in-neural-network-model
2. In MATLAB, you directly calculate the partial derivatives of the network output with respect to each input. Refer to the following code snippet which uses a random data on a dummy neural network:
rng(0); % For reproducibility
% Define number of features and samples
numFeatures = 4;
numSamples = 50;
% Example input matrix X (size: numFeatures x numSamples)
X = rand(numFeatures, numSamples);
layers = [
featureInputLayer(numFeatures, 'Name', 'input')
fullyConnectedLayer(10, 'Name', 'fc')
tanhLayer('Name', 'tanh')
fullyConnectedLayer(1, 'Name', 'fc_output')
];
% Create a layer graph from the layers
lgraph = layerGraph(layers);
% Convert the layer graph to a dlnetwork object for custom training and gradient computation
dlnet = dlnetwork(lgraph);
%% 3. Compute Gradient-Based Sensitivity Analysis
% Convert input data to a deep learning array (dlarray)
% 'CB' denotes that the data is arranged as Channel x Batch.
X_dl = dlarray(X, 'CB');
% Compute input gradients using automatic differentiation:
gradMatrix = computeInputGradients(dlnet, X_dl);
% Convert gradients back to numeric data for analysis
gradMatrix = extractdata(gradMatrix);
% Compute the average absolute sensitivity per input factor (across all samples)
avgSensitivity = mean(abs(gradMatrix), 2);
% Normalize to obtain relative significance (so that contributions sum to one)
relativeSignificance = avgSensitivity / sum(avgSensitivity);
% Display Results
disp('Average sensitivity for each input factor:');
Average sensitivity for each input factor:
disp(avgSensitivity);
0.0745 0.7714 0.7680 0.4400
disp('Relative significance (normalized):');
Relative significance (normalized):
disp(relativeSignificance);
0.0363 0.3756 0.3740 0.2142
% Plot relative significance as a bar graph
figure;
bar(relativeSignificance);
xlabel('Input Factor');
ylabel('Relative Significance');
title('Gradient-Based Sensitivity Analysis');
% This function computes the gradient of the network output with respect to the input.
function gradVals = computeInputGradients(net, X_dl)
% Use dlfeval with a helper function to perform automatic differentiation.
% dlfeval evaluates the function and enables gradient tracking.
[~, gradVals] = dlfeval(@(x) forwardPassWithGradient(net, x), X_dl);
end
% This helper function performs the forward pass and computes gradients via dlgradient.
function [output, gradients] = forwardPassWithGradient(net, X_dl)
% Perform the forward pass using the network.
output = forward(net, X_dl); % output has size: 1 x numSamples
% Sum the outputs to obtain a scalar (required by dlgradient)
% This sums over all samples.
loss = sum(output);
% Compute gradients of the loss with respect to the input X_dl.
gradients = dlgradient(loss, X_dl);
end
3. Finally, you can use Garson’s algorithm, which is used to analyze the connection weights to determine the relative contribution of each input neuron to the output. Refer the following MATLAB answer post for steps and code: https://www.mathworks.com/matlabcentral/answers/2064586-how-do-you-code-garson-s-algorithm-in-matlab-to-find-the-relative-importance-of-parameters-when-trai

类别

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