单层感知器
来源:互联网 发布:doc软件官方下载 编辑:程序博客网 时间:2024/05/01 07:54
#include <iostream>
using namespace std;
#define MAXSIZE 20
int wi[MAXSIZE]={0};
struct x_struct
{
int x1;
int x2;
int q;
};
void learn(int n,struct x_struct p[MAXSIZE],int b[MAXSIZE]);
int sgn(int x);
int aa[3]={1,1,1};
void t_main()
{
int i=1;
int fenlei[MAXSIZE];
int n;
cout<<"请输入您想要分类的样本点的个数:";
cin>>n;
struct x_struct yangben[MAXSIZE];
for(int k=0;k<n;k++)
{
cout<<"请输入第几"<<k+1<<"个样本:";
cin>>yangben[k].x1>>yangben[k].x2;
yangben[k].q=1;
}
cout<<"请依次输入每个样本的分类(A类输入1,B类输入0):";
for(int t=0;t<n;t++)
cin>>fenlei[t];
int tt=0;
learn(n,yangben,fenlei);
for(t=0;t<n;t++)
{
tt++;
if(tt>20)
t=n;
if(wi[t]==1)
{
t=-1;
learn(n,yangben,fenlei);
}
}
if(t==n)
cout<<"所要找的直线方程为:"<<"("<<aa[0]<<")"<<"X"<<"+"<<"("<<aa[1]<<")"<<"Y"<<"+"<<"("<<aa[2]<<")"<<"=0"<<"\n";
else
cout<<"未找到符合要求的直线方程!"<<"\n";
}
int sgn(int x)
{
if(x>=0)
return 1;
else
return 0;
}
//数组b用来存储用户的分类方法,n是用户输入的样本个数,p是用户输入的样本,a 是w
void learn(int n,struct x_struct p[MAXSIZE],int b[MAXSIZE])
{
int u;
for(int r=0;r<n;r++)
{
u=aa[0]*p[r].x1+aa[1]*p[r].x2+aa[2]*p[r].q;
cout<<"这次学习结束"<<"\n"<<"u="<<sgn(u)<<"\n";
if(sgn(u)==b[r])
wi[r]=0;
else
{
aa[0]=aa[0]+(b[r]-sgn(u))*p[r].x1;
aa[1]=aa[1]+(b[r]-sgn(u))*p[r].x2;
aa[2]=aa[2]+(b[r]-sgn(u))*p[r].q;
wi[r]=1;
}
cout<<"wi"<<"["<<r<<"]"<<"="<<wi[r]<<"\n";
}
}
运行结果为:
请输入您想要分类的样本点的个数:3
请输入第几1个样本:
3 2
请输入第几2个样本:
4 5
请输入第几3个样本:
9 0
请依次输入每个样本的分类(A类输入1,B类输入0):
1 1
0
这次学习结束
u=1
wi[0]=0
这次学习结束
u=1
wi[1]=0
这次学习结束
u=1
wi[2]=1
这次学习结束
u=0
wi[0]=1
这次学习结束
u=0
wi[1]=1
这次学习结束
u=0
wi[2]=0
这次学习结束
u=1
wi[0]=0
这次学习结束
u=1
wi[1]=0
这次学习结束
u=0
wi[2]=0
所要找的直线方程为:(-1)X+(8)Y+(2)=0
Press any key to continue
#include<iostream.h>
void main()
{
double p[2][3]={{-0.4,-0.5,0.6},{0.9,0,0.1}};
int t[3]={1,1,0};
int y[3]={-1,-1,-1};
int count=20;
double w[2]={0.01,0.01};
double b=0.01;
int i=0;
double e=0;
for( i=0;i<count;i++)
{
int x=i%3;
double v=b+p[0][x]*w[0]+p[1][x]*w[1];
if(v>=0)
y[x]=1;
else y[x]=0;
if((y[0]==t[0])&(y[1]==t[1])&(y[2]==t[2]))break;
/*int n=0;
for(int m=0;m<3;m++)
{
n=n+(t[m]-y[m])*(t[m]-y[m]);
}
e=n/3;
if(e=0)
break;*/
else
{
w[0]=w[0]+(t[x]-y[x])*p[0][x];
w[1]=w[1]+(t[x]-y[x])*p[1][x];
b=b+(t[x]-y[x])*b;
}
}
cout<<i<<endl;
cout<<"y="<<endl;
for(int j=0;j<3;j++)
cout<<y[j]<<" ";
cout<<endl;
cout<<"e="<<e<<endl;
//使用测试数据检测
double q[2][3]={{0.6,0.9,-0.1},{-0.1,-0.5,0.5}};
int y1[3]={0,0,0};
double e1=0;
for(int k=0;k<3;k++)
{
double v=b+q[0][k]*w[0]+q[1][k]*w[1];
if(v>=0)
y1[k]=1;
else y1[k]=0;
}
cout<<"y1="<<endl;
for( j=0;j<3;j++)
cout<<y1[j]<<" ";
cout<<endl;
}
运行结果:
5
y=
1 1 0
e=0
y1=
0 0 1
Press any key to continue
- 单层感知器
- 单层感知器神经网络
- 单层感知器
- 单层感知器
- 单层感知器-学习实践
- 单层感知器的MATLAB实现
- 单层感知器的MATLAB实现
- 单层感知器的学习与练习
- C语言实现单层感知器
- 神经网络学习之单层感知器
- [深度学习PART I] 单层感知器
- 人工神经网络(二)单层感知器
- 人工神经网络(二)单层感知器
- 单层感知器的python实现
- 单层感知机
- 单层感知机
- 使用nntool神经网络工具箱建立单层感知器
- 文本分类算法之--单层感知器的神经网络
- TCP/IP和Socket的关系
- MFC中App,Doc,MainFrame,View各指针的互相获取
- 14-2形状类族中的纯虚函数
- 防SQL注入函数群
- 第十三周项目二
- 单层感知器
- 14周项目一.3(动物怎么叫)
- JavaMail发邮件实战
- 最新c#开源项目
- 一键删除svn文件
- 第14周-项目1-Animal
- PHP漏洞全解(一)-PHP网页的安全性问题
- 第十四周项目二——图形类集锦
- 只有独立显卡(Nvidia GT640)的机器安装Ubuntu13.04黑屏