【问题标题】:Example of 10-fold cross-validation with Neural network classification in MATLABMATLAB 中神经网络分类的 10 折交叉验证示例
【发布时间】:2016-04-15 22:26:09
【问题描述】:

我正在寻找在神经网络中应用 10 倍交叉验证的示例。我需要这个问题的链接答案:Example of 10-fold SVM classification in MATLAB

我想对所有 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)');

【问题讨论】:

  • 您需要先提供一些代码。至少展示你如何在没有交叉验证的情况下实现神经网络,以及你试图调整什么参数。也看看这个答案:stackoverflow.com/a/28168462/1011724,所有你需要改变的是fun函数
  • 我没有任何代码。任何像 Iris 这样的数据库都可以。基本上,您发送的答案和问题中的链接都适用于 SVM。但是我不知道如何在 Matlab 中训练和测试 NN 分类器。分类器的任何规范都没有任何重要性
  • 堆栈溢出不是代码编写服务。当您遇到编码问题时,这是一个寻求帮助的论坛。您需要先自己尝试一下!您是否阅读过有关在 MATLAB 中训练神经网络的文档?
  • 是的,当然我自己试过了,我收到了意外的错误答案或错误。
  • 您的错误是因为如果您查看species,您会看到它是一个分类变量(即不是数字或逻辑)。你需要把它分成3个binary dummy variables

标签: matlab machine-learning neural-network classification cross-validation


【解决方案1】:

仅使用 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

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

【讨论】:

  • 感谢您的回答! O(:,end+1) = strcmp(species, S{s}); 中的 O 是什么意思?它给出了一个错误。
  • @WOEITG 哎呀,忘了初始化它,看我的编辑。抱歉,变量名称很差,O 将是您的转换(即转换为虚拟变量)输出
  • 如果懒得自己创建O数组,可以直接用[~,t] = iris_dataset;加载;-)
  • 使用@(a,b,c,d) fun(a,b,c,d) 代替@fun 有效
  • @Dan 我认为应该是net = feedforwardnet(10); 而不仅仅是feedforward
猜你喜欢
  • 2022-06-15
  • 2022-10-15
  • 2020-03-27
  • 2023-04-04
  • 2014-04-20
  • 1970-01-01
  • 2012-05-11
  • 2017-12-26
  • 2021-07-20
相关资源
最近更新 更多