2-Class Problem with patternnet

5 views (last 30 days)
afef
afef on 14 Jun 2017
Edited: afef on 14 Jun 2017
Hi, I have a problem using the NN toolbox a neural network shall be trained to recognize a two class problem. I used the default settings ( dividerand , 10 hidden neurons, divide radio 0.7, 0.15, 0.15) and my input is a 9xn matrix and my target is a 2xn matrix ([1; 0]for class one and [0; 1] for class two for each sample), where n=1012. the ratio of the classes are about 50:50 .this is the confusion matrix
This is the code that i used :
rng('default');
x = patientInputs;
t = patientTargets ;
inputs=mapminmax(x);
targets=t;
size(inputs);
trainFcn = 'trainscg'; % Scaled conjugate gradient backpropagation.
% Create a Pattern Recognition Network
hiddenLayerSize =10;
net = patternnet(hiddenLayerSize);
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};
net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'sample'; % Divide up every sample
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
net.performFcn = 'mse'; % Cross-Entropy
% Choose Plot Functions
% For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
'plotconfusion', 'plotroc'};
net.trainParam.max_fail =55;
net.trainParam.min_grad=1e-10;
net.trainParam.show=10;
net.trainParam.lr=0.01;
net.trainParam.epochs=90;
net.trainParam.goal=0.001;
% Train the Network
[net,tr] = train(net,inputs,targets);
y = net(inputs);
e = gsubtract(targets,y);
performance = perform(net,targets,y)
tind = vec2ind(targets);
yind = vec2ind(y);
percentErrors = sum(tind ~= yind)/numel(tind);
% Recalculate Training, Validation and Test Performance
trainTargets = t .* tr.trainMask{1};
valTargets = t .* tr.valMask{1};
testTargets = t .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,y)
valPerformance = perform(net,valTargets,y)
testPerformance = perform(net,testTargets,y)
% View the Network
view(net)
Can anyone tell me how to solve this problem and please go easy on me because newbie in matla and neural network .
Thanks

Accepted Answer

Greg Heath
Greg Heath on 14 Jun 2017
In order to insure stability with respect to changes in operating conditions I recommend
MINIMIZING THE NUMBER OF HIDDEN NODES
subject to the normalized mean-square-error constraint
NMSE = mse(error)/mean(var(target',1) < 0.01 % i.e., Rsquare >= 0.99
Although I derived this for regression, it works extremely well for classificaton.
I have zillions of posted examples in both the NEWSGROUP and ANSWERS. Try searching using
greg patternnet
Hope this helps.
Thank you for formally accepting my answer
Greg
  1 Comment
afef
afef on 14 Jun 2017
when i tried to minimize the number of hiddrn nodes less than 10 i got bad performance. Conerning the use of NMSE sorry i don't understand why should i use it ? when i was looking for your examples in NEWSGROUP i tried to modify my coe to be like this
x = patientInputs;
t = patientTargets;
N=1012
I=9
O=2
[ I N ] = size(x)
[ O N ] = size(t)
Ntrn = N-2*round(0.15*N) % Ntrn=708
Ntrneq = Ntrn*O % Ntrneq=1416
%For a robust design desire Ntrneq >> Nw or
H=10
Hub = -1+ceil( (Ntrneq-O) / (I+O+1)) % Hub =117
Nw = (I+1)*H+(H+1)*O % Number of unknown weights =122
%H << Hub = -1+ceil( (Ntrneq-O) / (I+O+1))
Ntrials = 10
rng(0)
j=0
for h =round([Hub/10, Hub/2, Hub])
j = j+1
h = h %12
Nw = (I+1)*h+(h+1)*O % Nw=146
Ndof = Ntrneq-Nw % Ndof=1270
net = patternnet(h);
net.divideFcn = 'dividerand'; % 'dividetrain'
for i = 1:Ntrials
net = configure(net,x,t);
[ net tr outputs regerrors ] = train(net,x,t);
assignedclasses = vec2ind(outputs);
classerr = assignedclasses~=trueclasses;
Nerr(i,j) = sum(classerr);
% FrErr = Fraction of Errors (Nerr/N)
[FrErr(i,j),CM,IND,ROC] = confusion(t,outputs);
FN(i,j) = mean(ROC(:,1)); % Fraction of False Negatives
TN(i,j) = mean(ROC(:,2)) ; % Fraction of True Negatives
TP(i,j) = mean(ROC(:,3)); % Fraction of True Positives
end
end
PctErr=100*Nerr/N
But i got this error message Undefined function or variable 'trueclasses'. and the confusion matrix show a bad accuracy so please tell me what should i do?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by