在 Matlab 中使用 trainbr 函数进行分类

数据挖掘 神经网络 分类 机器学习模型 matlab 表现
2022-03-06 17:56:12

我正在使用Matlab训练一个用于分类的神经网络,我不明白我是否可以使用trainbr训练函数(贝叶斯正则化反向传播)。它使用 MSE 性能度量,但我想使用交叉熵。如果我将交叉熵设置为性能函数,算法会将其设置回 MSE。

另一方面,我无法在此培训中使用验证集,也找不到如何更改它。

代码是:

x = A';
t = y';

% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainbr';  % Scaled conjugate gradient backpropagation.

% Create a Pattern Recognition Network
net = patternnet(hiddenLayerSize,trainFcn);

% Choose Input and Output Pre/Post-Processing Functions
% For a list of all processing functions type: help nnprocess
net.input.processFcns = {'removeconstantrows','mapminmax'};
net.output.processFcns = {'removeconstantrows','mapminmax'};

% Setup Division of Data for Training, Validation, Testing
% For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand';  % Divide data randomly
net.divideMode = 'sample';  % Divide up every sample
net.divideParam.trainRatio = 60/100;
net.divideParam.valRatio = 20/100;
net.divideParam.testRatio = 20/100;

% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
net.performFcn = 'crossentropy';  % Cross-Entropy
net.trainParam.epochs = 5000;

% Choose Plot Functions
% For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
    'plotconfusion', 'plotroc'};

% Train the Network
[net,tr] = train(net,x,t);

% Test the Network
y = net(x);
e = gsubtract(t,y);
net.performParam.regularization = 0; 
performance = perform(net,t,y); 

tind = vec2ind(t);
yind = vec2ind(y);
percentErrors = sum(tind ~= yind)/numel(tind);

% Recalculate Training, Validation and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,y); 
valPerformance = perform(net,valTargets,y);
testPerformance = perform(net,testTargets,y);

谢谢

2个回答

trainbr模式使用贝叶斯正则化反向传播。该方法在1中提出,它提出了一个带有损失函数 的回归问题, 其中是目标,是网络的响应。论文提出添加一个正则化项,导致损失函数的形式为 其中是所有网络权重之和的平方,即两个参数控制两个部分

ED=i=1n(tiai)2
tiaiF
F=βED+αEW
EWEW=i,jwij2αβEDEW:对于,网络将最小化损失,而不是真正试图保持低权重。对于,网络将最小化权重,允许更多错误。实际上,这意味着较大将阻止网络过度拟合,从而以更大的训练误差为代价获得更好的泛化。αβαβα

找到一个训练一个泛化良好但仍然具有低错误率的模型的关键是正确设置这是通过将它们视为随机变量并使用2中介绍的贝叶斯方法找到最佳设置来实现的。(我不会在这里谈论细节,你可以在两篇链接的论文中找到。)最后,论文提出了一个算法,它计算每次训练迭代中的最优这使得该算法的泛化能力非常好,尤其是在存在噪声输入信号的情况下。αβαβ

) 和正则化项 ( )之间的加权和因此,简而言之:您只能将其与 MSE 一起使用,而不能与交叉熵一起使用。您将需要不同的训练算法,您可以在 MATLAB 文档中找到一个列表,此处为。EDEW

参考:

1 FD Foresee 和 MT Hagan:“Gauss-Newton Approximation to Bayesian Learning”,1997 年国际神经网络联合会议论文集,1997 年6 月。DOI:10.1109/ICNN.1997.614194[链接到 PDF]

2 DJC McKay:“贝叶斯插值”,神经计算,1992 年 5 月,卷。4,第 3 期,第 415-447 页。DOI:10.1162/neco.1992.4.3.415[链接到 PDF]

设置大量验证检查(高于最大迭代次数),Matlab 计算验证错误,但不影响收敛。我仍然不知道如何计算交叉熵而不是 MSE。