⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 7类别的 Adaline 神经网络的模式识别程序, 可自行变换或者定义要分类的目标和向量的长度
💻 CPP
字号:
#include <fstream>
#include <iostream>
#include <cmath>
#include "Catagory.h"
using namespace std;

ofstream fout("output.txt");
int main()
{   //void Training(Category A, Category weight[N_Category], int &g);
    Category A[N_Category],cate;
 	double array_a1[N_Category][Vector_Long]={{0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,1,1,1,1,0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,1},
	 {1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,1,1,1,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,1,1,1,1,1,1,0},
	 {0,0,1,1,1,0,1,0,1,0,0,0,1,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,1,1,0,0},
	 {1,1,1,1,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,1,0,1,1,1,1,1,0,0},
	 {1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1},
	 {0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,1,1,1,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-1,-1,1,-1,-1,-1,-1},{-1,-1,-1,1,-1,-1,-1},{-1,-1,-1,-1,1,-1,-1},
	{-1,-1,-1,-1,-1,1,-1},{-1,-1,-1,-1,-1,-1,1}};

	for (int f=0; f < N_Category; f++)
	{ 
		A[f].Setcategory(array_a1[f], -1, array_c[f]);
	}

	for (int t=0; t < N_Category; t++)
	{ 
		A[t].Setcategory(array_a2[t], -1, array_c[t]);
	}

    for (int y=0;y < N_Category; y++)
	{ 
		A[y].Setcategory(array_a3[y], -1, array_c[y]);
	}


	int array_b[N_Category][Vector_Long]={{0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,1,1,1,1,1,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,1,1,1,0,1,1,1},
	{1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,1,1,1,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,1,1,1,1,1,1,0},
	{0,0,1,1,1,1,1,0,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,1,1,1,0},
	{1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,1,0,1,1,1,1,1,0,0},
	{1,1,1,1,1,1,1,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,1,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1},
	{0,0,0,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0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0,1,0,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,0,1,1,1,1,1,0,1,0,1,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,1}};
	for (int  i=0; i < N_Category;i++)
	{
        weight[i].Setcategory(Init_weight[i],-1,array_c[0]);
	}
    int g;
    cout << "The weight during training is:"<<endl;
    int k = 1;
    do
	 {
         g = 0;
         cout<<endl<< "Epoch " << k << endl;
		 for(int i = 0; i < N_Category; i++)
		 {  Training(A[i], weight, g);
			// cate.Training(A[i], weight, g);
             cout<<endl; 
             for( int j = 0; j < N_Category; j++)
			 {            
	             for( int m = 0; m < Vector_Long; m++)
			     {   
				    // cout<<weight[j].getvector(m)<<",";
  			     }
				// cout << weight[j].getbias() << endl;
			 }
		 } 
	     k++;
	 }
	 while (g != 0);

     int V_SUM[N_Category];
     int weight_changed[N_Category][Vector_Long];
	 int bias_changed[N_Category];
	 for( int j = 0; j < N_Category; j++)
	 {  
	     for( int i = 0; i < Vector_Long; i++)
		 {
  	        weight_changed[j][i] = weight[j].getvector(i);
		 
		 }
	 }

     for( j = 0; j < N_Category; j++)
	 {  
		 bias_changed[j]=weight[j].getbias();
	  
	 }
  cout<<endl;
  for( j = 0; j < N_Category; j++)
   {	  
	  V_SUM[j] = 0;
      for(int i=0; i < Vector_Long; i++)
	  {
         V_SUM[j] += weight_changed[j][i]* array_b[5][i];	 
	  }
      V_SUM[j] += (-1)*bias_changed[j]; // modified ********************************
   }
   for( j = 0; j < N_Category-1; j++)
   {	  
 	 cout<<V_SUM[j]<<"  , ";
   }
   cout<<V_SUM[6]<<endl;

     if (V_SUM[0] >= 0 && V_SUM[1] < 0 && V_SUM[2] < 0 && V_SUM[3] < 0 && V_SUM[4] < 0 && V_SUM[5] < 0 && V_SUM[6] < 0 )
         cout<<endl<<"This character is A."<<endl<<endl;

     else if ( V_SUM[0] < 0 && V_SUM[1] >= 0  && V_SUM[2] < 0  && V_SUM[3] < 0 && V_SUM[4] < 0 && V_SUM[5] < 0 && V_SUM[6] < 0  ) 
         cout<<endl<<"This character is B."<<endl<<endl;

     else if (V_SUM[0] < 0 && V_SUM[1] < 0  && V_SUM[2] > 0  && V_SUM[3] < 0 && V_SUM[4] < 0 && V_SUM[5] < 0 && V_SUM[6] < 0 ) 
         cout<<endl<<"This character is C."<<endl<<endl;

     else if (V_SUM[0] < 0 && V_SUM[1] < 0  && V_SUM[2] < 0  && V_SUM[3] > 0 && V_SUM[4] < 0 && V_SUM[5] < 0 && V_SUM[6] < 0 ) 
         cout<<endl<<"This character is D."<<endl<<endl;

     else if (V_SUM[0] < 0 && V_SUM[1] < 0  && V_SUM[2] < 0  && V_SUM[3] < 0 && V_SUM[4] > 0 && V_SUM[5] < 0 && V_SUM[6] < 0 ) 
         cout<<endl<<"This character is E."<<endl<<endl;

     else if (V_SUM[0] < 0 && V_SUM[1] < 0  && V_SUM[2] < 0  && V_SUM[3] < 0 && V_SUM[4] < 0 && V_SUM[5] > 0 && V_SUM[6] < 0 ) 
         cout<<endl<<"This character is F."<<endl<<endl;

     else if (V_SUM[0] < 0 && V_SUM[1] < 0  && V_SUM[2] < 0  && V_SUM[3] < 0 && V_SUM[4] < 0 && V_SUM[5] < 0 && V_SUM[6] > 0 ) 
         cout<<endl<<"This character is G."<<endl<<endl;

	 else cout<<endl<<"unknown!"<<endl<<endl;
	 
     return 0;
}

