我正在寻找一个在神经网络中应用10倍交叉验证的例子,我需要这个问题的链接答案:Example of 10-fold SVM classification in MATLAB
我想对所有三个类进行分类,而在示例中只考虑了两个类。
编辑:这是我为虹膜示例编写的代码
load fisheriris %# load iris dataset
k=10;
cvFolds = crossvalind('Kfold', species, k); %# get indices of 10-fold CV
net = feedforwardnet(10);
for i = 1:k %# for each fold
testIdx = (cvFolds == i); %# get indices of test instances
trainIdx = ~testIdx; %# get indices training instances
%# train
net = train(net,meas(trainIdx,:)',species(trainIdx)');
%# test
outputs = net(meas(trainIdx,:)');
errors = gsubtract(species(trainIdx)',outputs);
performance = perform(net,species(trainIdx)',outputs)
figure, plotconfusion(species(trainIdx)',outputs)
endmatlab给出的错误:
Error using nntraining.setup>setupPerWorker (line 62)
Targets T{1,1} is not numeric or logical.
Error in nntraining.setup (line 43)
[net,data,tr,err] = setupPerWorker(net,trainFcn,X,Xi,Ai,T,EW,enableConfigure);
Error in network/train (line 335)
[net,data,tr,err] = nntraining.setup(net,net.trainFcn,X,Xi,Ai,T,EW,enableConfigure,isComposite);
Error in Untitled (line 17)
net = train(net,meas(trainIdx,:)',species(trainIdx)');发布于 2016-01-14 07:56:30
只使用MATLAB的crossval函数比手动使用crossvalind要简单得多。由于您只是询问如何从交叉验证中获得测试“分数”,而不是使用它来选择最优的参数,例如隐藏节点的数量,所以您的代码将像下面这样简单:
load fisheriris;
% // Split up species into 3 binary dummy variables
S = unique(species);
O = [];
for s = 1:numel(S)
O(:,end+1) = strcmp(species, S{s});
end
% // Crossvalidation
vals = crossval(@(XTRAIN, YTRAIN, XTEST, YTEST)fun(XTRAIN, YTRAIN, XTEST, YTEST), meas, O);剩下的就是编写函数fun,它接受输入和输出训练和测试集(所有这些都是由crossval函数提供的,所以您不需要担心自己的数据分割),在训练集上训练一个神经网络,在测试集上测试它,然后使用您喜欢的度量输出一个分数。所以就像这样:
function testval = fun(XTRAIN, YTRAIN, XTEST, YTEST)
net = feedforwardnet(10);
net = train(net, XTRAIN', YTRAIN');
yNet = net(XTEST');
%'// find which output (of the three dummy variables) has the highest probability
[~,classNet] = max(yNet',[],2);
%// convert YTEST into a format that can be compared with classNet
[~,classTest] = find(YTEST);
%'// Check the success of the classifier
cp = classperf(classTest, classNet);
testval = cp.CorrectRate; %// replace this with your preferred metric
end我没有神经网络工具箱,所以我恐怕无法测试。但它应该证明这一原则。
https://stackoverflow.com/questions/34724463
复制相似问题