訓練程式
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#define INNODENUM 2
#define HIDENODENUM 2
#define OUTNODENUM 1
#define NCYCLE 1000 //traning cycle
#define NTRAIN 4 //traning data number
#define TRAINFILE "xor.tra" //training file
#define WEIFILE "xor.wei" //weight & theta file
#define MSEFILE "xor.mse" //mean square error file
#define eta 0.5 //eta: learning speed
#define alpha 0.2 //alpha: weight adjust speed
int main()
{
FILE *traFile,*weiFile,*mseFile;
float X[INNODENUM],H[HIDENODENUM],Y[OUTNODENUM];
float W_ih[INNODENUM][HIDENODENUM] , W_hy[HIDENODENUM][OUTNODENUM];
float dW_ih[INNODENUM][HIDENODENUM] , dW_hy[HIDENODENUM][OUTNODENUM];
float theta_h[HIDENODENUM],theta_y[OUTNODENUM];
float dtheta_h[HIDENODENUM],dtheta_y[OUTNODENUM];
float delta_h[HIDENODENUM],delta_y[OUTNODENUM];
float T[OUTNODENUM];
float sum ,mse;
int i,j;
int ICYCLE,ITRAIN; //counters for traning and data receiving cycle
int h;
//-----opening files------------
traFile = fopen(TRAINFILE,"r");
weiFile = fopen(WEIFILE,"w");
mseFile = fopen(MSEFILE,"w");
if(traFile == NULL)
{
printf("Can't open file...\n");
exit(-1);
}
//------------------------------
srand((int)time(0));
//-----setting weight & theta by random value-----
for(i = 0 ; i < INNODENUM ; i++)
{
for(h = 0 ; h < HIDENODENUM ; h++)
{
W_ih[i][h] = rand()/((RAND_MAX +1.0)-0.5);
dW_ih[i][h] = 0;
}
}
for(h = 0 ; h < HIDENODENUM ; h++)
{
for(j = 0 ; j < OUTNODENUM ; j++)
{
W_hy[h][j] = rand()/((RAND_MAX +1.0)-0.5);
dW_hy[h][j] = 0;
}
}
for(h = 0 ; h < HIDENODENUM;h++)
{
theta_h[h] = 0;
dtheta_h[h] = 0;
delta_h[h] = 0;
}
for(j = 0 ; j < OUTNODENUM ; j++)
{
theta_y[j] = 0;
dtheta_y[j] = 0;
delta_y[j] = 0;
}
//------------------------------------------------
//----Start traning---------------
for(ICYCLE = 0 ; ICYCLE < NCYCLE ; ICYCLE++)
{
mse = 0.0 ;
//-----input traning example--
fseek(traFile,0,0);
for(ITRAIN = 0; ITRAIN < NTRAIN ; ITRAIN++)
{
for( i = 0 ; i < INNODENUM ; i++)
{
fscanf(traFile,"%f",&X[i]);
}
for( j = 0 ; j < OUTNODENUM; j++)
{
fscanf(traFile,"%f",&T[j]);
}
//-------Compute H,Y-----------------
for(h = 0 ; h < HIDENODENUM ; h++)
{
sum = 0.0;
for(i = 0 ; i < INNODENUM ;i++)
{
sum += X[i] * W_ih[i][h];
}
H[h] = (float) (1.0/(1.0+exp(-1.0*(sum - theta_h[h]))));
}
for(j = 0 ; j < OUTNODENUM ; j++)
{
sum = 0.0;
for(h = 0 ; h < HIDENODENUM ; h++)
{
sum += H[h] * W_hy[h][j];
}
Y[j] = (float)(1.0/ (1.0+exp(-1.0*(sum - theta_y[j]))));
//printf("Y[%d] = %f\n",j,Y[j]);
}
//--------Compute delta---------------
for(j = 0 ; j < OUTNODENUM ; j++)
{
delta_y[j] = Y[j] * (1.0-Y[j]) * (T[j]-Y[j]);
}
for(h= 0 ; h < HIDENODENUM ; h++)
{
sum = 0.0 ;
for(j = 0 ; j < OUTNODENUM ; j++)
{
sum += W_hy[h][j] *delta_y[j];
}
delta_h[h] = H[h]*(1.0-H[h])*sum;
}
//-------Compute dW dtheta---------
for(j = 0 ; j < OUTNODENUM; j++)
{
for(h = 0 ; h < HIDENODENUM; h++)
{
dW_hy[h][j] = eta * delta_y[j]*H[h] + alpha* dW_hy[h][j];
}
}
for(j = 0 ; j < OUTNODENUM; j++)
dtheta_y[j] = -eta* delta_y[j] + alpha* dtheta_y[j];
for(h = 0 ; h < HIDENODENUM; h++)
{
for(i = 0 ; i < INNODENUM; i++)
{
dW_ih[i][h] = eta * delta_h[h]*X[i] + alpha* dW_ih[i][h];
}
}
for(h = 0 ; h < HIDENODENUM; h++)
dtheta_h[h] = -eta* delta_h[h] + alpha* dtheta_h[h];
//-------Compute new WQ---------
for(j =0 ; j < OUTNODENUM ; j++)
{
for(h = 0 ; h < HIDENODENUM ; h++)
W_hy[h][j] += dW_hy[h][j];
}
for(j = 0 ; j < OUTNODENUM ; j++)
{
theta_y[j] += dtheta_y[j];
}
for(h =0 ; h < HIDENODENUM ; h++)
{
for(i = 0 ; i < INNODENUM ; i++)
W_ih[i][h] += dW_ih[i][h];
}
for(h = 0 ; h < INNODENUM ; h++)
{
theta_h[h] += dtheta_h[h];
}
for(j = 0 ; j < OUTNODENUM ; j++)
{
mse += (T[j]-Y[j])*(T[j] - Y[j]);
//printf("Y[%d] = %f\n",j,Y[j]);
}
}//------end of one learning cycle---------------------
mse = mse /(float) NTRAIN;
if(ICYCLE % 10 == 9)
{
printf("ICYCLE= %d , mse = %f\n",ICYCLE,mse);
fprintf(mseFile,"%d %f\n",ICYCLE,mse);
}
}
//------end of traning-------------------
//Write weight and theta to file-------
printf("\n");
for(h = 0 ; h< HIDENODENUM ;h++)
{
for(i = 0 ; i < INNODENUM ; i++)
{
printf("W_ih[%d][%d] = %f ",i,h,W_ih[i][h]);
fprintf(weiFile,"%f ",W_ih[i][h]);
}
printf("\n");
fprintf(weiFile,"\n");
}
printf("\n");
fprintf(weiFile,"\n");
for(j = 0 ; j< OUTNODENUM ;j++)
{
for(h = 0 ; h < HIDENODENUM ; h++)
{
printf("W_hy[%d][%d] = %f ",h,j,W_hy[h][j]);
fprintf(weiFile,"%f ",W_hy[h][j]);
}
printf("\n");
fprintf(weiFile,"\n");
}
printf("\n");
fprintf(weiFile,"\n");
for(h = 0 ; h < HIDENODENUM ; h++)
{
printf("theta_h[%d] = %f ",h,theta_h[h]);
fprintf(weiFile,"%f ",theta_h[h]);
}
printf("\n\n");
fprintf(weiFile,"\n\n");
for(j = 0 ; j < OUTNODENUM ; j++)
{
printf("theta_y[%d] = %f ",j,theta_y[j]);
fprintf(weiFile,"%f ",theta_y[j]);
}
printf("\n\n");
fprintf(weiFile,"\n\n");
fclose(traFile);
fclose(weiFile);
fclose(mseFile);
return 0;
}
回想程式
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define WEIFILE "xor.wei"
#define INNODENUM 2
#define HIDENODENUM 2
#define OUTNODENUM 1
float W_ih[INNODENUM][HIDENODENUM];
float W_hy[HIDENODENUM][OUTNODENUM];
float theta_h[HIDENODENUM];
float theta_y[OUTNODENUM];
int main()
{
FILE *weiFile;
int i,j,h;
float sum = 0.0;
char ch = '0';
float X[INNODENUM],H[HIDENODENUM],Y[OUTNODENUM];
weiFile = fopen(WEIFILE,"r");
fseek(weiFile,0,0);
if(weiFile == NULL)
{
perror("Can't open file\n");
exit(-1);
}
for(h = 0 ; h < HIDENODENUM ; h++)
{
for(i = 0 ; i < INNODENUM ; i++)
fscanf(weiFile,"%f",&W_ih[i][h]);
}
for(j = 0 ; j < OUTNODENUM ; j++)
{
for(h = 0 ; h < HIDENODENUM ; h++)
fscanf(weiFile,"%f",&W_hy[h][j]);
}
for(h = 0 ; h < HIDENODENUM ; h++)
{
fscanf(weiFile,"%f",&theta_h[h]);
}
for(j = 0 ; j < OUTNODENUM ; j++)
{
fscanf(weiFile,"%f",&theta_y[j]);
}
do
{
for(i = 0 ; i < INNODENUM ; i++)
{
printf("Please input x[%d]: ",i);
scanf("%f",&X[i]);
}
for(h= 0 ; h < HIDENODENUM ; h++)
{
sum = 0.0;
for(i = 0 ; i < INNODENUM ; i++)
sum = sum+X[i] * W_ih[i][h];
H[h] = (float)1.0 / (1.0 + exp(-1.0*(sum - theta_h[h])));
}
for(j= 0 ; j < OUTNODENUM ; j++)
{
sum = 0.0;
for(h = 0 ; h < HIDENODENUM ; h++)
sum = sum+ H[h] * W_hy[h][j];
Y[j] = (float)1.0 / (1.0 + exp(-1.0*(sum - theta_y[j])));
}
printf("Recall result is: \n");
for(j = 0 ; j < OUTNODENUM ; j++)
{
printf("Y[%d] = %f\t",j,Y[j]);
}
printf("\n");
printf("Wanna quit?(y/n):");
scanf("%c",&ch);
//system("pause");
}while(ch != 'q');
return 0;
}
訓練檔
1.0 1.0 0.0
1.0 0.0 1.0
0.0 1.0 1.0
0.0 0.0 0.0
留言列表