如何在Matlab中通过“模式网”神经网络使用k折交叉验证?

时间:2020-04-12 12:14:36

标签: matlab neural-network cross-validation k-fold

我正在尝试通过patternnet神经网络使用k倍交叉验证。

inputs1是特征向量,targets1是来自'iris_dataset'的标签向量。并且xtrainxtestytrainytest分别是使用cvpartition函数分割后的训练和测试功能和标签。

步骤如下:

1。首先,创建了模式识别网络(patternnet)。

In the first and second scripts: net = patternnet; 

2。使用cvpartition将数据划分为k折后,将创建两个训练和测试功能以及标签(k=10)。

In the first and second scripts: fold = cvpartition(targets_Vec, 'kfold', kfold);

3。然后,configure命令用于配置网络对象,并初始化网络的权重和偏差。

In the first script: net = configure(net, xtrain', dummyvar(ytrain)'); % xtrain and ytrain are features and labels from step (2).

In the second script: net = configure(net, inputs1, targets1); % inputs1 and targets1 are features and labels before splitting up.

4。初始化参数和超参数后,使用训练数据(通过train()函数对网络进行训练。

In the first script: [net, tr] = train(net, xtrain', dummyvar(ytrain)'); % xtrain and ytrain are features and labels from step (2).

In the second script: [net, tr] = train(net, inputs1, targets1); % inputs1 and targets1 are features and labels before splitting up.

5。最后,使用经过训练的网络(通过net()函数估算目标)。

In the first script: pred = net(xtest'); % testing features from step (2).

In the second script: pred = net(inputs1); 

由于训练和测试功能是使用cvpartition分开的,因此应该使用训练功能及其标签来训练网络,然后再通过测试功能(新数据)对其进行测试。

尽管train()函数用于训练网络,但是它将自己的输入(来自步骤(2)的训练数据和标签)分为训练,验证和测试数据,而来自步骤的原始测试数据(2)仍未使用。

因此,我需要一个用于训练步骤2(训练和验证)中的功能和标签以进行学习的功能,以及另一个用于对新数据进行分类的功能(来自步骤2的测试功能)。

搜索后,我写了两个脚本,我认为第一个脚本不正确,但是我不确定第二个脚本也不正确吗?如何解决?

第一个脚本:

clc; close all; clearvars;
load iris_dataset
max_iter = 10;
kfold = 10;
[inputs, targets] = iris_dataset;  
inputs = inputs';
targets = targets';

targets_Vec= [];
for j = 1 : size(targets, 1)
    if max(targets(j, 1:3) == 1) && find(targets(j, 1:3))==1
        targets_Vec = [targets_Vec; 1];
    elseif max(targets(j, 1:3) == 1) && find(targets(j, 1:3))==2
        targets_Vec = [targets_Vec; 2];
    elseif max(targets(j, 1:3) == 1) && find(targets(j, 1:3))==3
        targets_Vec = [targets_Vec; 3];
    end
end

net = patternnet; ... Create a Pattern Recognition Network 

rng('default');     
... Divide data into k-folds
fold = cvpartition(targets_Vec, 'kfold', kfold);
... Pre
pred2 = []; ytest2 = []; Afold = zeros(kfold,1); 

... Neural network start
for k = 1 : kfold
    ... Call index of training & testing sets
    trainIdx = fold.training(k); testIdx = fold.test(k);
    ... Call training & testing features and labels
    xtrain = inputs(trainIdx,:); ytrain = targets_Vec(trainIdx);
    xtest = inputs(testIdx,:);   ytest = targets_Vec(testIdx);

    ... configure
    net = configure(net, xtrain', dummyvar(ytrain)');

    ... Initialize neural network
    net.layers{1}.name='Hidden Layer 1';
    net.layers{2}.name='Output Layer';
    net.layers{1}.size = 20;
    net.layers{1}.transferFcn = 'tansig'; 
    net.trainFcn = 'trainscg'; 
    net.performFcn = 'crossentropy'; 

    ... Choose Input and Output Pre/Post-Processing Functions
    net.input.processFcns = {'removeconstantrows','mapminmax'};
    net.output.processFcns = {'removeconstantrows','mapminmax'};

    ... Train the Network
    [net, tr] = train(net, xtrain', dummyvar(ytrain)'); 
    ... Estimate the targets using the trained network.(Test)
    pred = net(xtest'); 

    ... Confusion matrix
    [c, cm] = confusion(dummyvar(ytest)',pred);        
    ... Get accuracy for each fold
    Afold(k) = 100*sum(diag(cm))/sum(cm(:));
    ... Store temporary result for each fold
    pred2 = [pred2(1:end,:), pred]; 
    ytest2 = [ytest2(1:end); ytest]; 
end

... Overall confusion matrix
[~,confmat] = confusion(dummyvar(ytest2)', pred2); 
confmat=transpose(confmat);
... Average accuracy over k-folds
acc = mean(Afold);
... Store results 
NN.fold = Afold;
NN.acc = acc; 
NN.con = confmat; 
fprintf('\n Final classification Accuracy (NN): %g %%',acc);  

第二个脚本:

clc; close all; clearvars;
load iris_dataset
max_iter = 10;
kfold = 10;
[inputs1, targets1] = iris_dataset;  
inputs = inputs1';
targets = targets1';

targets_Vec= [];
for j = 1 : size(targets, 1)
    if max(targets(j, 1:3) == 1) && find(targets(j, 1:3))==1
        targets_Vec = [targets_Vec; 1];
    elseif max(targets(j, 1:3) == 1) && find(targets(j, 1:3))==2
        targets_Vec = [targets_Vec; 2];
    elseif max(targets(j, 1:3) == 1) && find(targets(j, 1:3))==3
        targets_Vec = [targets_Vec; 3];
    end
end

net = patternnet; ... Create a Pattern Recognition Network

rng('default');     
... Divide data into k-folds
fold = cvpartition(targets_Vec, 'kfold', kfold);
... Pre
pred2 = []; ytest2 = []; Afold = zeros(kfold,1); 

... Neural network start
for k = 1 : kfold
    ... Call index of training & testing sets
    trainIdx = fold.training(k); testIdx = fold.test(k);
    ... Call training & testing features and labels
    xtrain = inputs(trainIdx,:); ytrain = targets_Vec(trainIdx);
    xtest = inputs(testIdx,:);   ytest = targets_Vec(testIdx);

    ... configure
    net = configure(net, inputs1, targets1);

    trInd = find(trainIdx);   tstInd = find(testIdx);
    net.divideFcn = 'divideind'; 
    net.divideParam.trainInd = trInd;
    net.divideParam.testInd = tstInd;

    ... Initialize neural network
    net.layers{1}.name='Hidden Layer 1';
    net.layers{2}.name='Output Layer';
    net.layers{1}.size = 20;                      
    net.layers{1}.transferFcn = 'tansig'; 
    net.trainFcn = 'trainscg'; 
    net.performFcn = 'crossentropy';

    ... Choose Input and Output Pre/Post-Processing Functions
    net.input.processFcns = {'removeconstantrows','mapminmax'};
    net.output.processFcns = {'removeconstantrows','mapminmax'};

    ... Train the Network
    [net, tr] = train(net, inputs1, targets1);
    pred = net(inputs1); ... Estimate the targets using the trained network (Test) 

    ... Confusion matrix
    [c, cm] = confusion(targets1, pred);
    y = net(inputs1);
    e = gsubtract(targets1, y);
    performance = perform(net, targets1, y);
    tind = vec2ind(targets1);
    yind = vec2ind(y);  

    percentErrors = sum(tind ~= yind)/numel(tind);

    ... Recalculate Training, Validation and Test Performance
    trainTargets = targets1 .* tr.trainMask{1};
%         valTargets = targets1 .* tr.valMask{1};
    testTargets = targets1 .* tr.testMask{1};
    trainPerformance = perform(net, trainTargets, y);
%         valPerformance = perform(net, valTargets, y);
    testPerformance = perform(net, testTargets, y);
    test_Fold(k) = testPerformance;

end

test_Fold_mean = mean(test_Fold);
acc = 100*(1-test_Fold_mean);   
fprintf('\n Final classification Accuracy (NN): %g %%',acc);   

0 个答案:

没有答案