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

📄 perceptron_test.cpp

📁 神经网络中的感知机的学习算法的c语言程序源代码
💻 CPP
字号:
#include<math.h>
#include<stdlib.h>
#include<iostream.h>
#include<time.h>
#define Pi 4*atan(1)

const int m=10;
const int k=3;
double x[m+30][k]={0.0};
double rand1,rand2;
double w[k]={0.1};
double thita=0.1;
int d;
double a=0.1;
double y;
int e,e_count;
int count;

void main()
{
	int i,j,t;
	srand((unsigned)time(NULL));
	for(i=0;i<m+30;i++)
	{
		for(j=0;j<k;j++)
		{
			rand1=rand()/32767.0;
			rand2=rand()/32767.0;
			if(rand1<1e-308)
				rand1=1e-308;
			x[i][j]=sqrt((-2)*log(rand1))*cos(2*Pi*rand2);
		}
	}
	t=(k-1)/2;
	count=0;
	do
	{	
		count++;
		//cout<<endl<<"第"<<count<<"次迭代"<<endl;
		e_count=0;
		for(i=0;i<m;i++)
		{	
			y=0;
			for(j=0;j<k;j++)
				y+=x[i][j]*w[j];
			y+=thita;
			if(y>=0)
				y=1;
			else y=0;
			if(x[i][t]>=0)
				e=int(1-y);
			else e=int(0-y);			
			for(j=0;j<k;j++)
				w[j]+=a*e*x[i][j];
			thita+=a*e;
			if(e!=0)
				e_count++;
		}
		//cout<<e_count<<endl;
	}
	while(e_count!=0);
	cout<<"迭代次数:"<<count<<endl;
	cout<<"收敛权矢量:"<<endl;
	cout<<"w[0]="<<thita<<endl;
	for(i=0;i<k;i++)
		cout<<"w["<<i+1<<"]="<<w[i]<<endl;
	e_count=0;
	for(i=m;i<m+30;i++)
	{
		y=0;
		for(j=0;j<k;j++)
			y+=x[i][j]*w[j];
		y+=thita;
		if(y>=0)
			y=1;
		else y=0;
		if(x[i][t]>=0)
			e=int(1-y);
		else e=int(0-y);
		if(e==0)
			e_count++;
	}
	cout<<"30个向量检验的正确结果数为:"<<e_count<<endl;
	e_count=e_count*100/30;
	cout<<"正确率:"<<e_count<<"%"<<endl;
}

⌨️ 快捷键说明

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