我正在寻找一个在神经网络中应用10倍交叉验证的例子.我需要这个问题的链接答案:MATLAB中10倍SVM分类的例子
我想对所有3个类进行分类,而在示例中只考虑了两个类.
编辑:这是我为iris示例编写的代码
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) end
matlab给出的错误:
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)');
Dan.. 6
使用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
我没有神经网络工具箱所以我无法测试这个我害怕.但它应该证明这一原则.
使用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
我没有神经网络工具箱所以我无法测试这个我害怕.但它应该证明这一原则.