首页 > 编程知识 正文

人工神经网络感知器算法,图像模式识别算法

时间:2023-05-03 06:17:26 阅读:58656 作者:2874

编写一、问题描述感知器算法程序,使用该程序求解以下模式分类的解向量:

1 () (0),1 )0),1 )0),1 )0) }

2 ) ) (001 )、0 1 1 )、0 1 0 )、11 ) }

w(1)=(-1-2-20 )

二、算法介绍关于这次的算法程序,分为以下部分。

1、变量定义#定义了用于存储#define N 100struct Node //培训样本的数据结构{ int element[N]; //培训样本int sample; //类别} node[N]; 在此定义一个名为Node的数据结构,其中包含两个元素: element (用于记录每个架构类的数据的element )、sample (用于记录相应数据所属的架构类的sample )。

int n; //训练样本维度int m; //训练样本个数int c; //增量系数Node w[N]; //权重向量定义了其他要使用的变量。 (注:这里定义的都是全局变量)

2、数据输入部分cout '请输入培训样本的维数。' ; cin n; cout '请输入每个培训样本的样本数。' ; cin m; cout '培训样例w1:n '; for(intI=0; i m; I ) for(intj=0; j n; j({CINnode[I].Element[j]; node[i].sample=1; } } cout '培训样例w2:n '; for(intI=0; i m; I ) for(intj=0; j n; j({CINnode[mI].Element[j]; node[m i].sample=2; }接受用户手动输入的训练样本的维数和个数用于以后使用(注意:这里使用的变量n、m都是全局变量)。 然后,用户输入培训示例并将其放入名为node的node类型数组中。 (注意:输入培训示例时,模式类1的sample=1将同步更新,模式类2的sample=2将用于以后使用。)

3、归一化处理change(node ); 语音变更(nodex ) ) for ) intI=0; i 2 * m; I ) x(I ).element )=1; for(intI=0; i 2 * m; I ) for(intj=0; j=n; j () if(x ) I ).sample==2) x ) I ).element )=x ) I ).element ) *(-1 ); 调用Change函数将训练样本转换为扩展向量的形式,并将属于模式类2的样本乘以-1。

4、请输入初始化权重向量和校正增量系数cout '初始化权重向量。 n '; for(intI=0; i=n; I ) { cin w[1].element[i]; } cout '请输入校正增量系数。' ; cin c; 接受用户手动输入的权重向量和校正增量系数。

5、迭代计算部分preceptron(node,w ); voidpreceptRon(nodex (,Node w[] ) )//百分号算法({ int a=1; //用于记录当前权重向量校正次数cout '的权重向量w(1)为) ); for(intI=0; i=n; I ) { cout

<< w[a].element[i] << " "; } cout << "n"; int number = 0; //用于判断是否可以离开迭代循环的变量 while (1) { int temp = Mul(w[a], x[(a - 1) % (2 * m)]); if (temp > 0) { number++; for (int i = 0; i <= n; i++) { w[a + 1].element[i] = w[a].element[i]; } } else { for (int i = 0; i <= n; i++) { w[a + 1].element[i] = w[a].element[i] + c * x[(a - 1) % (2 * m)].element[i]; } } a++; cout << "经过调整后的权向量W(" << a << ")为:"; for (int i = 0; i <= n; i++) { cout << w[a].element[i] << " "; } cout << "n"; if (a % (2 * m) == 1 && number == (2 * m))//符合退出迭代条件 break; if (a % (2 * m) == 1) number = 0; }}

这里使用了一个名为number的参数用于记录每轮迭代时计算结果大于0的次数,当number与模式类样本个数(2m)相等时(即一轮迭代中并未调整权向量),跳出循环。

三、计算过程

 四、结果图像

五、完整代码 #include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>using namespace std;#define N 100struct Node //定义了一个用于存放训练样本的数据结构{ int element[N];//训练样本 int sample;//类别} node[N];int n;//训练样本的维数int m;//训练样本的个数int c;//校正增量系数Node w[N];//权向量void Change(Node x[]){ for (int i = 0; i < 2 * m; i++) x[i].element[n] = 1; for (int i = 0; i < 2 * m; i++) { for (int j = 0; j <= n; j++) { if (x[i].sample == 2) x[i].element[j] = x[i].element[j] * (-1); } }}int Mul(Node a, Node b) //用于实现权向量与训练样本相乘{ int result = 0; for (int i = 0; i <= n; i++) { result = result + a.element[i] * b.element[i]; } return result;}void Preceptron(Node x[], Node w[]) //感知器算法{ int a = 1; //用于记录当前权向量矫正次数 cout << "权向量W(1)为:"; for (int i = 0; i <= n; i++) { cout << w[a].element[i] << " "; } cout << "n"; int number = 0; //用于判断是否可以离开迭代循环的变量 while (1) { int temp = Mul(w[a], x[(a - 1) % (2 * m)]); if (temp > 0) { number++; for (int i = 0; i <= n; i++) { w[a + 1].element[i] = w[a].element[i]; } } else { for (int i = 0; i <= n; i++) { w[a + 1].element[i] = w[a].element[i] + c * x[(a - 1) % (2 * m)].element[i]; } } a++; cout << "经过调整后的权向量W(" << a << ")为:"; for (int i = 0; i <= n; i++) { cout << w[a].element[i] << " "; } cout << "n"; if (a % (2 * m) == 1 && number == (2 * m))//符合退出迭代条件 break; if (a % (2 * m) == 1) number = 0; }}int main(){ /*第一步:由用户输入各训练样本的参数*/ cout << "请输入训练样本的维数:"; cin >> n; cout << "请输入每个训练样本的样本数(这里假设只有两类w1,w2):"; cin >> m; cout << "请输入训练样本w1:n"; for (int i = 0; i < m ; i++) { for (int j = 0; j < n ; j++) { cin >> node[i].element[j]; node[i].sample = 1; } } cout << "请输入训练样本w2:n"; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> node[m + i].element[j]; node[m + i].sample = 2; } } /*第二步:将输入的训练样本转化为增广向量的形式,并将属于w2的样本乘以-1*/ Change(node); /*第三步:初始化权向量与校正增量系数*/ cout << "请输入初始权向量:n"; for (int i = 0; i <= n; i++) { cin >> w[1].element[i]; } cout << "请输入校正增量系数:"; cin >> c; /*第四步:迭代计算*/ Preceptron(node, w); return 0;} 六、备注

本文是感知器算法,使用C++的简单编写,可能有很多不足之处,欢迎讨论

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。