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

📄 main.cpp

📁 感知器神经网络对简单的句子进行分类的程序, 可以扩展为对文章进行文本分类
💻 CPP
字号:
#include <fstream>
#include <iostream>
#include <cmath>
#include "head.h"

using namespace std;

void Training(Category A, Category weight[3], int &g) ;
void Vector(char sen[],int a[11]);

int main()
{
    Category A[6], B;
    int array_b[11];
    int array_a[6][11]={{0,0,0,0,0,1,0,0,1,0,1},{1,0,0,0,1,1,0,0,1,0,1},{1,0,1,0,1,1,1,1,0,1,0},
	{1,1,0,1,0,0,1,1,1,0,1},{1,0,1,1,0,1,1,0,1,1,1},{0,1,0,1,0,1,0,0,1,1,1}};

	int array_c[6][3]={{1,-1,-1},{1,-1,-1},{-1,1,-1},{-1,1,-1},{-1,-1,1},{-1,-1,1}};
cout<<array_a[0];
    A[0].Setcategory(array_a[0], -1, array_c[0]);
    A[1].Setcategory(array_a[1], -1, array_c[1]);

    A[2].Setcategory(array_a[2], -1, array_c[2]);
    A[3].Setcategory(array_a[3], -1, array_c[3]);

    A[4].Setcategory(array_a[4], -1, array_c[4]);
    A[5].Setcategory(array_a[5], -1, array_c[5]);
    
    char sentence[80];
    cout<<"The sentence you want to category is: "<<endl;

    FILE *fp;       // input the document from the file
    if ((fp = fopen( "sentence1.txt","rw" ) ) == NULL )
	{
        printf( "Can't open the file sentence4.txt\n" );
    }
    else 
	{
        fgets( sentence, 80, fp ); 
        puts( sentence );
        fclose( fp );
	}

    Category weight[3];
    int Init_weight[3][11]={{-1,4,-1,13,-1,2,-7,-1,11,-1,-1},
	{-1,-2,-1,3,-1,-1,4,-1,6,-1,-3},{-5,9,-8,-4,-6,-2,-1,10,-1,12,-3}};
    weight[0].Setcategory(Init_weight[0],-1,array_c[0]);
    weight[1].Setcategory(Init_weight[1],-1,array_c[0]);
    weight[2].Setcategory(Init_weight[2],-1,array_c[0]);
    Vector(sentence, array_b);
    B.Setcategory(array_b,-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 < 6; i++)
		 { 
			 Training(A[i], weight, g);
             cout<<endl; 
             for( int j = 0; j < 3; j++)
			 {            
	             for( int m = 0; m < 11; m++)
			     {   
				     cout<<weight[j].getvector(m)<<",";
  			     }
				 cout << weight[j].getbias() << endl;
			 }
		 } 
	     k++;
	 }
	 while (g != 0);

     int vector_sum[3];
     int weight_changed[3][11];
	 int bias_changed[3];
	 for( int j = 0; j < 3; j++)
	 {  // cout<<endl;
	     for( int i = 0; i < 11; i++)
		 {
  	        weight_changed[j][i] = weight[j].getvector(i);
		  //  cout<<weight_changed[j][i]<<"," ;
		 }
	 }

     for( j = 0; j < 3; j++)
	 {  // cout<<endl;
		 bias_changed[j]=weight[j].getbias();
	   //  cout<<bias_changed[j]<<",";
	 }
	// cout<<endl;
   // for( int i = 0; i < 11; i++)
   // cout<<array_b[i]<<",";

     for( j = 0; j < 3; j++)
	 {
		 vector_sum[j] = ( weight_changed[j][0]* array_b[0] + weight_changed[j][1]* array_b[1] 
		                 + weight_changed[j][2]* array_b[2] + weight_changed[j][3]* array_b[3] 
 	                     + weight_changed[j][4]* array_b[4] + weight_changed[j][5]* array_b[5]
				         + weight_changed[j][6]* array_b[6] + weight_changed[j][7]* array_b[7] 
	                     + weight_changed[j][8]* array_b[8] + weight_changed[j][9]* array_b[9] 
				         + weight_changed[j][10]* array_b[10]) + (-1)*bias_changed[j];	
		
	 }

 	 cout<<endl<<vector_sum[0]<<" ,"<<vector_sum[1]<<" ,"<<vector_sum[2]<<" ,"<<endl;

     if (vector_sum[0] > 0 && vector_sum[1] < 0 &&vector_sum[2] < 0 )
         cout<<endl<<"This document should assign into category A."<<endl<<endl;

     else if (vector_sum[0] < 0 && vector_sum[1] > 0 && vector_sum[2]< 0 ) 
         cout<<endl<<"This document should assign into category B."<<endl<<endl;

     else if (vector_sum[0] < 0 && vector_sum[1] < 0 && vector_sum[2] > 0 ) 
         cout<<endl<<"This document should assign into category C."<<endl<<endl;

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

