Neural Network program problem in classification

Hi All
I am using this code to train my network, the problem is , if I give an input that is somehow among the value of the inputs I have chosen to train , it gives the right output , but if I give something out of this range , still the output is in the same range of the targets I have given to the code :
close all, clear all, clc, plt = 0
load('input.txt')
%load input
load ('target.txt')
%normalizing data
input=input';
target=target';
% input = mapstd(input);
% target = mapstd(target);
x=input;
t=target;
% [ x, t ] = simpleclass_dataset;
[ I N ] = size(x) % [ 2 1000 ]
[ O N ] = size(t) % [ 4 1000 ]
%vec2ind Transform vectors to indices. takes an NxM matrix V and returns a 1xM vector of indices
% indicating the position of the largest element in each column of V.
trueclass = vec2ind(t);
class1 = find(trueclass==1);
class2 = find(trueclass==2); %in my example all the largest elements are on the 2nd column
class3 = find(trueclass==3);
class4 = find(trueclass==4);
N1 = length(class1)
N2 = length(class2)
N3 = length(class3)
N4 = length(class4)
x1 = x(:,class1);
x2 = x(:,class2);
x3 = x(:,class3);
x4 = x(:,class4);
plt = plt + 1
hold on
plot(x1(1,:),x1(2,:),'ko')
plot(x2(1,:),x2(2,:),'bo')
plot(x3(1,:),x3(2,:),'ro')
plot(x4(1,:),x4(2,:),'go')
%
% Nw = (I+1)*H+(H+1)*O;
Hub = -1+ceil( (0.7*N*O-O)/(I+O+1)) % 399
Hmax = 40 % Hmax << Hub
dH = 4 % Design ~10 candidate nets
Hmin = 2 % I know 0 and 1 are too small
rng(0) % Allows duplicating the rsults
j=0
for h=Hmin:dH:Hmax
j = j+1
net = patternnet(10);
net = init(net); % Improving Results since we use patternet we should use init
[ net tr y ] = train( net, x, t );
assignedclass = vec2ind(y);
err = assignedclass~=trueclass;
Nerr = sum(err);
PctErr(j,1) = 100*Nerr/N;
end
h = (Hmin:dH:Hmax)'
PctErr = PctErr
I just want to know , according to the graphs of confusion , performance ,and the classes drawn , is the training enough or too much or little ?

 Akzeptierte Antwort

Greg Heath
Greg Heath am 1 Mär. 2015

0 Stimmen

You are confused.
What is the physical problem you are trying to solve?
1. What are the inputs?
2. What are the corresponding outputs?
3. The targets are not unit vectors. Therefore they are not appropriate for use in a standard NN classifier.

7 Kommentare

