Perceptron implementation

I have tried to implement the perceptron network using the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<stdio.h>
#include<conio.h>
main()
{
      signed int x[4][2],tar[4];
      float w[2],wc[2],out=0;
      int i,j,k=0,h=0;
      float s=0,b=0,bc=0,alpha=0;
      float theta;
      printf("Enter the value of theta & alpha:");
      scanf("%f%f",&theta,&alpha);
      for(i=0;i<=3;i++)
      {   printf(" Enter the value of %d Inputrow & Target",i);
          printf(":");
          for(j=0;j<=1;j++)
          {
                           scanf("%d",&x[i][j]);
          }
          scanf("%d",&tar[i]);
          w[i]=0;
          wc[i]=0;
      }
      printf("Net\tTarget\tWeight Changes\tNew Weights\t Bias Changes\tBias\n");
      printf("----------------------------------------------------------------\n");
      mew:
          printf("ITERATION %d\n",h);
          printf("------------------------------------------------------------\n");
          for(i=0;i<=3;i++)
          { 
             for(j=0;j<=1;j++)
             {
                s+=(float)x[i][j]*w[j];
             }
             s+=b;
             printf("%.2f\t",s);
             if(s>theta)
             out=1;
             else if(s<-theta)
             out=-1;
             else
             {
                 out=0;
             }
             printf("%d\t",tar[i]);
             s=0;
             if(out==tar[i])
             { 
                for(j=0;j<=1;j++)
                {
                                 wc[j]=0;
                                 bc=0;
                                 printf("%.2f\t",wc[j]);
                }
                for(j=0;j<=1;j++)
                printf("%.2f\t",w[j]);
                k+=1;
                b+=bc;
                printf("%.2f\t\t",bc);
                printf("%.2f\t",b);
             }
             else
             {
                 for(j=0;j<=1;j++)
                 {
                                  wc[j]=x[i][j]*tar[i]*alpha;
                                  w[j]+=wc[j];
                                  printf("%.2f\t",wc[j]);
                                  wc[j]=0;
                 }
                 for(j=0;j<=1;j++)
                 printf("%.2f\t",w[j]);
                 bc=tar[i]*alpha;
                 b+=bc;
                 printf("%.2f\t\t",bc);
                 printf("%.2f\t",b);
             }
             printf("\n");
             }
             if(k==4)
             {
                     printf("\n Final Weights\n");
                     for(j=0;j<=1;j++)
                     {
                                      printf("w[%d]=%.2f\t",j,w[j]);
                     }
                     printf("Bias b=%.2f",b);
             }
             else
             {
                 k=0;
                 h=h+1;
                 getch();
                 goto mew;
             }
             getch();
          }

The output fails to converge and the iterations keep on going on endlessly.
Pls check and help.
Last edited on
closed account (oN3AqMoL)
First thing:Use the [code] function in your codes. It makes it MUCH easier to read.
Done! Thanks for the tip!
Now pls take a look at the code....
Last edited on
closed account (oN3AqMoL)
Hmph. I cant figure out the problem, Im a newbie with C code though, I probably cant help much.If it helps it looks like theres a stray - in one of your if statements, the one by s> theta
Last edited on
Topic archived. No new replies allowed.