void sort (double a[],int n)
{
    double temp;
	for (int i=1;i<=n-1;i++)
	{
		for (int j=0;j<n-i-1;j++)
		{
			if (a[j]<a[j+1])
			{
			    temp=a[j];
				a[j]=a[j+1];
				a[j+1]=temp;
			}
		}
	}

}

void Training(Category A, Category weight[N_Category], int &g) 
{
    double sum[N_Category];
    double vec1[Vector_Long];
    double bias1;
    double desire[N_Category];
    double W1[N_Category][Vector_Long];
    double Bias[N_Category];
	double alpha = 0.01;
	double delta[N_Category];
	double T = 100;

    for(int i = 0; i < Vector_Long; i++ )
	{
        vec1[i] = A.getvector(i);
	}

    bias1 = A.getbias();

    for (int j=0; j < N_Category; j++)
	{
	    desire[j]=A.getdesire_output(j);
	}

    for ( j=0; j < N_Category; j++)
	{
	     for(int i = 0; i < Vector_Long; i++ )
		 { 
             W1[j][i] = weight[j].getvector(i);
		 }
	}
    for ( j=0; j < N_Category; j++)
	{
         Bias[j] = weight[j].getbias();
	}
 
    for ( j = 0; j < N_Category; j++)
	{  
	    sum[j] = 0;
        for (int i=0; i < Vector_Long; i++)
		{
            sum[j] += vec1[i]* W1[j][i] ;
		}
        sum[j] += bias1* Bias[j]; 
	   // cout<<"  *****  "<<sum[j]; 
	}
//*****************************************************************
  /*  for ( j=0; j < N_Category; j++)
	{ 
        if (sum[j] < 0)
        sum[j] = -1;
        else if (sum[j] > 0) 
            sum[j] = 1;
        else 
	        sum[j] = desire[j];
	}
*/
//*****************************************************************
	for (int f = 0; f < N_Category;f++)
	{
	    delta[f]=desire[f]-sum[f];
		cout<<delta[f]<<" ";
	}
	sort (delta,N_Category);
	cout<<" &&&&&& "<<delta[0]<<" &&&&&&   ";
	cout<<fabs(delta[0])<<" %%%%%%%%%%%%% ";
//******************************************************************
  /* for ( j=0; j < N_Category; j++)
	{ 
        if (sum[j] < 0)
        sum[j] = -1;
        else if (sum[j] > 0) 
            sum[j] = 1;
        else 
	        sum[j] = desire[j];
	}*/
//************************************************************************************
   
	  for ( j = 0; j < N_Category; j++)
	  {   
		  if (fabs(delta[0]) < T)
		  {
    //  if (sum[j] != desire[j]) 
	       for (int i = 0; i < Vector_Long; i++)
		   {
	           W1[j][i] += alpha * vec1[i] * delta[j];
		   }
	       Bias[j] += alpha * delta[j];
	       g++;
		}
	  }
   
    for ( j=0; j < N_Category; j++)
	{
	    for(int i = 0; i < Vector_Long; i++ )
		{ 
            weight[j].setvector(i, W1[j][i]);
		}
	}
    for ( j=0; j < N_Category; j++)
	{
        weight[j].setbias(Bias[j]);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -