determine input output neural network

9 views (last 30 days)
Syafiq Muhammad
Syafiq Muhammad on 2 May 2012
this is the 1st time i work with the matlab
so anyone can tell me
1. what does this mean by 'scaling the data'
2. what does this mean by 'adjust the target'
3. from the endpart coding below, i can see 4 variable exist, which is TRAINDATA, TRAINTARGET, TESTDATA, TESTTARGET
so my question is, what's the difference between them and also which one is the input & the target?? (or there maybe other variables)
4. how to train, validate & test the network??
%for your information, i am using matlab r2008
%------------------------------------------------------------------
clear all; close all; clc;
nosetrain=xlsread('C:\MATLAB7\work\pengelasan\Ndrink_i_trn.xlsx'); nosetest=xlsread('C:\MATLAB7\work\pengelasan\Ndrink_i_tst.xlsx');
[nrow_trn,ncol_trn]=size(nosetrain); [nrow_tst,ncol_tst]=size(nosetest);
%scaling the data------------------------------------------------
meantrain=mean(nosetrain(:,2:ncol_trn)); stdtrain=std(nosetrain(:,2:ncol_trn)); meantest=mean(nosetest(:,2:ncol_tst)); stdtest=std(nosetest(:,2:ncol_trn));
for i=1:(ncol_trn-1) for j=1:nrow_trn sc_nosetrain(j,i)=(nosetrain(j,(i+1))-meantrain(i))/stdtrain(i); end end
for i=1:(ncol_tst-1) for j=1:nrow_tst sc_nosetest(j,i)=(nosetest(j,i+1)-meantest(i))/stdtest(i); end end
%adjust target-------------------------------------------------------------
for i=1:nrow_trn if (nosetrain(i,1)==1 | nosetrain(i,1)==3 | nosetrain(i,1)==5) nosetrain(i,1)=1;
end
if (nosetrain(i,1)==2 | nosetrain(i,1)==4 | nosetrain(i,1)==6 | nosetrain(i,1)==7)
nosetrain(i,1)=2;
end
if (nosetrain(i,1)==8 | nosetrain(i,1)==9 | nosetrain(i,1)==10 | nosetrain(i,1)==11)
nosetrain(i,1)=3;
end
if (nosetrain(i,1)==12 | nosetrain(i,1)==13 | nosetrain(i,1)==14 | nosetrain(i,1)==15)
nosetrain(i,1)=4;
end
end
for i=1:nrow_tst if (nosetest(i,1)==1 | nosetest(i,1)==3 | nosetest(i,1)==5) nosetest(i,1)=1;
end
if (nosetest(i,1)==2 | nosetest(i,1)==4 | nosetest(i,1)==6 | nosetest(i,1)==7)
nosetest(i,1)=2;
end
if (nosetest(i,1)==8 | nosetest(i,1)==9 | nosetest(i,1)==10 | nosetest(i,1)==11)
nosetest(i,1)=3;
end
if (nosetest(i,1)==12 | nosetest(i,1)==13 | nosetest(i,1)==14 | nosetest(i,1)==15)
nosetest(i,1)=4;
end
end
traindata=[sc_nosetrain]; traintarget=[nosetrain(:,1)]; testdata=[sc_nosetest]; testtarget=[nosetest(:,1)];
%neural network traindata=traindata'; traintarget=traintarget'; testdata=testdata'; testtarget=testtarget'; a=minmax(traindata);
for bil=1:10 bil_H1=2*bil;
net = newff(a,[bil_H1 1],{'tansig' 'purelin'},'trainlm');
%training
net.trainParam.epochs = 500;
net.trainParam.goal = 0;
net = train(net,traindata,traintarget);
%testing
Y1 = sim(net,traindata);
Y2 = sim(net,testdata);
for i=1:nrow_tst
if Y2(i)< 1.5
Y2(i)= 1;
elseif (Y2(i)>= 1.5 & Y2(i)< 2.5)
Y2(i)= 2;
elseif (Y2(i)>= 2.5 & Y2(i)< 3.5)
Y2(i)= 3;
else
Y2(i)= 4;
end
end
%accuracy
mlp=0;
for i=1:nrow_tst
if Y2(i)== testtarget(i)
mlp=mlp+1;
end
end
pmlp=(mlp/nrow_tst)*100;
training=[traintarget;Y1]';
testing=[testtarget;Y2];
training=[traintarget',Y1'];
testing=[testtarget',Y2'];
percentage=[bil_H1;pmlp]
end %

Answers (3)

