2022年Bp网络算法源代码 .pdf





《2022年Bp网络算法源代码 .pdf》由会员分享,可在线阅读,更多相关《2022年Bp网络算法源代码 .pdf(9页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、#include #include #include #include #define IN 2 / 输入向量维数#define OUT 2 / 输出向量维数#define NUM 20 / 样本数量#define Loop_MAX 262140 / 最大循环次数#define dot_MAX 20 / 最大结点个数typedef struct /bp 人工神经网络结构 int dot; / 隐层节点数double vINdot_MAX; / 输入层权矩阵double udot_MAXdot_MAX; / 隐藏层权矩阵double wdot_MAXOUT; / 输出层权矩阵double rat
2、e; / 学习率double error; / 允许误差限bp_net; double vIN3=0.5,0.4,0.1,0.2,0.6,0.2; / 题目要求的权矩阵double u33=0.10,0.55,0.35,0.20,0.45,0.35,0.25,0.15,0.60; / 同上double w3OUT=0.30,0.35,0.35,0.25,0.45,0.30; / 同上double fnet(double net) /Sigmoid 函数 return 1/(1+exp(-net); void Initialize_Bp(bp_net *bp) / 初始化 bp 网络 print
3、f( 请输入隐层结点数:n); scanf(%d, &(*bp).dot); printf( 请输入学习率:n); scanf(%lf, &(*bp).rate); printf( 请输入允许误差限:n); scanf(%lf, &(*bp).error); int i, j,flag; start: printf( 请选择权矩阵输入形式:n1 、随机产生权矩阵n2 、手动输入权矩阵n3 、名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 9 页 - - - - - - -
4、 - - 载入题目要求权矩阵n); scanf(%d,&flag); if(flag=1) srand(unsigned)time(NULL); / 随机函数产生随机权矩阵for (i = 0; i IN; i+) for (j = 0; j (*bp).dot; j+) (*bp).vij = rand() / (double)(RAND_MAX); for (i = 0; i (*bp).dot; i+) for (j = 0; j (*bp).dot; j+) (*bp).uij = rand() / (double)(RAND_MAX); for (i = 0; i (*bp).dot
5、; i+) for (j = 0; j OUT; j+) (*bp).wij = rand() / (double)(RAND_MAX); else if(flag=2) printf( 输入权矩阵: n); for (i = 0; i IN; i+) for (j = 0; j (*bp).dot; j+) scanf(%lf,&(*bp).vij); printf( 隐藏权矩阵:n); for (i = 0; i (*bp).dot; i+) for (j = 0; j (*bp).dot; j+) scanf(%lf,&(*bp).uij); printf( 输出权矩阵: n); for
6、 (i = 0; i (*bp).dot; i+) for (j = 0; j OUT; j+) scanf(%lf,&(*bp).wij); else if(flag=3) for (i = 0; i IN; i+) for (j = 0; j 3; j+) (*bp).vij = vij; for (i = 0; i 3; i+) for (j = 0; j 3; j+) (*bp).uij = uij; for (i = 0; i 3; i+) for (j = 0; j OUT; j+) (*bp).wij = wij; printf( 载入完成! n); else 名师资料总结 -
7、- -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 9 页 - - - - - - - - - printf( 输入错误!请重新输入!n); goto start; printf( 初始化输入权矩阵:n); for (i = 0; i IN; i+) for (j = 0; j (*bp).dot; j+) printf(%lf ,(*bp).vij); printf(n); printf( 初始化隐藏层权矩阵:n); for (i = 0; i (*bp).dot; i+) for (j =
8、 0; j (*bp).dot; j+) printf(%lf ,(*bp).uij); printf(n); printf( 初始化输出权矩阵:n); for (i = 0; i (*bp).dot; i+) for (j = 0; j OUT; j+) printf(%lf ,(*bp).wij); printf(n); printf(n); void Train_Bp(bp_net *bp, double xNUMIN, int yNUMOUT) / 训练 bp 网络 double e = (*bp).error; / 允许误差限double rate = (*bp).rate; / 学
9、习率int dot = (*bp).dot; / 隐藏层结点double vINdot_MAX,wdot_MAXOUT,udot_MAXdot_MAX; / 权矩阵double Error_Inputdot_MAX,Error_hiderdot_MAX, Error_OutputOUT; / 各个结点的反向误差double Inputdot_MAX,hiderdot_MAX,OutputOUT; / 各个层的结点输出int i,j,k,n,flag; /flag为是否继续修改权矩阵标志量double temp; for (i = 0; i IN; i+) / 复制结构体中的权矩阵for (j
10、= 0; j dot; j+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 9 页 - - - - - - - - - vij = (*bp).vij; for (i = 0; i dot; i+) for (j = 0; j dot; j+) uij = (*bp).uij; for (i = 0; i dot; i+) for (j = 0; j OUT; j+) wij = (*bp).wij; for (n = 0;nLoop_MAX; n+) / 反向误差计
11、算 flag=1; for (i= 0; i NUM; i+) for (k= 0; k dot; k+) / 计算输入层输出向量 temp = 0; for (j = 0; j IN; j+) temp += xij * vjk; Inputk = fnet(temp); for (k= 0; k dot; k+) / 计算隐藏层输出向量 temp = 0; for (j = 0; j dot; j+) temp +=Inputj * ujk; hiderk = fnet(temp); for (k = 0; k OUT; k+) / 计算输出层输出向量 temp = 0; for (j =
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年Bp网络算法源代码 2022 Bp 网络 算法 源代码

限制150内