⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lvq_back.c

📁 有SOM、LVQ、ART三種不同方式的類神經網路可以參考的實例
💻 C
字号:
#include  <stdio.h>
#include  <stdlib.h>
#include  <math.h>

#define  Ntest   			4
#define  Ninp	  			2
#define  Nhid    			6
#define  Nout    			2
#define  test_file  		"d:\\tc\\xor.tra"
#define  weight_file 	"d:\\tc\\xor.wei"
#define  recall_file    "d:\\tc\\xor.rec"

void main(void){

 FILE  *fp1,*fp2,*fp3;
 float X[Ninp],T[Nout],Y[Nout];
 float W_xh[Ninp][Nhid];
 float sum;
 int   Itest;
 int   i,j,h;
 int	 hmin,compute_class;
 float min;
 float dist[Nhid];

 /*----- open files -----*/
 fp1=fopen(test_file,"r");
 fp2=fopen(weight_file,"r");
 fp3=fopen(recall_file,"w");
 if(fp1==NULL)
 {
  puts("Test file not exist !!");
  getchar();
  exit(1);
 }

 if(fp2==NULL)
 {
  puts("Weight file not exist !!");
  getchar();
  exit(1);
 }

 /*----- input weight value -----*/
 fseek(fp2,0,0);
  for (h=0;h<Nhid;h++)
    for (i=0;i<Ninp;i++)
      fscanf(fp2,"%f",&W_xh[i][h]);


 fseek(fp1,0,0);

 for (Itest=0;Itest<Ntest;Itest++)
 {
  /* read input X and desired output T */
  for(i=0;i<Ninp;i++)
    fscanf(fp1,"%f",&X[i]);
  for(j=0;j<Nout;j++)
    fscanf(fp1,"%f",&T[j]);

 /*----- comput net[h] -----*/
 for(h=0;h<Nhid;h++)
 {
  sum=0.0;
  for(i=0;i<Ninp;i++)
   sum=sum+(X[i]-W_xh[i][h])*(X[i]-W_xh[i][h]);
  dist[h]=sqrt(sum);
 }

 /*----- find net[h*] -----*/
 min=1.0e+10;
 for (h=0;h<Nhid;h++)
 {
   if (dist[h] < min)
   {
     hmin=h;
     min=dist[h];
   }
 }

 /*----- compute Y -----*/
 for (j=0;j<Nout;j++)
   Y[j]=0.0;

 compute_class=hmin/(Nhid/Nout);
 Y[compute_class]=1.0;

 /*----- print result -----*/
 for (j=0;j<Nout;j++)
   printf("T[%d]=%2.0f  ",j,T[j]);

 for (j=0;j<Nout;j++)
   printf("Y[%d]=%2.0f  ",j,Y[j]);

 for (j=0;j<Nout;j++)
   if (T[j] != Y[j])  printf("*");

 printf("\n");

}/* end of total learning cycle */

 printf("\n");

 /*----- close files -----*/
 fclose(fp1);
 fclose(fp2);
 fclose(fp3);
 getchar();
} /* end of the program */


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -