如何在 matlab 中编写 Garson 算法来查找训练神经网络模型时参数的相对重要性?

问题描述 投票:0回答:1

我正在进行一项预测分析研究,并遇到了加森算法,但我在为其编写公式时遇到了麻烦。这是我的代码:


%%% Data Inputs
inputs = data(:, 1:4)';
targets = data(:, 5)';

%%% Create a feedforward neural network
net = feedforwardnet([10, 15, 20]);

%%%% Set the number of neurons in each hidden layer
net.layers{1}.size = 10;
net.layers{2}.size = 15;
net.layers{3}.size = 20;

%%%% Change activation functions (e.g., 'tansig' to 'purelin' for output layer)
net.layers{1}.transferFcn = 'tansig'; % Activation function for first hidden layer
net.layers{2}.transferFcn = 'tansig'; % Activation function for second hidden layer
net.layers{3}.transferFcn = 'tansig'; % Activation function for third hidden layer
net.layers{4}.transferFcn = 'purelin'; % Activation function for output layer

%%%% Divide data for training, validation, and testing
net.divideParam.trainRatio = 0.7;
net.divideParam.valRatio = 0.15;
net.divideParam.testRatio = 0.15;

%%%% Train the neural network
[net, tr] = train(net, inputs, targets);

%%%% Calculate Garson's importance
importance = garsonsAlgorithm(net);

%%%% Display feature importances
disp('Feature Importances (Garson''s Algorithm):');
for i = 1:length(importance)
    disp(['Importance for x' num2str(i) ': ' num2str(importance(i))]);
end

Here is the garson's algorithm function that I wrote down:

function importance = garsonsAlgorithm(net)
    %%% Extract connection weights from the neural network
    weights = cell2mat(net.IW);
    
    % Check if there are bias weights
    if isfield(net, 'b')
        bias_weights = cell2mat(net.b);
        weights = [weights; bias_weights];
    end
    
    %%% Calculate the absolute values of the weights
    absolute_weights = abs(weights);
    
    %%% Calculate the importance for each input feature
    importance = sum(absolute_weights, 1);
    
    %%% Normalize importance values
    importance = importance / sum(importance);
end




我想知道这是否正确,因为我是编程新手。另外,当改变加森算法中的输入数量时,输出会有任何变化(相对重要性)吗?

我附上了Garson算法的公式供参考:

Formula for Garson's Algorithm

algorithm matlab machine-learning neural-network
1个回答
0
投票

您对 Garson 算法的实现似乎走在正确的轨道上。加森算法是一种通过检查连接权重来确定神经网络中输入变量的相对重要性的技术。

您的代码从神经网络中提取连接权重并计算每个输入特征的绝对权重,然后对这些权重进行归一化以计算每个特征的重要性。

但是,代码中可能存在与偏差权重处理相关的小问题。在您的实现中,您使用 isfield(net, 'b') 检查是否存在偏差权重。相反,您可以直接检查网络结构中是否存在偏置权重,如下所示:

% Check if there are bias weights
if isfield(net, 'b')
    bias_weights = cell2mat(net.b);
    % Rest of your bias weight processing
end

关于改变 Garson 算法中的输入数量时输出的变化:是的,特征的相对重要性可以根据输入的数量而改变。添加或删除输入可能会改变网络的学习行为以及输入和输出之间的关系,这可能会影响每个输入特征的重要性。每当网络架构或输入维度发生变化时,必须重新运行 Garson 算法,以准确评估特征重要性。

请记住,Garson 算法提供了特定神经网络架构和数据背景下特征重要性的相对度量。因此,对特征重要性的解释应该考虑模型的整个背景和正在解决的问题。

© www.soinside.com 2019 - 2024. All rights reserved.