Greg Heath
Greg Heath on 3 May 2012
% determine input output neural network % Syafiq Muhammad asked about 19 hours ago % Latest activity: Edit by Syafiq Muhammad about 19 hours ago % % this is the 1st time i work with the matlab % so anyone can tell me
% 1. what does this mean by 'scaling the data'
NNETS WORK BEST WHEN THE DATA IS SCALED
help mapstd
doc mapstd
help mapminmax
doc mapminax
% 2. what does this mean by 'adjust the target'
CODE THE TARGET VECTORS
This network is a classifier. Each input vector is a member of a class identified by an integer class index (1: Nclasses). The corresponding target vector is a column from the unit matrix eye(Nclasses). The row position of the 'one' is the class index.
% 3. from the endpart coding below, i can see 4 variable exist, which is % TRAINDATA, TRAINTARGET, TESTDATA, TESTTARGET % % so my question is, what's the difference between them and also which % one is the input & the target?? (or there maybe other variables)
Change "TRAINDATA" to "TRAININPUT", etc. There is no explicit validation data. If there is enough data, and you so desire, you can combine the train and test data then divide into train, val and test subsets.
% 4. how to train, validate & test the network??
See the documentation for your version of the NN Toolbox.
help nnet
ESPECIALLY try to duplicate and understand the demos.
nnet/demos
% for your information, i am using matlab r2008 % %------------------------------------------------------------------
clear all; close all; clc;
1. PUT EACH STATEMENT ON A SINGLE LINE. IF THE CORRESPONDING OUTPUT VOLUME IS SUFFICIENTLY SMALL, REMOVE THE ENDING SEMICOLON WHILE DEBUGGING.
nosetrain=xlsread('C:\MATLAB7\work\pengelasan\Ndrink_i_trn.xlsx');
nosetest=xlsread('C:\MATLAB7\work\pengelasan\Ndrink_i_tst.xlsx');
[nrow_trn,ncol_trn]=size(nosetrain);
[nrow_tst,ncol_tst]=size(nosetest);
2. SEPARATE INPUT AND TARGET MATRICES. MAKE SURE THE MATRICES ARE ORIENTED CORRECTLY FOR THE NNET TBX : TRN/VAL/TST INPUTS SHOULD HAVE THE SAME NUMBER OF ROWS. SIMILARLY FOR TARGET MATRICES
%scaling the data------------------------------------------------
3. STANDARDIZE TRAINING DATA INPUT TO HAVE ROWS WITH ZERO MEANS AND UNITY VARIANCES
meantrain=mean(nosetrain(:,2:ncol_trn));
stdtrain=std(nosetrain(:,2:ncol_trn));
4.DANGER! THIESE ARE COLUMN MEANS AND STDS. BETTER TO ORIENT MATRICES FIRST AND NORMALIZE VARIABLES SECOND.
meantest=mean(nosetest(:,2:ncol_tst));
stdtest=std(nosetest(:,2:ncol_trn));
5. INCORRECT. DELETE LAST TWO COMMANDS: NONTRAINING DATA SHOULD BE NORMALIZED WITH MEANS AND STANDARD DEVIATIONS OF TRAINING DATA
for i=1:(ncol_trn-1)
for j=1:nrow_trn
sc_nosetrain(j,i)=(nosetrain(j,(i+1))-meantrain(i))/stdtrain(i);
end
end
for i=1:(ncol_tst-1)
for j=1:nrow_tst
sc_nosetest(j,i)=(nosetest(j,i+1)-meantest(i))/stdtest(i);
end
end
6. VECTORIZE THE ABOVE LOOPS
%adjust target-------------------------------------------------------------
7. CREATE AND CODE CLASS INDEX R0W VECTORS AND TARGET MATRICES. FOR CLARITY, USE NEW VARIABLE NAMES.
for i=1:nrow_trn
if (nosetrain(i,1)==1 | nosetrain(i,1)==3 | nosetrain(i,1)==5)
nosetrain(i,1)=1;
end
if (nosetrain(i,1)==2 | nosetrain(i,1)==4 | nosetrain(i,1)==6 | nosetrain(i,1)==7)
nosetrain(i,1)=2;
end
if (nosetrain(i,1)==8 | nosetrain(i,1)==9 | nosetrain(i,1)==10 | nosetrain(i,1)==11)
nosetrain(i,1)=3;
end
if (nosetrain(i,1)==12 | nosetrain(i,1)==13 | nosetrain(i,1)==14 | nosetrain(i,1)==15)
nosetrain(i,1)=4;
end
end
for i=1:nrow_tst
if (nosetest(i,1)==1 | nosetest(i,1)==3 | nosetest(i,1)==5)
nosetest(i,1)=1;
end
if (nosetest(i,1)==2 | nosetest(i,1)==4 | nosetest(i,1)==6 | nosetest(i,1)==7)
nosetest(i,1)=2;
end
if (nosetest(i,1)==8 | nosetest(i,1)==9 | nosetest(i,1)==10 | nosetest(i,1)==11)
nosetest(i,1)=3;
end
if (nosetest(i,1)==12 | nosetest(i,1)==13 | nosetest(i,1)==14 | nosetest(i,1)==15)
nosetest(i,1)=4;
end
end
traindata=[sc_nosetrain];
traintarget=[nosetrain(:,1)];
testdata=[sc_nosetest];
testtarget=[nosetest(:,1)];
8. NO. CONVERT CLASS INDEX ROW VECTORS TO TARGET MATRICES USING IND2VEC
%neural network traindata=traindata'; traintarget=traintarget'; testdata=testdata'; testtarget=testtarget'; a=minmax(traindata);
9. WHY ARE THE ABOVE STATEMENTS COMMENTED OUT??... TRANSPOSITION FROM EXCEL ORIENTATION TO NNTBX ORIENTATION SHOULD OCCUR IMMEDIATELY AFTER READING IN THE DATA.
for bil=1:10
bil_H1=2*bil;
net = newff(a,[bil_H1 1],{'tansig' 'purelin'},'trainlm');
%training
net.trainParam.epochs = 500;
net.trainParam.goal = 0;
net = train(net,traindata,traintarget);
%testing
Y1 = sim(net,traindata);
Y2 = sim(net,testdata);
for i=1:nrow_tst
if Y2(i)< 1.5
Y2(i)= 1;
elseif (Y2(i)>= 1.5 & Y2(i)< 2.5)
Y2(i)= 2;
elseif (Y2(i)>= 2.5 & Y2(i)< 3.5)
Y2(i)= 3;
else
Y2(i)= 4;
end
end
10. IF CODED CORRECTLY, CAN GET INDICES FROM VEC2IND(Y1), ETC
I'LL STOP HERE
%accuracy
mlp=0;
for i=1:nrow_tst
if Y2(i)== testtarget(i)
mlp=mlp+1;
end
end
pmlp=(mlp/nrow_tst)*100;
training=[traintarget;Y1]';
testing=[testtarget;Y2];
training=[traintarget',Y1'];
testing=[testtarget',Y2'];
percentage=[bil_H1;pmlp]
end %
HOPE THIS HELPS.
GREG
  1 Comment
