基于MATLAB的BR神经网络交叉验证实践
在做项目需要对BR神经网络进行K-Flod交叉验证并最终得到结果,于是写了这个代码。但是,有疑问的一点是这行代码:
net1 = feedforwardnet(3, ‘trainbr’);
不确定“net1”应该放在“for”循环的外面还是里面,放在不同的位置会得到不同的结果。
clear
load allData.mat;
indices = crossvalind('KFold',200,5);
net1 = feedforwardnet(5, 'trainbr');
k=0;
record = zeros(1,5);
record2 = zeros(1,5);
record3 = zeros(1,5);
for i=1:5
k = k+1;
test1=(indices==k); %结果为逻辑值,每次循环选取一个组作为测试集
t_rain1=~test1; %取test的补集即为训练集
Train_Data=allData(t_rain1,:); %提取训练集数据
TestData=allData(test1,:); %提取测试集数据
Feature = Train_Data(:,1:13);
test_Feature = TestData(:,1:13);
DPP = Train_Data(:,14);
test_DPP = TestData(:,14);
Feature = Feature';
test_Feature = test_Feature';
DPP = DPP';
test_DPP = test_DPP';
net1.divideFcn = 'dividerand';
net1.divideParam.testRatio = 0;
net1.divideParam.valRatio = 0;
[nets,tr] = train(net1, Feature, DPP);
predict = nets(test_Feature);
perf = mse(nets,test_DPP,predict);
record(i) = perf;
end
mean_mse = mean(record);
rmse = sqrt(mean_mse);
交叉验证的部分代码参考了 沙子在流 的博文。
引用链接: https://blog.csdn.net/weixin_38672958/article/details/116453688?spm=1001.2014.3001.5501
|