ANN并且不使用扫描输入

时间:2018-03-24 21:15:51

标签: c neural-network logic

我有人工神经网络的代码,但是我的教授说我们不能扫描输入,因为他因课堂大小而难以评分,我有两个代码块,一个有ANN,另一个没有,但我无法得到第二个正确的代码。我错过了什么?

结果应为1 1 1 0

编辑:示例输入应为enter image description here

#include <stdio.h>
#include <stdlib.h>

int main()
{
int s,m,dot,i,j,k,ite;
float iterationError, T = 0.5;
printf("What is the sample size?\n");
scanf ("%d", &s); /* taking input of sample size from user and storing it in s */
printf("Number of inputs  in a sample?\n");
scanf ("%d", &m); /* taking input of # of inputs in a sample from user and storing it in m */
float x[s][m], z[s], n[s], w[m]; /* defining the varible */
for (i=0;i<m;i++)
{
    w[i] = 0; /* initialising the weights to zero */
}
for (i=0;i<s;i++)
{
    printf("Enter the input training data of sample %d\n",i+1);
    for (j=0;j<m;j++)
    {
        scanf("%f",&x[i][j]); /* taking inputs of each sample from user and storing it in matrix x */
    }
    printf("Enter the output of sample %d\n",i+1);
    scanf("%f",&z[i]); /* taking desired output of each sample from user and storing it in array z */
}
for (i=0;i<s;i++)
{
    dot = 0;
    for (j=0;j<m;j++)
    {
        dot = dot + x[i][j]*w[j]; /* calculating the dot product of input and their corresponding weights */
    }
    /* assigning the output based on the calculated dot product */
    if (dot > T)
    {
        n[i] = 1;
    }
    else
    {
        n[i] = 0;
    }
    iterationError = iterationError+abs(z[i]-n[i]); /* error for initial assumption of weights */
}
printf("Initial output: %f \t%f \t%f \t%f\n",n[0],n[1],n[2],n[3]);
printf("The adjusted weights are:\n");
ite = 0;
/* training the code in a loop to adjust the weights for desired outputs */
while (iterationError > 0.4 && ite < 20){
    ite = ite + 1;
    printf("Iteration: %d ************************************\n",ite);
    for (i=0;i<s;i++)
    {
        iterationError = 0; /* making the error zero for new iteration */
        /* updating the weights based on the algorithm given */
        if (n[i]!=z[i])
        {
            if (n[i]==0)
            {
                for (j=0;j<m;j++)
                {
                    w[j] = w[j] + x[i][j];
                }
            }
            else
            {
                for (j=0;j<m;j++)
                {
                    w[j] = w[j] - x[i][j];
                }
            }
        }
        printf("%f \t%f \t%f \t",w[1],w[2],w[3]);
        /* calculating the output, as discussed above, based on updated 
         weights */
        for (k=0;k<s;k++)
        {
            dot = 0;
            for (j=0;j<m;j++)
            {
                dot = dot + x[k][j]*w[j];
            }
            if (dot > T)
            {
                n[k] = 1;
            }
            else
            {
                n[k] = 0;
            }
            iterationError = iterationError+abs(z[k]-n[k]);
        }
        printf("iterationError = %f\n",iterationError);
    }
}
printf("Final output: %f \t%f \t%f \t%f\n",n[0],n[1],n[2],n[3]);
return 0;
  }

这是工作代码的代码,这是我到目前为止尝试不使用scanf创建它时的代码

#include <stdio.h>
#include <stdlib.h>
int main()
{
float trainingData [100][4] = { {1,0,0,1}  , {1,0,1,1}  , {1,1,0,1}  , 
{1,1,1,0} };
float w[100][4]= {{0,0,0},{0,0,0},{0,0,0}};   //w is weights


int s,m,dot,i,j,k,ite,d;
float iterationError, T = 0.5;
float net;
float z;
int out[4];
if (net>T)
    out[i] =1;
else out[i] =0;

    for (i=0;i<3;i++)
{
    d=0;
    for (j=0;j<2;j++) //out is correct
    {
        net = net + trainingData[i][j]*w[i][j];
    }
    if (net>T)
    {
        out[i] =1;
    }
    else
    {
        out[i] =0;
    }
    for (j=0;j<2;j++)
    {

    iterationError = iterationError + abs(trainingData[i][j]-out[i]);
    }

 }
 printf(" initial output: %f\t %f\t %f\t %f\n",out[0],out[1],out[2],out[3]);
  printf( "the adjusted weights are:\n");

 ite = 0;
 while (iterationError>0.4 && ite < 20)
{
ite = ite+1;
printf("Iteration: %d *******************************\n",ite);
for (i=0; i<3;i++)
{
    iterationError=0;
    if (out[i] != trainingData[i][3])
    {
        if (out[i]==0)
        {
            for (j=0; j<2;j++)
            {
                w[j] = w[j] + trainingData[i][j];
            }
        }
        else
        {
           for(j=0; j<2;j++)
           {
               w[j] = w[j] - trainingData[i][j];
           }
        }
    }
    printf("%f \t %f \t %f \t %f \n", w[0],w[1],w[2]);
    for(k=0;k<3;k++)
    {
        net=0;
        for(j=0;j<2;j++)
        {
            net = net+ trainingData[k][j]*w[j];
        }
        if (net>t)
        {
            out[k]=1;
        }
        else
        {
            out[k] = 0;
        }
        iterationError = iterationError+abs(trainingData[k]-out[k]);
    }
    printf("iterationError = %f\n",iterationError);
}
}
   printf("Final output: %f \t%f \t%f \t%f\n",n[0],n[1],n[2],n[3]);
   return 0;
  }

我只是想指向正确的方向,以便我可以学习它。

0 个答案:

没有答案