Art2神经网络源程序_第1页
Art2神经网络源程序_第2页
Art2神经网络源程序_第3页
Art2神经网络源程序_第4页
Art2神经网络源程序_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、'* ADAPTIVE RESONANCE THEORY (ARTN) ETWORK*#include <stdio.h>#include <stdlib.h> #include <string.h> #include <conio.h> #include <math.h>/ DEFINES#define MAXCNEURONS 75/ MAX COMPARISON LAYER NEURONS#define MAXRNEURONS 30/ MAX RECOGNITION LAYER NEURONS#define MAXPATT

2、ERNS 30#define VERBOSE 1/ MAX NUMBER OF PATTERNS IN A TRAINING SETclass ARTNET private:double WbMAXCNEURONSMAXRNEURONS; / Bottom up weight matrixintWtMAXRNEURONSMAXCNEURONS; / Top down weight matrixintInDataMAXPATTERNSMAXCNEURONS;/ Array of input vectors to be/ presented to the networkintNumPatterns

3、;/ Number of input patternsdouble VigilThresh;/ Vigilence threshold valuedouble L;/ ART training const (see text)intM;/ # of neurons in C-layerintN;/ # of neurons in R-layerintXVectMAXCNEURONS;/ Current in vect at C-CVectMAXCNEURONS;/ Output vector from C-layerintBestNeuron;/ Current best R

4、-layer NeuronintReset;/ Active when vigilence has/ disabled someoneintRVectMAXCNEURONS;/ Output vector from R-layerintPVectMAXCNEURONS;/ WeightedOutput vector from R-layerintDisabledMAXRNEURONS;/ Resets way of disqualifying neuronsintTrainedMAXRNEURONS;/ To identify allocated R-NeuronsvoidClearPvect

5、();voidClearDisabled();voidRecoPhase();/ Recognition phasevoidCompPhase();/ Comparison phasevoidSearchPhase();/ Search PhasevoidRunCompLayer();/ Calc comparison layer by 2/3 rulevoidRunRecoLayer();/ Calc recognition layers R-vectvoidRvect2Pvect(int);/ Distribute winners resultintGain1();/ Comp layer

6、 gainintGain2();/ Reco layer gaindouble Vigilence();/ Calc vigilence metricvoidInitWeights();/ Initialize weightsvoidTrain();/ Weight adjustment is done herepublic:ARTNET(void);/ Constructor/initializationsintLoadInVects(char *Fname);/ load all data vectorsvoidRun(int i);/ Run net w/ ith patternvoid

7、ShowWeights();/ display top down and/ bottom up weightsvoidShowInVect();/ Display current input patternvoidShowOutVect();/ P-vector from Reco layer(see text);/ / METHOD DEFINITIONSARTNET:ARTNET()int i;L=2.0;N=MAXRNEURONS;for (i=0; i<N; i+) /Set all neurons to untrained and enabledTrainedi=0;Disab

8、ledi=0; /* endfor */int ARTNET:LoadInVects(char *Fname)FILE *PFILE;int i,j,k;PFILE = fopen(Fname,"r");if (PFILE=NULL) printf("nUnable to open file %sn",Fname);exit(0); fscanf(PFILE,"%d",&NumPatterns); fscanf(PFILE,"%d",&M); fscanf(PFILE,"%lf"

9、,&VigilThresh); for (i=0; i<NumPatterns; i+) for (j=0; j<M; j+) fscanf(PFILE,"%d",&k);/How many patterns/get width of input vector/Read all the pattern data and.InDataij=k; /* endfor */ /* endfor */ InitWeights(); return NumPatterns; int ARTNET:Gain2() int i;for (i=0; i<M;

10、 i+) if (XVecti=1) return 1; /* endfor */ / .save it for later.void ARTNET:Rvect2Pvect(int best) int i;for (i=0; i<M; i+) PVecti= Wtbesti; /* endfor */int ARTNET:Gain1()int i,G;G=Gain2();for (i=0; i<M; i+) if (RVecti=1)return 0; /* endfor */return G;void ARTNET:RunCompLayer()int i,x;for (i=0;

11、i<M; i+) x=XVecti+Gain1()+PVecti;if (x>=2) CVecti=1;else CVecti=0; /* endif */ /* endfor */double ARTNET:Vigilence()int i;double S,K,D;/ count # of 1's in p-vect & x-vectK=0.0;D=0.0;for (i=0; i<M; i+) K+=CVecti;D+=XVecti; /* endfor */S=K/D;return S;void ARTNET:RunRecoLayer()int i,j,

