在 MATLAB 中使用神经网络分类进行 10 折交叉验证的示例 [英] Example of 10-fold cross-validation with Neural network classification in MATLAB

查看:36
本文介绍了在 MATLAB 中使用神经网络分类进行 10 折交叉验证的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个在神经网络中应用 10 倍交叉验证的例子.我需要这个问题的链接答案:MATLAB中10-fold SVM分类示例

I am looking for an example of applying 10-fold cross-validation in neural network.I need something link answer of this question: Example of 10-fold SVM classification in MATLAB

我想对所有 3 个类进行分类,而在示例中只考虑了两个类.

I would like to classify all 3 classes while in the example only two classes were considered.

这是我为 iris 示例编写的代码

here is the code I wrote for iris example

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 given by 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)');

推荐答案

使用 MATLAB 的 crossval 功能,而不是使用 crossvalind 手动完成.由于您只是询问如何从交叉验证中获得测试分数",而不是使用它来选择最佳参数(例如隐藏节点的数量),因此您的代码将像这样简单:

It's a lot simpler to just use MATLAB's crossval function than to do it manually using crossvalind. Since you are just asking how to get the test "score" from cross-validation, as opposed to using it to choose an optimal parameter like for example the number of hidden nodes, your code will be as simple as this:

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 函数提供给它,这样你就不会需要担心自己拆分数据),在训练集上训练神经网络,在测试集上测试它,然后使用您喜欢的指标输出分数.所以是这样的:

All that remains is to write that function fun which takes in input and output training and test sets (all provided to it by the crossval function so you don't need to worry about splitting your data yourself), trains a neural net on the training set, tests it on the test set and then output a score using your preferred metric. So something like this:

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

我没有神经网络工具箱,所以恐怕我无法对此进行测试.但它应该证明这个原则.

I don't have the neural network toolbox so I am unable to test this I'm afraid. But it should demonstrate the principle.

这篇关于在 MATLAB 中使用神经网络分类进行 10 折交叉验证的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