SCAN0139

SCAN0140

SCAN0141

SCAN0142

SCAN0143

SCAN0144

SCAN0145

SCAN0146

SCAN0147

 

==============================================================

C-CODE FILE: pa.c

 

#include <stdio.h>

#include <math.h>

#include <conio.h>

#include <process.h>

#include <stdlib.h>

 

#define N 8

 

void compute_output(void);

void compute_error(void);

void change_weights(void);

void train(int);

void print_err(void);

void print_w(void);

void print_act(void);

void print_pat(void);

void print_target(void);

 

float target[] = { 1,1,-1,-1,1,1,-1,-1};

float pattern[] = { 1, -1, 1, -1, 1, -1, 1, -1};

 

float activation[N]; /* output nodes */

float netout[N];

float error[N];

float weight[N][N];

float lrate = 0.125;

 

 

void main(void){

int i,j;

   /* initialize network */

   for(i=0;i<N;i++){

      for(j=0;j<N;j++)

         weight[i][j]=0;

      }

 

   for(j=0;j<50;j++) train(j);

 

}

 

void train(int cycle){

int i;

/*   system("CLS"); */

   printf("\n*******************************************************");

   printf("\n\nTrain and evaluate cycle %d",cycle);

 

   for(i=0;i<N;i++){

      netout[i]=0;

      error[i]=0;

      activation[i]=0;

   }

   change_weights();

   compute_output();

   compute_error();

 

   print_pat();

   print_w();

   print_act();

   print_target();

   print_err();

}

 

void compute_output(void){

int i,j;

   /* this routine assumes no threshold, thus all layer 0 nodes fire. */

   for(j=0;j<N;j++)

      for(i=0;i<N;i++) 

         netout[j] +=(pattern[i] * weight[i][j]);

      

   for(i=0;i<N;i++)

      activation[i] = netout[i];

}

 

void compute_error(void){

int i;

   for(i=0; i<N; i++)

      error[i] = target[i] - (activation[i]/N); /* normalized dot */

}

 

void change_weights(void){

int i,j;

   for(i=0; i<N; i++)

      for(j=0; j<N; j++)

         weight[i][j] += lrate * pattern[i] * target[j] - (lrate * weight[i][j]);

}

 

void print_w(void){

int i,j;

   printf("\n\n");

   for(j=0; j<N; j++)

      printf("     %d",j);

   for(i=0; i<N; i++){

      printf("\n%d: ",i);

      for(j=0; j<N; j++)

         printf("%+1.2f ",weight[i][j]);

      }

}

 

void print_target(void){

int i;

   printf("\n\nt=");

   for(i=0; i<N; i++){

         printf(" %+1.2f",target[i]);

      }

}

void print_pat(void){

int i;

   printf("\n\np=");

   for(i=0; i<N; i++){

         printf(" %+1.2f",pattern[i]);

      }

}

void print_act(void){

int i;

   printf("\n\no=");

   for(i=0; i<N; i++)

         printf(" %+1.2f",activation[i]);

}

void print_err(void){

int i;

   printf("\n\ne=");

   for(i=0; i<N; i++)

         printf(" %+1.2f",error[i]);

}

 

==============================================================

MakeFile: pa.mak

 

pa.exe      : pa.obj

                                     cl pa.obj /link /SE:256 /NOE /ST:4096

 

pa.obj     : pa.c 

                                     cls

                                     cl /nologo /AL /W3 /c pa.c

 

==============================================================

OUTPUT: pa.out

 

Following are the outputs of my pa.c program for question 1.1

*******************************************************

 

 

Train and evaluate cycle 0

 

p= +1.00 -1.00 +1.00 -1.00 +1.00 -1.00 +1.00 -1.00

 

     0     1     2     3     4     5     6     7

0: +0.13 +0.13 -0.13 -0.13 +0.13 +0.13 -0.13 -0.13 

1: -0.13 -0.13 +0.13 +0.13 -0.13 -0.13 +0.13 +0.13 

2: +0.13 +0.13 -0.13 -0.13 +0.13 +0.13 -0.13 -0.13 

3: -0.13 -0.13 +0.13 +0.13 -0.13 -0.13 +0.13 +0.13 

4: +0.13 +0.13 -0.13 -0.13 +0.13 +0.13 -0.13 -0.13 

5: -0.13 -0.13 +0.13 +0.13 -0.13 -0.13 +0.13 +0.13 

6: +0.13 +0.13 -0.13 -0.13 +0.13 +0.13 -0.13 -0.13 

7: -0.13 -0.13 +0.13 +0.13 -0.13 -0.13 +0.13 +0.13 

 

o= +1.00 +1.00 -1.00 -1.00 +1.00 +1.00 -1.00 -1.00

 

t= +1.00 +1.00 -1.00 -1.00 +1.00 +1.00 -1.00 -1.00

 

e= +0.88 +0.88 -0.88 -0.88 +0.88 +0.88 -0.88 -0.88

*******************************************************

 

Train and evaluate cycle 1

 

p= +1.00 -1.00 +1.00 -1.00 +1.00 -1.00 +1.00 -1.00

 

     0     1     2     3     4     5     6     7

0: +0.25 +0.25 -0.25 -0.25 +0.25 +0.25 -0.25 -0.25 

