在 MATLAB 中使用神经网络分类进行 10 折交叉验证的示例

Example of 10-fold cross-validation with Neural network classification in MATLAB

我正在寻找在神经网络中应用 10 折交叉验证的示例 network.I 需要一些东西 link 这个问题的答案:Example of 10-fold SVM classification in MATLAB

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

编辑:这是我为鸢尾花示例编写的代码

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

使用 MATLAB 的 crossval 函数比使用 crossvalind 手动完成要简单得多。由于您只是询问如何从 cross-validation 获取测试 "score",而不是使用它来选择最佳参数,例如隐藏节点的数量,因此您的代码将如此简单:

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

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