/**
program description;
This program allows a user to build a generalized delter
rule net for supervised learning.User can specify the
number of input & output units,number of hidden layers
and number of units in each hidden layer.
After the net is built,learning takes place in the net
with a given set of training samples.User specifies
values of the learning rate eta,the momentum rate alpha,
maximum tolerence errors
and
maximum number of iterations.
After learning,all the information relevant
to the
structure of the net,including weights and
thresholds are stored in files.
outputs can be generated for new patterns by
reading
from file and by constructing the net.
Training set samples and additional samples for
processing are stored in files.
**/
#include<ctype.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
#ifndef VAX
/*for declarations of calloc() on PC or compatiable*/
#include <malloc.h>
#endif
/*define constants used thoughout functions*/
#define NMXUNIT 50 /*max no. of units in a layer*/
#define NMXHLR 5/*max no. of hidden layers*/
#define NMXOATTR 4/*max no. of output feathers*/
#define NMXINP 5000/*max no. of input samples*/
#define NMXIATTR 20/*max no. of input feathers*/
#define SEXIT 3/*exit successfully*/
#define RESTART 2/*restart*/
#define FEXIT 1/*exit in failure*/
#define CONTNE 0/*continue calculation*/
/*data base:declarations of variables*/
float eta ; /**learning rate**/
float alpha ; /**momentum rate**/
float maxe ; /**max allowed system error**/
float maxep; /**max allowed pattern error**/
float minep; /*min pattern error*/
float increrror; /*error increment*/
float *wtptr[NMXHLR+1];
float *outptr[NMXHLR+2];
float *errptr[NMXHLR+2];
float *delw[NMXHLR+1];
float target[NMXINP][NMXOATTR];
float input[NMXINP][NMXIATTR],ep[NMXINP];
float output[NMXINP][NMXOATTR];
float lembda,a,c,downlim,uplim;
float err_curr,err_curr_copy;
int nunit[NMXHLR+2],nhlayer,ninput,ninattr,noutattr,nsample;
int result,cnt,cnt_num;
int nsnew,nsold;
char task_name[20];
char wellname[NMXINP][12];
char in_curve_name[NMXIATTR][16],out_curve_name[NMXOATTR][16],wellno[16];
FILE *fp1,*fp2,*fp3,*fopen();
int fplot10;
//function statement
void output_generation(void);
void learning(void);
void user_session(void);
void set_up(void);
void init(void);
void initwt(void);
int random(void);
int rumelhart(int,int);
void forward(int);
float sigmoid(float);
int introspective(int,int);
void dread(char *);
void wtread(char *);
void dwrite(char *);
void wtwrite(char *);
void savedat(void);
/*********************MAIN********************/
void main(void)
{
char select[20],cont[10];
strcpy(task_name,"********");
do
{
printf("\n** Select L(earning) or O(utput generation) **\n");
do
{
scanf("%s",select);
switch(select[0])
{
case 'o':
case 'O':
output_generation();
break;
case 'l':
case 'L':
learning();
default:
printf("\n Answer learning or output generation");
break;
}
}while((select[0]!='o')&&(select[0]!='O')&&(select[0]!='l')&&(select[0]!='L'));
printf("\n Do you want to continue?");
scanf("%s",cont);
}while((cont[0]=='y')||(cont[0]=='Y'));
printf("\n It is all finished .");
printf("\n Good bye!");
}
/* main body of output generation */
void output_generation()
{
int i,j,m;
char ans[10];
char dfile[20];
char data_file_name[20];
FILE *fp1,*fp2;
/* If task is already in the memory,data files for task
do not need to be read in. but, if it is a new task,data
files should be read in to recinstruct the net */
printf("\nGeneration of output for a new pattern");
printf("\n\t Present task name is %s",task_name);
printf("\n\t Work on a different task?");
printf("\n\t Answer yes or no:");
scanf("%s",ans);
if((ans[0]=='y')||(ans[0]=='Y'))
{
printf("\n\t Type the task name :");
scanf("%s",task_name);
dread(task_name);
init();
wtread(task_name);
}
/*input data for
output generation are created*/
printf("\nEnter file name for pattern to ");
printf("be processed:");
scanf("%s",dfile);
strcpy(data_file_name,dfile);
strcpy(data_file_name,dfile);
strcat(data_file_name,"_net.dat");
strcat(dfile,".dat");
if((fp1=fopen(dfile,"r"))==NULL)
{
perror("\nCan't open dfile");
exit(0);
}
if((fp2=fopen(data_file_name,"w"))==NULL)
{
perror("\nCan't open the data file");
exit(0);
}
//fscanf(fp1,"%s",wellno);
//for(i=0;i<ninattr;i++)
// fscanf(fp1,"%s",in_curve_name[i]);
//fprintf(fp2,"%-10s",wellno);
//for(i=0;i<ninattr;i++)
// fprintf(fp2," %-10s",in_curve_name[i]);
//for(i=0;i<noutattr;i++)
// fprintf(fp2," %snet ",out_curve_name[i]);
//fprintf(fp2,"\n");
do
{
// fscanf(fp1,"%s",wellname[0]);
for(m=0;m<ninattr;m++)
fscanf(fp1,"%f",&input[0][m]);
forward(0);
fprintf(fp2,"%-10s",wellname[0]);
for(i=0;i<ninattr;i++)
fprintf(fp2,"%-10.7f",input[0][i]);
for(i=0;i<noutattr;i++)
fprintf(fp2,"%-10.7f",output[0][i]);
fprintf(fp2,"\n");
} while(!feof(fp1));
if(fclose(fp1)!=0)
{
perror("\n Can't close the data file.");
exit(0);
}
if(fclose(fp2)!=0)
{
perror("\nCan't close the data file.");
exit(0);
}
}
/*main body of learning */
void learning()
{
user_session();
set_up();
init();
do{
initwt();
result=rumelhart(0,ninput);
}while(result==RESTART);
if(result==FEXIT)
{
printf("\n Max number of iteration reached,");
printf("\n but failed to decrease system");
printf("\n error sufficiently");
}
dwrite(task_name);
wtwrite(task_name);
}
/* read in the input data file specified by
user during the interactive session */
void user_session()
{
int i,j;
static char showdata;
char fnam[20],name0[6],name[5],dtype[20];
FILE *fp;
printf("\n start of learning session");
/* for task with name task_name,input data file of the task is
automatically set to be task_name. dat by the program */
printf("\n\t Enter the task name:");
scanf("%s",task_name);
printf("\n How many features in input pattern?:");
scanf("%d",&ninattr);
printf("\n How many output units?");
scanf("%d",&noutattr);
printf("\n Total number of input samples?:");
scanf("%d",&ninput);
strcpy(fnam,task_name);
strcat(fnam,".dat");
if((fp=fopen(fnam,"r"))==NULL)
{
printf("\n File %s does not exist",fnam);
exit(0);
}
printf("\n Do you want to look at data just read?");
printf("\n Answer yes or no:");
scanf("%s",dtype);
showdata=((dtype[0]=='y')||(dtype[0]=='Y'));
//fscanf(fp,"%s",wellno);
//if(showdata) printf(" %s ",wellno);
for(i=0;i<ninattr;i++)
{
// fscanf(fp,"%s",in_curve_name[i]);
// if(showdata) printf("%s ",in_curve_name[i]);
}
for(i=0;i<noutattr;i++)
{
// fscanf(fp,"%s",out_curve_name[i]);
// if(showdata) printf("\b\b\b%s",out_curve_name[i]);
}
printf("\n");
for(i=0;i<ninput;i++)
{
//fscanf(fp,"%s",wellname[i]);
//if(showdata) printf("%s ",wellname[i]);
for(j=0;j<ninattr;j++){
fscanf(fp,"%f",&input[i][j]);
if(showdata) printf("%f ",input[i][j]);
}
for(j=0;j<noutattr;j++){
fscanf(fp,"%f",&target[i][j]);
if(showdata) printf("%f \n",target[i][j]);
}
}
if((i=fclose(fp))!=0)
{
printf("\n File cannot be closed %d",i);
exit(0);
}
}
/*specify architecture of net and
values of learning parameters*/
void set_up()
{
int i;
eta=0.9;
//printf("\nLearning rate eta(default=0.9)?:");
//scanf("%f",&eta);
alpha=0.7;
//printf("\nMomentum rate alpha(default=0.7)?:");
//scanf("%f",&alph
评论0