farzad
farzad am 1 Mär. 2015
Bearbeitet: farzad am 1 Mär. 2015
Dear Professor
My problem is something related to a structure, and If I understood you correctly ,it is not an image processing , so the order of the numbers are as in the files , also , each sample data are written in each row, so to use it in Matlab ,I have to change the rows with columns
the Targets can not be unit vectors in their actual value , and I don't know if one can change it to unit vectors
so , my question is : Can we use Neural Networks , classification , for a Problem with the outputs that are not only 0 and 1 ??
I add that I did not Accept this answer and I do not know how your answer was accepted , but I am not yet convinced and yet not have got my answer , in your previous answer , I put the code according to the MATLAB HELP EXAMPLE , but , my data all go to the same class !!
farzad
farzad am 1 Mär. 2015
for the questions :
1- the inputs are 789 Samples of 8 elements
2- the outputs are 789 Samples of 3 Elements
3- in this case , my question is : Can my problem be solved by NN at all after being transfered to a unit vector, or there is no such possibility and solution to my problem ?
% My problem is something related to a structure,
Uninformative
%and If I understood you correctly ,it is not an image processing ,
When did I imply that?
% so the order of the numbers are as in the files ,
I don't know what you mean.
% also , each sample data are written in each row, so to use it in Matlab ,
% I have to change the rows with columns
OK
% the Targets can not be unit vectors in their actual value , and I don't
% know if one can change it to unit vectors
Then how are you going to design a useful classifier?
% so , my question is : Can we use Neural Networks , classification , for a
% Problem with the outputs that are not only 0 and 1 ??
Yes. Provided you know what you are doing. However, you do not. So, my
advice is
If you have c classes , convert the columns of the target to columns
of eye(c) with the row index of the "1" indicating the class index in {1:c}
% I add that I did not Accept this answer and I do not know how your answer
% was accepted , but I am not yet convinced and yet not have got my answer
OK. I flagged the acceptance (don't know how to remove it).
% , in your previous answer , I put the code according to the MATLAB HELP
% EXAMPLE , but , my data all go to the same class !!
If your targets are not unit vector columns then what you did makes no sense.
%3- in this case , my question is : Can my problem be solved by NN at all
%after being transfered to a unit vector, or there is no such possibility
%and solution to my problem ?
It depends on
(1) the data (2) what you mean by solved ... minimize error rates on nontraining data?
farzad
farzad am 1 Mär. 2015
Dear Professor Heath
the input is some material properties, the output are some contstants and dimensions , so they are not dimensionally related to each other , I really don't know how should I define my problem to help more in understanding the problem.
Still , I have tried to convert the target matrix to the way you have mentioned , if I have understood correctly , and I attached the new files , but no iterations goes on , and this time nothing happens !!!
farzad
farzad am 1 Mär. 2015
by being solved I mean , for example if I give the inputs :
input = [20793635.0022993088 3308122.1690468886 484.8760825559 1622.0869505062 1.9257234725 0.8661640625]
I should get the output (as a test) :
output= [0.0010000000 35.0000000000 0.0600000000]
as you see , the second element is always the largest .
farzad
farzad am 2 Mär. 2015
I have searched all the web , never found a similar example in classification problem , that the indice of the vectors is always one number
farzad
farzad am 3 Mär. 2015
Dear Professor I think I have managed to create to appropriate file , If I am not wrong , but now I have a problem with what that is happening with matlab when starts to train the network , the GRADIENT is NaN , and the performance is to infinity , and not accessible , could you please help me with this problem

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Brendan Hamm
Brendan Hamm am 26 Feb. 2015

2 Stimmen

Just looking at this briefly, you have multiple output classes but only 1 class that is being used for training data. Therefore there is no distinction for the classification to make ... everything is of the same class. If you train me to classify everything I see as a circle, and then give me a square, I classify it as a circle still. You need a better training set or your neural net is pointless.

1 Kommentar

farzad
farzad am 26 Feb. 2015
Thank you dear Brendan , This was Exactly my question , also in my previous questions , thank you , ok , I am searching in the posts , to understand the classification , or find a method , but first I don't really find a good example , for my case , and I don't know the strategy , like the first photo I have inserted , how could it help me ? and if there is any suggestion for distinguishing , please help me , thanks a lot

Melden Sie sich an, um zu kommentieren.

Greg Heath
Greg Heath am 27 Feb. 2015

1 Stimme

The code you are using is for 4 classes.
Revise the code for the number of classes that you have,
For c classes the columns of target are {0,1} c-dimensional unit vectors.

7 Kommentare

farzad
farzad am 27 Feb. 2015
Thank you dear professor heath
Yes I have to change the classes to 8, but my question is how to write that part to not to put all the input in one class, because in the code using the vec2ind leads to assignment of all the inputs to one class
For classification of c classes the columns of the target are columns of eye(c) with the 1 in the row corresponding to the corresponding class index.
If you do not understand that, look at the target matrices for the classification examples in the documentation
help nndatasets
farzad
farzad am 28 Feb. 2015
Dear Professor
the examples only show how the matrices are , but the Target data are already prepared in the simpleclass -dataset
but this does not Answer my question , because I have a target matrix , that I need to know , how to convert it to the {0,1} c-dimensional unit vectors , because the vec2ind(target) ONLY finds the Largest element in each row of the target , that in my case it's the second column in all of the rows , so the trueclass matrix in my case is a row matrix that all the elements are 2 , the code doesn't give me such matrix as you have mentioned
farzad
farzad am 28 Feb. 2015
Bearbeitet: farzad am 28 Feb. 2015
here is the example of classification in nndatasets :
[x,t] = simpleclass_dataset;
plot(x(1,:),x(2,:),'+')
net = patternnet(10);
net = train(net,x,t);
view(net)
y = net(x)
plotconfusion(t,y)
it just loads the simpleclass_dataset , that the {0,1} c-dimensional is already made , I don't know how !!!
Also I don't know how the second line of the above code help us ? what does that plot say ??!!
farzad
farzad am 28 Feb. 2015
Bearbeitet: farzad am 28 Feb. 2015
I have tried the example of nndataset for my case , but yet : it puts all the targets at the same class so they are accumulated in class 2 , according to the confusion matrix , so I think , for my type of data , I need some other comment , or there is a missing comment
close all, clear all, clc, plt = 0
load('input.txt')
%load input
load ('target.txt')
%normalizing data
input=input';
target=target';
% input = mapstd(input);
% target = mapstd(target);
x=input;
t=target;
setdemorandstream(491218382)
net = patternnet(10);
view(net)
[net,tr] = train(net,x,t);
nntraintool
plotperform(tr)
testX = x(:,tr.testInd);
testT = t(:,tr.testInd);
testY = net(testX);
testIndices = vec2ind(testY)
plotconfusion(testT,testY)
[c,cm] = confusion(testT,testY)
fprintf('Percentage Correct Classification : %f%%\n', 100*(1-c));
fprintf('Percentage Incorrect Classification : %f%%\n', 100*c);
plotroc(testT,testY)
my target vector should look like a [0 1 0;0 1 0;0 1 0 ;........;0 1 0] but I see that the lines of the example code , do not do this for my case
farzad
farzad am 28 Feb. 2015
Also , Talking about the first code , there is no difference between the input vector x and x2 at all !!! my question is , if all the data go to the same class , is this correct ?

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 26 Feb. 2015

Kommentiert:

am 3 Mär. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by