12、k;double NetMAXRNEURONS;int BestNeruon=-1;double NetMax=-1;for (i=0; i<N; i+) /Traverse all R-layer NeuronsNeti=0;for (j=0; j<M; j+) / Do the productNeti +=Wbij*CVectj; /* endfor */if (Neti>NetMax) && (Disabledi=0) /disabled neurons cant win! BestNeuron=i;NetMax=Neti; /* endfor */fo

13、r (k=0; k<N; k+) if (k=BestNeuron)RVectk=1;/ Winner gets 1elseRVectk=0; / lateral inhibition kills the rest /* endfor */void ARTNET:RecoPhase()int i;/First force all R-layer outputs to zerofor (i=0; i<N; i+) RVecti=0; /* endfor */ for (i=0; i<M; i+) PVecti=0; /* endfor */Now Calculate C-lay

14、er outputsRunCompLayer(); /C-vector now has the result RunRecoLayer();/Calc dot prod w/ bot up weight & CRvect2Pvect(BestNeuron);void ARTNET:CompPhase()double S;RunCompLayer();/Cvector<-dif between x & pS=Vigilence();if (S<VigilThresh)Reset=1;RVectBestNeuron=0;DisabledBestNeuron=1;else

15、Reset=0;void ARTNET:SearchPhase() double S;while (Reset) ClearPvect();RunCompLayer(); /Xvect -> CvectRunRecoLayer();/Find a new winner with prev winners disabledRvect2Pvect(BestNeuron); /new pvect based on new winner S=Vigilence(); /calc vigilence for the new guy if (S<VigilThresh) /check if h

16、e did okReset=1; / if not disable him too RVectBestNeuron=0;DisabledBestNeuron=1;elseReset=0;/Current Best neuron is a good winner.Train him /* endwhile */if (BestNeuron!=-1) Train();else /Failed to allocate a neuron for current pattern.printf("Out of neurons in F2n"); /* endif */ClearDisa

17、bled();void ARTNET:ClearDisabled() int i;for (i=0; i<M; i+) Disabledi=0; /* endfor */void ARTNET:ClearPvect() int i;for (i=0; i<M; i+) PVecti=0; /* endfor */void ARTNET:Train()int i,z=0;for (i=0; i<M; i+) z+=CVecti; /* endfor */for (i=0; i<M; i+) WbBestNeuroni=L*CVecti/(L-1+z);WtBestNeur

18、oni=CVecti; /* endfor */TrainedBestNeuron=1;void ARTNET:Run(int tp)int i,j;ClearPvect();for (i=0; i<M; i+) XVecti=InDatatpi; /* endfor */RecoPhase();CompPhase(); SearchPhase();/ Initialize weights/ from R-neuron i/ to C-neuron j/ All init'd to 1/ from C-neuron i / to R-neuron jvoid ARTNET:Ini

19、tWeights() int i,j;double b;for (i=0; i<N; i+) for (j=0; j<M; j+) Wtij= 1; /* endfor */ /* endfor */ b=L/(L-1+M); for (i=0; i<N; i+) for (j=0; j<M; j+) Wbij= b; /* endfor */ /* endfor */void ARTNET:ShowWeights() int i,j;printf("nTop Down weights:n"); for (i=0; i<N; i+) if(Tr

20、ainedi=1) for (j=0; j<M; j+) printf("%d ",Wtij); /* endfor */ printf("n"); /* endif */ /* endfor */ printf("nBottom up weights:n"); for (i=0; i<N; i+) if(Trainedi=1) for (j=0; j<M; j+) printf("%f ",Wbij); /* endfor */ printf("n"); /* endif */ /* endfor */void ARTNET:ShowInVe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论