Syafiq Muhammad
Syafiq Muhammad on 6 May 2012
ok now i become more and more confusing..
:-(
i think i understand up until the part of where neural network section starts here til the end of program
%neural network
traindata=traindata'; traintarget=traintarget'; testdata=testdata'; testtarget=testtarget'; a=minmax(traindata);
can you make simple explaination line-by-line started from this section until the end of program??

Sign in to comment.


Greg Heath
Greg Heath on 6 May 2012
That's a job for you.
If you have a question on any commands let me know.
You can use the debugger, a very small data set and remove semicolons to see the output from any command.
Hope this helps.
Greg
  3 Comments
Syafiq Muhammad
Syafiq Muhammad on 9 May 2012
one more thing, how to create classification tree based from this coding??
Syafiq Muhammad
Syafiq Muhammad on 9 May 2012
with also regression tree, which is i think the most important thing in this classification right??

Sign in to comment.


Greg Heath
Greg Heath on 10 May 2012
Q1. at initial program code, the nosetrain (& also nosetest) is something that we recall from excel file. is it?? if its right, then why when i command "nosetrain" the value is not the same as the value as in that excel file earlier??
A1. I do not know why you cannot transfer your data correctly. Post this question separately.
Q2: can you explain more about adjust target?? what do you mean by "CREATE AND CODE CLASS INDEX R0W VECTORS AND TARGET MATRICES"
A2. The best way to understand NN classifier results is to use class indices ranging from 1 to c (Number of classes).
The best way for a NN classifier to train is to use columns of the c-dimensional unit matrix as targets. Use the functions IND2VEC and VEC2IND to tranform back and forth.
If you obtain data in another format, the best approach is to transform it. "Adjust" is a misleading characterization.
Q3. one more thing, how to create classification tree based from this coding??
A3. I do not know. Post this question separately.
Q4: with also regression tree, which is i think the most important thing in this classification right??
A4. I do not understand the question. Reword and post with Q3.
Hope this Helps.
Greg

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!