1: -0.25 -0.25 +0.25 +0.25 -0.25 -0.25 +0.25 +0.25  

2: +0.25 +0.25 -0.25 -0.25 +0.25 +0.25 -0.25 -0.25 

3: -0.25 -0.25 +0.25 +0.25 -0.25 -0.25 +0.25 +0.25 

4: +0.25 +0.25 -0.25 -0.25 +0.25 +0.25 -0.25 -0.25 

5: -0.25 -0.25 +0.25 +0.25 -0.25 -0.25 +0.25 +0.25 

6: +0.25 +0.25 -0.25 -0.25 +0.25 +0.25 -0.25 -0.25 

7: -0.25 -0.25 +0.25 +0.25 -0.25 -0.25 +0.25 +0.25 

 

o= +2.00 +2.00 -2.00 -2.00 +2.00 +2.00 -2.00 -2.00

 

t= +1.00 +1.00 -1.00 -1.00 +1.00 +1.00 -1.00 -1.00

 

e= +0.75 +0.75 -0.75 -0.75 +0.75 +0.75 -0.75 -0.75

*******************************************************

 

Train and evaluate cycle 2

 

p= +1.00 -1.00 +1.00 -1.00 +1.00 -1.00 +1.00 -1.00

 

     0     1     2     3     4     5     6     7

0: +0.38 +0.38 -0.38 -0.38 +0.38 +0.38 -0.38 -0.38 

1: -0.38 -0.38 +0.38 +0.38 -0.38 -0.38 +0.38 +0.38 

2: +0.38 +0.38 -0.38 -0.38 +0.38 +0.38 -0.38 -0.38 

3: -0.38 -0.38 +0.38 +0.38 -0.38 -0.38 +0.38 +0.38 

4: +0.38 +0.38 -0.38 -0.38 +0.38 +0.38 -0.38 -0.38 

5: -0.38 -0.38 +0.38 +0.38 -0.38 -0.38 +0.38 +0.38 

6: +0.38 +0.38 -0.38 -0.38 +0.38 +0.38 -0.38 -0.38 

7: -0.38 -0.38 +0.38 +0.38 -0.38 -0.38 +0.38 +0.38 

 

o= +3.00 +3.00 -3.00 -3.00 +3.00 +3.00 -3.00 -3.00

 

t= +1.00 +1.00 -1.00 -1.00 +1.00 +1.00 -1.00 -1.00

 

e= +0.63 +0.63 -0.63 -0.63 +0.63 +0.63 -0.63 -0.63

*******************************************************

 

Train and evaluate cycle 3

 

p= +1.00 -1.00 +1.00 -1.00 +1.00 -1.00 +1.00 -1.00

 

     0     1     2     3     4     5     6     7

0: +0.50 +0.50 -0.50 -0.50 +0.50 +0.50 -0.50 -0.50 

1: -0.50 -0.50 +0.50 +0.50 -0.50 -0.50 +0.50 +0.50 

2: +0.50 +0.50 -0.50 -0.50 +0.50 +0.50 -0.50 -0.50 

3: -0.50 -0.50 +0.50 +0.50 -0.50 -0.50 +0.50 +0.50 

4: +0.50 +0.50 -0.50 -0.50 +0.50 +0.50 -0.50 -0.50 

5: -0.50 -0.50 +0.50 +0.50 -0.50 -0.50 +0.50 +0.50 

6: +0.50 +0.50 -0.50 -0.50 +0.50 +0.50 -0.50 -0.50 

7: -0.50 -0.50 +0.50 +0.50 -0.50 -0.50 +0.50 +0.50 

 

o= +4.00 +4.00 -4.00 -4.00 +4.00 +4.00 -4.00 -4.00

 

t= +1.00 +1.00 -1.00 -1.00 +1.00 +1.00 -1.00 -1.00

 

e= +0.50 +0.50 -0.50 -0.50 +0.50 +0.50 -0.50 -0.50

*******************************************************

 

Train and evaluate cycle 4

 

p= +1.00 -1.00 +1.00 -1.00 +1.00 -1.00 +1.00 -1.00

 

     0     1     2     3     4     5     6     7

0: +0.63 +0.63 -0.63 -0.63 +0.63 +0.63 -0.63 -0.63 

1: -0.63 -0.63 +0.63 +0.63 -0.63 -0.63 +0.63 +0.63 

2: +0.63 +0.63 -0.63 -0.63 +0.63 +0.63 -0.63 -0.63 

3: -0.63 -0.63 +0.63 +0.63 -0.63 -0.63 +0.63 +0.63 

4: +0.63 +0.63 -0.63 -0.63 +0.63 +0.63 -0.63 -0.63 

5: -0.63 -0.63 +0.63 +0.63 -0.63 -0.63 +0.63 +0.63 

6: +0.63 +0.63 -0.63 -0.63 +0.63 +0.63 -0.63 -0.63 

7: -0.63 -0.63 +0.63 +0.63 -0.63 -0.63 +0.63 +0.63 

 

o= +5.00 +5.00 -5.00 -5.00 +5.00 +5.00 -5.00 -5.00

 

t= +1.00 +1.00 -1.00 -1.00 +1.00 +1.00 -1.00 -1.00

  

e= +0.38 +0.38 -0.38 -0.38 +0.38 +0.38 -0.38 -0.38