感知机的实现和感知机对偶形式的实现

以下两个均为感知机对于3个样本数据(3,3,1),(4,3,1),(1,1,-1)的分类实现。

这是感知机普通形式的实现

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
int x1,x2,y;
node(int x1=0,int x2=0,int y=0)
:x1(x1),x2(x2),y(y)
{
}	
};
class A{
	
};
int sign(int x)
{
	if(x>0)
	return 1;
	else if(x<0)
	return -1;
	else if(x==0)
	return 0;
}
int main()
{
    node nn[3];
    nn[0]=node(3,3,1);
    nn[1]=node(4,3,1);
    nn[2]=node(1,1,-1);
    int w1=0,w2=0,b=0;
    int cnt=0;
    
    while(true)
    {
    	//cout<<cnt<<"   cnt"<<endl;
    	cnt++;
    	bool jump=false;
    	for(int i=0;i<3;i++)
    	{
    		int x1=nn[i].x1;
    		int x2=nn[i].x2;
    		int y=nn[i].y;
    		
    		if(sign(w1*x1+w2*x2+b)!=y)
    		{
    			w1=w1+y*x1;
    			w2=w2+y*x2;
    			b=b+y;
    			cout<<w1<<" "<<w2<<" "<<b<<" "<<cnt<<" "<<x1<<" "<<x2<<" "<<y<<endl;
    			jump=true;
    		}
    		if(jump)
    		break;
    	}
    	if(jump==false)
    	break;
    }
    cout<<w1<<" "<<w2<<" "<<b<<endl; 
    
}
以下是感知机对偶形式的分类实现

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int gram[3][3];
struct node
{
int x1,x2,y,a;
node(int x1=0,int x2=0,int y=0,int a=0)
:x1(x1),x2(x2),y(y),a(a)
{
}	
};
node nn[3];
int sign(int x)
{
	if(x>0)
	return 1;
	else if(x<0)
	return -1;
	else if(x==0)
	return 0;
}
void calgram()
{
	memset(gram,0,sizeof(gram));
	for(int i=0;i<3;i++)
	for(int j=0;j<3;j++)
	{   
	    int x11=nn[i].x1;
	    int x12=nn[i].x2;
	    int x21=nn[j].x1;
	    int x22=nn[j].x2;
		gram[i][j]+=(x11*x21+x12*x22); 
	}
}
int main()
{
	//node nn[3];
    nn[0]=node(3,3,1,0);
    nn[1]=node(4,3,1,0);
    nn[2]=node(1,1,-1,0);
    calgram();
    int b=0;
    int cnt=0;
    while(true)
    {  
    cnt++;
    if(cnt==10)
    break;
        bool jump=false;
    	for(int i=0;i<3;i++)
    	{
    		int yi=nn[i].y;
    	    int res=0;
    		for(int j=0;j<3;j++)
    		{
    			int aj=nn[j].a;
    			int yj=nn[j].y;
    		    res+=gram[i][j]*aj*yj;
    		}
    		res+=b;
    	
    		if(sign(res)*yi<=0)
    		{
    			//cout<<sign(res)<<" "<<yi<<" "<<i<<endl;
    			nn[i].a+=1;//学习率为1
				b+=yi; 
				jump=true;
		
            //cout<<"*****"<<endl;
				break;
    		}
    	}
    //	for(int i=0;i<3;i++)
    //	cout<<nn[i].a<<" ";
    //	cout<<b<<endl;
    	if(jump==false)
    	{
    		break;
    	}
    }
    int w1=0,w2=0;
    for(int i=0;i<3;i++)
    {
    	int x1=nn[i].x1;
    	int x2=nn[i].x2;
    	int y=nn[i].y;
    	int a=nn[i].a;
    	w1+=x1*a*y;
    	w2+=x2*a*y;
    }
    	cout<<w1<<" "<<w2<<" "<<b<<endl;
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值