# include <conio.h>
# include <stdlib.h>
# include <time.h>
# include <math.h>
# include <stdio.h>
# include <string.h>
/*********************************/
# define inpoints 2
# define outpoints 1
# define defaultpoints 8
# define datagroup 500
# define testdatagroup 1000
# define ALFA 0.25
# define BETA 0.1
char * datafile = "data.dat";
/*********************************/
void make_wv( w, v, dlta_v, dlta_w) /*随机赋初值*/
float w [defaultpoints] [inpoints + 1], v [defaultpoints + 1];
float dlta_v [defaultpoints + 1], dlta_w [defaultpoints] [inpoints + 1];
{
int i, j;
randomize();
for (i = 0; i < defaultpoints; i++ ) {
for (j = 0; j <= inpoints; j++ ) {
w[i][j] = + ((float) rand()/RAND_MAX - (float) rand()/RAND_MAX);/**/
}
}
for (i = 0; i <= defaultpoints; i++ ) {
v[i] = + ((float) rand()/RAND_MAX - (float) rand()/RAND_MAX);
}
for (i = 0; i < defaultpoints; i++ ) {
for (j = 0; j <= inpoints; j++ ) {
dlta_w [i] [j] = 0.0;
}
}
for (i = 0; i <= defaultpoints; i++ ) {
dlta_v [i] = 0.0;
}
}
void make_x(fp, x, d, group) /*产生训练样本或测试样本*/
FILE *fp;
int group;
float x[datagroup][inpoints + 1],d[datagroup];
{
int i, j;
randomize();
for (i = 0; i < group; i++ ) {
for (j = 0; j < inpoints; j++ ) {
x[i][j] = (float) rand()/RAND_MAX - (float) rand()/RAND_MAX;
}
x[i][inpoints] = 1.0;
if (x[i][0]*x[i][1] < 1e-6) d[i]=0;
else d[i]=1;
}
for (i = 0; i < group; i++ ) {
for (j = 0; j < inpoints; j++ ) {
fprintf(fp,"%f ", x[i][j]);
}
fprintf(fp,"%f ", d[i]);
fprintf(fp,"\n");
}
}
void out_sub1 (w, v ,x ,out, y, k) /*正向传播*/
float w [defaultpoints] [inpoints + 1], v [defaultpoints + 1], x [datagroup] [inpoints + 1];
float out[defaultpoints + 1], * y;
int k;
{
int i,j;
float (* wp) [inpoints + 1],(* xp) [inpoints +1];
float sum1,sum2=0;
wp = w; xp = x; * y = 0;
for (i=0; i < defaultpoints; i++) {
sum1 = 0;
for (j = 0; j <= inpoints; j++ ) {
sum1 += (* (* (wp+i)+j)) * (* (* (xp+k)+j));
}
out[i] = 1.0/(1.0 + exp(-sum1));
}
out[defaultpoints]=1.0;
for (i=0; i <= defaultpoints; i++) {
sum2 = sum2 + v [i] * out[i];
}
*y = 1.0/(1.0 + exp(-sum2));
}
void out_sub2(v, x, o, d, y, k, dlta_v, dlta_w)
float v [defaultpoints], x [datagroup] [inpoints + 1],* d;
float * y, o [datagroup] [defaultpoints + 1];
int k;
float dlta_v [defaultpoints + 1], dlta_w [defaultpoints] [inpoints + 1]; /*反向传播*/
{
int i,j;
for(i = 0; i < defaultpoints; i++){
for(j = 0; j <= inpoints; j++){
dlta_w [i] [j] += (d [k] - y [k]) * y[k] * (1-y[k])* v [i]* o [k] [i]
* (1 - o [k] [i]) * x [k] [j];
/* printf(" dlta_w [%d] [%d]= %f\n",i,j,dlta_w [i] [j]);*/
}
}
for(i = 0; i <= defaultpoints; i++){
dlta_v [i] += (d [k] - y [k]) * y[k] * (1-y[k]) * o [k] [i];
/* printf(" dlta_v [%d]= %f\n",i,dlta_v [i]);*/
}
}
void myw (w, v, dlta_w, dlta_v, x, d, o, y, n, fp) /*写权值*/
float w [defaultpoints] [inpoints + 1], v [defaultpoints + 1];
float dlta_w [defaultpoints] [inpoints + 1], dlta_v [defaultpoints + 1];
float x [datagroup] [inpoints + 1], d [datagroup];
float o [datagroup] [defaultpoints + 1], y [datagroup];
int n;
FILE * fp;
{
int i,j;
fprintf(fp, "\n\n");
for(j=0; j < defaultpoints; j++){
for(i = 0; i <= inpoints; i++){
fprintf(fp,"%f ", w[j][i]);
}
fprintf(fp,"\n");
}
for(j=0; j <= defaultpoints; j++){
fprintf(fp,"%f ", v[j]);
}
fprintf(fp,"\n");
for(j=0; j < defaultpoints; j++){
for(i = 0; i <= inpoints; i++){
fprintf(fp,"%f ", dlta_w[j][i]);
}
fprintf(fp,"\n");
}
for(j=0; j <= defaultpoints; j++){
fprintf(fp,"%f ", dlta_v[j]);
}
fprintf(fp,"\n");
for(i=0; i < datagroup; i++){
for(j=0;j <= inpoints; j++){
fprintf(fp,"%8.6f ",x [i] [j]);
}
fprintf(fp,"%10.8f %10.8f",d[i],y[i]);
fprintf(fp,"\n");
}
for(i=0;i < datagroup; i++){
for(j=0;j<=defaultpoints;j++){
fprintf(fp,"%f ",o[i][j]);
}
}
fprintf(fp,"\n%d",n);
}
void myr (w, v, dlta_w, dlta_v, x, d, o, y, n, fp) /*读权值*/
float w [defaultpoints] [inpoints + 1], v [defaultpoints + 1];
float dlta_w [defaultpoints] [inpoints + 1], dlta_v [defaultpoints + 1];
float x [datagroup] [inpoints + 1], d [datagroup];
float o [datagroup] [defaultpoints + 1], y [datagroup];
int *n;
FILE * fp;
{
int i,j;
for(j=0; j < defaultpoints; j++){
for(i = 0; i <= inpoints; i++){
fscanf(fp,"%f", &w[j][i]);
}
}
for(j=0; j <= defaultpoints; j++){
fscanf(fp,"%f", &v[j]);
}
for(j=0; j < defaultpoints; j++){
for(i = 0; i <= inpoints; i++){
fscanf(fp,"%f", &dlta_w[j][i]);
}
}
for(j=0; j <= defaultpoints; j++){
fscanf(fp,"%f", &dlta_v[j]);
}
for(i=0; i < datagroup; i++){
for(j=0;j <= inpoints; j++){
fscanf(fp,"%8.6f",&x [i] [j]);
}
fscanf(fp,"%10.8f%10.8f",&d[i],&y[i]);
}
for(i=0;i < datagroup; i++){
for(j=0; j <= defaultpoints; j++){
fscanf(fp,"%f",&o[i][j]);
}
}
fscanf(fp,"\n%d",n);
}
void main()
{
int h,i,j,k,n,ch,count=0;
float w [defaultpoints] [inpoints + 1], dlta_w [defaultpoints] [inpoints + 1];
float x [datagroup] [inpoints + 1],xc[inpoints + 1],tstx [testdatagroup] [inpoints + 1];
float o [datagroup] [defaultpoints + 1], outc[defaultpoints + 1] ,y [datagroup];
float v [defaultpoints + 1], dlta_v [defaultpoints + 1];
float d [datagroup],tstd [testdatagroup];
float e,earr,rate;
float yc,sigma;
FILE *fp, *fpq, *fpt;
printf("C:读数据文件QR.DAT并进行数据拟合运算用于测试\n");
printf("其它键:开始执行程序\n");
ch = toupper(getche());
if (ch=='C'){
if((fpq=fopen("qr.dat","rt")) == NULL){ /*读qr.dat*/
printf("open file QR.DAT error");
exit(0);
}
if((fpt=fopen("testdata.dat","wt")) == NULL){
printf("open file testdata.DAT error");
exit(0);
}
make_x(fpt,tstx,tstd,testdatagroup); /*产生测试样本并写进testdata.dat*/
myr(w, v, dlta_w, dlta_v, x, d, o, y, &n, fpq);
fscanf(fpq, "%f", &e);
fclose(fpq);
/*测试并计算和输出正确率*/
for(h=0; h < testdatagroup; h++){
out_sub1 (w, v ,tstx ,outc, &yc, h);
fprintf(fpt,"\n%f ",yc);
if((tstd[h] >=0.5)&&(yc >=0.5)||((tstd[h] < 0.5)&&(yc <0.5)))
count+=1;
}
rate = ((float)count/(float)testdatagroup)*100;
printf("\ncorrect rate=%f%%\n",rate);
fprintf(fpt,"\n %f%%",rate);
fclose(fpt);
exit(0);
}
else{
make_wv(w, v, dlta_v, dlta_w);
printf("\n------w------\n"); /*屏幕输出初置*/
for (j=0; j < defaultpoints; j++){
for (i=0; i <= inpoints; i++){
printf("%f",w [j] [i]);
}
printf("\n");
}
printf("\n------v------\n");
for (j=0; j <= defaultpoints; j++){
printf("%f",v [j]);
}
printf("\n-----dlta_w-----\n");
for (j=0; j < defaultpoints; j++){
for (i=0; i <= inpoints; i++){
printf("%f",dlta_w [j] [i]);
}
printf("\n");
}
printf("\n------dlta_v------\n");
for (j=0; j <= defaultpoints; j++){
printf("%f",dlta_v [j]);
}
printf("\n");
if((fp=fopen(datafile,"wt")) == NULL){
printf("open file %s error",datafile);
exit(0);
}
/* printf("\n------1------\n");*/
make_x(fp,x,d,datagroup); /*产生训练样本并写入data.dat*/
fclose(fp);
n=1;
}
/*训练*/
do{
/* printf("------do-1------\n");*/
for (i =
没有合适的资源?快使用搜索试试~ 我知道了~
广义异或bp算法.rar_bp标准程序_bp算法 c++_符号函数
共9个文件
bak:2个
cpp:1个
plg:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 200 浏览量
2022-09-22
16:37:08
上传
评论
收藏 16KB RAR 举报
温馨提示
本文件为用C语言实现的可实现广义异或问题的bp神经网络算法。该问题是对标准异或问题的推广。在标准异或问题中,输入X1和X2取离散量-1或+1,在广义异或问题中,输入(X1,X2)可以在区间[-1,+1] X [-1, +1]内任意取值,而输出为Y=sign(x1,x2),其中sign()为符号函数,在区间[-1,+1] X [-1, +1]内随机产生500个训练样本.本程序用标准BP网实现该分类问题.
资源推荐
资源详情
资源评论
收起资源包目录
广义异或bp算法.rar (9个子文件)
www.pudn.com.txt 218B
bp
BP.CPP 10KB
BP_J.BAK 10KB
BP.BAK 12KB
bp.ncb 49KB
BP.DSW 527B
bp.plg 1KB
BP.OPT 48KB
Debug
BP.DSP 3KB
共 9 条
- 1
资源评论
alvarocfc
- 粉丝: 126
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (源码)基于C++的简易操作系统模拟器.zip
- (源码)基于ROS和PCL的激光与UWB定位仿真系统.zip
- (源码)基于Arduino的iBeacon发送系统.zip
- (源码)基于C语言和汇编语言的简单操作系统内核.zip
- (源码)基于Spring Boot框架的AntOA后台管理系统.zip
- (源码)基于Arduino的红外遥控和灯光控制系统.zip
- (源码)基于STM32的简易音乐键盘系统.zip
- (源码)基于Spring Boot和Vue的管理系统.zip
- (源码)基于Spring Boot框架的报表管理系统.zip
- (源码)基于树莓派和TensorFlow Lite的智能厨具环境监测系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功