A website for better software and better life in Internet! MY WEB Chinese URLs Greeting Creator Input Tips Everyday Source Code Java Applet Games Visit webpage in VC M3U Music List Editor Input Tips everyday Combox in HTML Self-made Screen Saver Model of Neural Network Display Chinese Characters Mouse in Maze, a classic game in math Serial Port Communication

Source Code: A simple Model of Neurone

## A simple Model of Neurone Network

Here is source code of a Neurone. You can know the principle of Neuron Network after you understand it. It is basically a complete Neuron Network because it includes Back-Propogation training Algorithm though it's so short.

Function menu() is to show a menu, including Train, Use, and Demo.
The weight values (AND1,AND2, OR1,OR2) in Demo are generated from training.

```
/*Neurone Model*/
#include < math.h>
#include < stdio.h>
#include < time.h>
#include < stdlib.h>
#include < conio.h>
#define AND1 .571388
#define AND2 .560999
#define OR1 1.149045
#define OR2 1.131896
#define SITA 1
#define NIU .1
#define OMIGA 50

float w;
float terg;

main()
{int m_result = 1, s;
float w1;
while(m_result != 5){
date();
switch(m_result){
case(1):             /*    Train*/
case(3):randomize();  /*ReTrain*/
w = random(80)+10; w = w/100.0;
w = random(80)+10; w = w/100.0;
study();
break;
case(2):practic(w);   /*Use new Model*/
break;
case(4):printf(" what do you want to see, OR or AND?(1/2) " );
scanf(" %d" , & s);
if(s==1){w1 = OR1;  w1 = OR2;  }
else if(s==2){w1 = AND1; w1 = AND2; }
else break;
practic(w1);
break;        /*Demo, show trained model*/
default:{}
}       /*End Switch*/
}               /*End While */
clrscr();
printf(" Good Bye!" );
}
{int r1;
clrscr();
printf(" \n\n\n" );
printf(" \t\t1\tStudy a Model\n" );     /*Train   */
printf(" \t\t2\tPratice the model\n" ); /*Retrain    */
printf(" \t\t3\tDefail the MOdel\n" );  /*Use Model trained  */
printf(" \t\t4\tPrepared Model\n" );    /*Demo*/
printf(" \t\t5\tEnd\n" );               /*End        */
printf(" \n\n\t\tWhat do you want to choice?(%d)" , r);
r1 = getche();
if (r1!=13) r = r1-48;
return(r);
}

date()    /*Show values.*/
{int i, j;
clrscr();
for(i = 0; i < 4; i++)
for(j = 0; j< 3; j++){
gotoxy(20+j*4, i+5);
printf("%.0f", terg[i][j]);
}
printf("\n\n\tThe quarities:%f, %f \n", w, w);
}

study() /*function of training*/
{float x, y, width;
int wrong = 1, i, total = 0;
input();  /*andinput(); */ /*orinput();  */
while(wrong){
wrong = 0;
for(i = 0; i< 4; i++){
x = OMIGA*(w*terg[i]+w*terg[i]-SITA);
y = 1.0/(1.0+exp(-x));
width = terg[i]-y;  /*Delta = "Teacher signal-Y" */
if(fabs(width)>=.01){
wrong = 1;
total++;
w = w*(1+width*NIU);     /*Change Weight0*/
w = w*(1+width*NIU);     /*Change Weight1*/
{error(); return 1; }
}
}
}/*If (fabs(width)>=0.01) then wrong = 0 and jump out of the loop*/
}
practic(quarity) /*Use trained model*/
float quarity;
{int k, y;
float x, width, x1, x2;
for(k = 0; k < 4; k++){
gotoxy(0, 4+k*3);
scanf("%f, %f", &x1, &x2);
x = OMIGA*(quarity*x1+quarity*x2-SITA);
y = 1.0/(1.0+exp(-x))+.01;  /*Accuracy is 0.01*/
printf("\t\t\ty = "%d" , y);
getch();
}
}

input() /*Input events when training*/
{int i;
printf("Please input the 4 groups data:\n\n");
for(i = 0; i< 4; i++){
printf("\tThe %d group x1, x2, teacher's point: ", i+1);
scanf("%f, %f, %f", &terg[i], &terg[i], &terg[i]);
}
clrscr();
}

andinput()
{terg = 1; terg = 1; terg = 1;
terg = 1;
terg = 0;
terg = 0;
terg = 0;
terg = 1;
terg = 0;
terg = 0;
terg = 0;
terg = 0;
}

orinput()
{terg = 1;
terg = 1;
terg = 1;
terg = 1;
terg = 0;
terg = 1;
terg = 0;
terg = 1;
terg = 1;
terg = 0;
terg = 0;
terg = 0;
}

error() /*Exit, maybe the Original weight value is not good,
or the model is not relizable (ex. XOR Model) */
{clrscr();
printf("\n\n\n\tBecause the firse QUARITIES's choise is unavailble, \n");
printf("\tIt can't get the right result, forveve!");
}
```
I think the comments in the source code are enought to read. Contact Info User Support Sales Question Webmaster