void Vector(char sen[],int a[11])
{ 	//char sen[]={"shipment of gold arrived in a truck ."};
  for (int i=0;i<11;i++)
  {
	  a[i]=0;
  }
	int c; // the character in the sentence
	int b=0; //count the character in every word store in temp
	int word=0; 
	char temp[11]; //store a word
    for (c=0; (sen[c]) != '\0'; c++) //not in end of the sentence
	{
	   if (sen[c]==' ') // end of a word when met the space
	   { 
		 word=0;
	     temp[b]='\0';
         b=0;

		if (strcmp("a",temp)==0) 
	       a[0]++;
	     else if (strcmp("arrived",temp)==0)
		   a[1]++;
         else if (strcmp("damaged",temp)==0)
		   a[2]++;
         else if (strcmp("delivery",temp)==0)
		   a[3]++;
         else if (strcmp( "fire",temp)==0)
		   a[4]++;
         else if (strcmp("gold",temp)==0)
		   a[5]++;
         else if (strcmp( "in",temp)==0)
		   a[6]++;
         else if (strcmp( "of",temp)==0)
		   a[7]++;
         else if (strcmp( "silver",temp)==0)
		   a[8]++;
         else if(strcmp("shipment",temp)==0)
		   a[9]++;
         else if (strcmp("truck",temp)==0)
		   a[10]++;
	   }

      else if (word==0)
	  {
	   word=1;
       temp[b]=sen[c]; // put each characte into temp when not met the space
       b++;
	  } // character plus
      else 
	  {
       temp[b]=sen[c];
       b++;
	  }
	}   
 cout<<"The weight of each word in the sentence is: "<<endl;
 cout<<" a: "<<a[0]<<" arrived: "<<a[1]<<" damaged: "<<a[2]
     <<" delivery: "<<a[3]<<" fire: "<<a[4]<<" gold: "<<a[5]
     <<" in: "<<a[6]<<" of: "<<a[7]<<"  silver: "<<a[8]
     <<"   shipment: "<<a[9]<<"  truck: "<<a[10]<<endl<<endl;
 cout<<"The vector of the sentence is: "<<endl<<endl;
 for ( i = 0; i < 11; i++)
 {
   cout<<"  "<<a[i];
 }
 cout<<endl<<endl;
}

void Training(Category A, Category weight[3], int &g) 
{
 double sum[3];

 int vec1[11];
 int bias1;
 int desire[3];
 int W1[3][11];
 int Bias[3];
 
 for(int i = 0; i < 11; i++ )
 {
    vec1[i] = A.getvector(i);
 }

 bias1 = A.getbias();

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

 for ( j=0; j<3; j++)
 {
	 for(int i = 0; i < 11; i++ )
	 { 
        W1[j][i] = weight[j].getvector(i);
	 }
 }
 for ( j=0; j<3; j++)
 {
     Bias[j] = weight[j].getbias();
 }
 
 for ( j=0; j<3; j++)
 {
     sum[j] = ( vec1[0]* W1[j][0] + vec1[1]* W1[j][1] 
	          + vec1[4]* W1[j][4] + vec1[5]* W1[j][5] 
	          + vec1[2]* W1[j][2] + vec1[3]* W1[j][3]
	          + vec1[6]* W1[j][6] + vec1[7]* W1[j][7]
	          + vec1[8]* W1[j][8] + vec1[9]* W1[j][9] 
	          + vec1[10]* W1[j][10] ) + bias1* Bias[j];
     cout<<"  *******  "<<sum[j];   
 }

 for ( j=0; j<3; j++)
 {
	 
 if (sum[j] < 0)
       sum[j] = -1;
 else if (sum[j] > 0) 
       sum[j] = 1;
 else 
	   sum[j] = desire[j];
 }


 if (sum[0] != desire[0]) 
 {  
	 for (int i = 0; i < 11; i++)
	 {
		 W1[0][i] += vec1[i] * desire[0];
	 }
	 Bias[0] += bias1 * desire[0];
 }

 if (sum[1] != desire[1]) 
 {  
	 for (int i = 0; i < 11; i++)
	 {
	     W1[1][i] += vec1[i] * desire[1];
	 }
	 Bias[1] += bias1 * desire[1];
 }

 if (sum[2] != desire[2]) 
 {  
	 for (int i = 0; i < 11; i++)
	 {
	     W1[2][i] += vec1[i] * desire[2];
	 }
	 Bias[2] += bias1 * desire[2];
     g++;
 }

 for ( j=0; j<3; j++)
 {
	 for(int i = 0; i < 11; i++ )
	 { 
        weight[j].setvector(i, W1[j][i]);
	 }
 }
 for ( j=0; j<3; j++)
 {
     weight[j].setbias(Bias[j]);
 }

}

⌨️ 快捷键说明

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