Re: Matlab LVQ Help
- From: Greg Heath <heath@xxxxxxxxxxxxxxxx>
- Date: Sat, 19 Jan 2008 09:48:03 -0800 (PST)
On Jan 18, 1:57 pm, Greg Heath <he...@xxxxxxxxxxxxxxxx> wrote:
On Jan 16, 11:47 am, rahil_1722 <rahil1...@xxxxxxxxx> wrote:
Hi,
I am new on neural network toolbox. I am trying to useLVQalgorithm,but it is not working at all.
Right now I am trying to train the network for a simple sine curve,
but it is not trained according to the target.
Can you help me with your suggestion,
my function is
y=sin(x);
x is my input and y is my target output.
I classified target results in four different classes.
0.5<=value<=1.0; class 1
0.0<=value<0.5; class 2
-0.5<=value<0.0; class 3
-1<=value<-0.5; class 4
I would highly appriciate if you help me with some idea, i am stucked here for a long time.
Here is the code I am using,
---------------------------------------------------
% NOTE1. You MUST clear the workspace
% NOTE2. Initialize RNGs INTEGER state (for run duplication)
close all, clear, clc
STATE = 0
rand('state',STATE)
randn('state',STATE)
t=-5:.1:5;
x=t;
for i=1:length(x)
y(i)=sin(x(i));
end
VECTORIZE!
y = sin(x);
for i=1:1:length(y)
if(0.5<(y(1,i)) && (y(1,i))<=1)
tc1(1,i)=1;
end
end
for i=1:1:length(y)
if(0<=(y(1,i)) && (y(1,i))<0.5)
tc1(1,i)=2;
end
end
for i=1:1:length(y)
if(-0.5<=(y(1,i)) && (y(1,i))<0)
tc1(1,i)=3;
end
end
for i=1:1:length(y)
if(-1.0<=(y(1,i)) && (y(1,i))<-0.5)
tc1(1,i)=4;
end
end
For 1-D matrices only need one index: y(i) and tc(i)
For simpler logic
help elseif
help else
p1=x;
check(1,:)=p1;
check(2,:)=tc1;
checkall=sortrows(check,2);
p=checkall(1,:);
tc=checkall(2,:);
% NOTE3. t already defined as -5:0.1:5
t=ind2vec(tc);
targets=full(t);
% NOTE4. Kohonen, the creator ofLVQ, recommends that
% the number of hidden nodes be determined by a clustering
% algorithm such as his SOM (AKA SOFM).
% In this simple 1-D example we see from plot(x,tc) that the
% optimal value is
%
% sc = 10
sc=16;
calu(1,1)=sum(targets(1,:));
calu(2,1)=sum(targets(2,:));
calu(3,1)=sum(targets(3,:));
calu(4,1)=sum(targets(4,:));
c1=calu(1,1)/(sum(calu(1,1)+calu(2,1)+calu(3,1)+calu(4,1)));
c2=calu(2,1)/(sum(calu(1,1)+calu(2,1)+calu(3,1)+calu(4,1)));
c3=calu(3,1)/(sum(calu(1,1)+calu(2,1)+calu(3,1)+calu(4,1)));
c4=calu(4,1)/(sum(calu(1,1)+calu(2,1)+calu(3,1)+calu(4,1)));
Yuck. VECTORIZE!
pc=[c1 c2 c3 c4];
net=newlvq(minmax(p),sc,pc);
% NOTE5. Quote from help NEWLVQ:
% "LEARNLV2 should only be used to finish training
% of networks already trained with LEARNLV1."
% LEARNV1 is default learning algorithm
net.inputWeights{1,1}.learnFcn='learnlv2';
% net.inputWeights{1,1}.learnFcn='learnlv1';
Since LEARNLV1 is the default, just eliminate the
command.
%----------training------------
% NOTE6. epochmax = 250 is more than sufficient
% NOTE7. default lr = 0.01
net.trainParam.epochs=1000;
net.trainParam.lr=0.05;
net=init(net);
%NOTE8: Weights are automatically initialized to midpoint
% values of p in the newlvq command. You can check if init
% does anything different or better by using IW = net.IW
IW = cell2mat(net.IW)
LW = cell2mat(net.LW)
% before and after net = init(net)
% NOTE9. Kohonen, the creator ofLVQ, recommends that
% the number of hidden nodes AND the initial weights be
% determined by a clustering algorithm such as his SOM
% (AKA SOFM).
% In this simple 1-D example you can see from plot(x,tc)
% what good values for IW0 should be. Then initialize
% via
%
% net.IW = IW0;
This syntax doesn't work. I'll post a new thread to find out
how to do it.
net=train(net,p,targets);
%NOTE10. Get warning from LEARNV2
% Warning: Divide by zero.
% (Type "warning off MATLAB:divideByZero" to suppress this warning.)
% > In C:\MATLAB6p5p1\toolbox\nnet\nnet\learnlv2.m at line 164
% In C:\DOCUME~1\Gregory\LOCALS~1% \Temp\matlab_nnet\tp215164.m at
line 78
% In C:\MATLAB6p5p1\toolbox\nnet\nnet\trainr.m at line 164
% In C:\MATLAB6p5p1\toolbox\nnet\nnet\@network\train.m at line 278
% In C:\MATLAB6p5p1\work\lvqexample0.m at line 109
y_output=sim(net,p);
yc=vec2ind(y_output);
%NOTE11. Added diagnostics
%NOTE12. MSEc = 2*MSE only if errors are between
% classes with indices differing by unity
err = targets-y_output;
MSE = mse(err) % 0.1287
Nerr = sum(sum(abs(err))/2) % 26
Ntrn = length(p) % 101
pcterr = 100*Nerr/Ntrn % 25.7426
ec = tc-yc;
MSEc = mse(ec) % 0.2574
summary = [(1:Ntrn)' tc' yc' ec']
NOTE13. See my May 23 2007 post by searching with
greg-heathlvqglossary
Need spaces
greg-heath lvq glossary
Hope this helps.
Greg
.
- Follow-Ups:
- Re: Matlab LVQ Help
- From: Greg Heath
- Re: Matlab LVQ Help
- References:
- Matlab LVQ Help
- From: rahil_1722
- Re: Matlab LVQ Help
- From: Greg Heath
- Matlab LVQ Help
- Prev by Date: Re: Regression help please
- Next by Date: How to use mat2cell
- Previous by thread: Re: Matlab LVQ Help
- Next by thread: Re: Matlab LVQ Help
- Index(es):
Relevant Pages
|