神经网络BP算法
神经网络BP算法(vC++程序)文件输入输出目录为:F:\BP\
训练样本文件名:训练样本.txt
值为:
1
1
-1
1
-1
1
0
1
0
1
输出文件名为:阈值.txt 权值.txt
=========================
#include "stdlib.h"
#include "math.h"
#include "conio.h"
#include "stdio.h"
#define N 2 /*/学习样本个数*/
#define IN 3 /*/输入层神经元数目*/
#define HN 3 /*/隐层神经元数目*/
#define ON 2 /*/输出层神经元数目*/
#define Z 20 /*/旧权值保存-》每次study的权值都保存下来*/
double P; /*/单个样本输入数据*/
double T; /*/单个样本教师数据*/
double W; /*/输入层至隐层权值*/
double V; /*/隐层至输出层权值*/
double X; /*/隐层的输入*/
double Y; /*/输出层的输入*/
double H; /*/隐层的输出*/
double O; /*/输出层的输出*/
double YU_HN; /*/隐层的阈值*/
double YU_ON; /*/输出层的阈值*/
double err_m; /*/第m个样本的总误差*/
double a; /*/输出层至隐层的学习效率*/
double b; /*/隐层至输入层学习效率*/
double alpha;/*/动量因子,改进型bp算法使用*/
double d_err;
FILE *fp;
/*定义一个放学习样本的结构*/
struct {
double input;
double teach;
}Study_Data;
/*改进型bp算法用来保存每次计算的权值*/
struct {
double old_W;
double old_V;
}Old_WV;
int Start_Show()
{
clrscr();
printf("\n ***********************\n");
printf(" * Welcome to use *\n");
printf(" *this program of *\n");
printf(" *calculating the BP *\n");
printf(" * model! *\n");
printf(" * Happy every day!*\n");
printf(" ***********************\n");
printf("\n\nBefore starting,please read the follows carefully:\n\n");
printf(" 1.Please ensure the Path of the '训练样本.txt'(xunlianyangben.txt) is \ncorrect,like 'F:\BP\训练样本.txt'!\n");
printf(" 2.The calculating results will be saved in the Path of 'F:\\BP\\'!\n");
printf(" 3.The program will load 10 datas when running from 'F:\\BP\\训练样本.txt'!\n");
printf(" 4.The program of BP can study itself for no more than 30000 times.\nAnd surpassing the number,the program will be ended by itself in\npreventing running infinitely because of error!\n");
printf("\n\n\n");
printf("Now press any key to start...\n");
getch();
getch();
clrscr();
}
int End_Show()
{
printf("\n\n---------------------------------------------------\n");
printf("The program has reached the end successfully!\n\nPress any key to exit!\n\n");
printf("\n ***********************\n");
printf(" * This is the end*\n");
printf(" * of the program which*\n");
printf(" * can calculate the BP*\n");
printf(" * model! *\n");
printf(" ***********************\n");
printf(" *Thanks for using!*\n");
printf(" * Happy every day!*\n");
printf(" ***********************\n");
getch();
exit(0);
}
GetTrainingData() /*OK*/
{ int m,i,j;
int datr;
if((fp=fopen("f:\\bp\\训练样本.txt","r"))==NULL) /*读取训练样本*/
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
m=0;
i=0;
j=0;
while(fscanf(fp,"%d",&datr)!=EOF)
{j++;
if(j<=(N*IN))
{if(i {
Study_Data.input=datr;
/*printf("\nthe Study_Datat[%d].input[%d]=%f\n",m,i,Study_Data.input);getch();*//*use to check the loaded training datas*/
}
if(m==(N-1)&&i==(IN-1))
{
m=0;
i=-1;
}
if(i==(IN-1))
{
m++;
i=-1;
}
}
else if((N*IN) {if(i {Study_Data.teach=datr;
/*printf("\nThe Study_Data[%d].teach[%d]=%f",m,i,Study_Data.teach);getch();*//*use to check the loaded training datas*/
}
if(m==(N-1)&&i==(ON-1))
printf("\n");
if(i==(ON-1))
{m++;
i=-1;
}
}
i++;
}
fclose(fp);
printf("\nThere are [%d] datats that have been loaded successfully!\n",j);
/*show the data which has been loaded!*/
printf("\nShow the data which has been loaded as follows:\n");
for(m=0;m {for(i=0;i {printf("\nStudy_Data[%d].input[%d]=%f",m,i,Study_Data.input);
}
for(j=0;j {printf("\nStudy_Data[%d].teach[%d]=%f",m,j,Study_Data.teach);
}
}
printf("\n\nPress any key to start calculating...");
getch();
return 1;
}
/*///////////////////////////////////*/
/*初始化权、阈值子程序*/
/*///////////////////////////////////*/
initial()
{int i;
int ii;
int j;
int jj;
int k;
int kk;
/*隐层权、阈值初始化*/
for(i=0;i {
for(j=1;j {W=(double)((rand()/32767.0)*2-1); /*初始化输入层到隐层的权值,随机模拟0 和 1 -1 */
printf("w[%d][%d]=%f\n",i,j,W);
}
}
for(ii=0;ii {
for(jj=0;jj {V= (double)((rand()/32767.0)*2-1); /*初始化隐层到输出层的权值,随机模拟0 和 1 -1*/
printf("V[%d][%d]=%f\n",ii,jj,V);
}
}
for(k=0;k {
YU_HN = (double)((rand()/32767.0)*2-1);/*隐层阈值初始化 ,-0.01 ~ 0.01 之间*/
printf("YU_HN[%d]=%f\n",k,YU_HN);
}
for(kk=0;kk {
YU_ON = (double)((rand()/32767.0)*2-1); /*输出层阈值初始化 ,-0.01 ~ 0.01 之间*/
}
return 1;
}/*子程序initial()结束*/
/*//////////////////////////////////////////*/
/*第m个学习样本输入子程序*/
/*/////////////////////////////////////////*/
input_P(int m)
{ int i,j;
for(i=0;i{P=Study_Data.input;
printf("P[%d]=%f\n",i,P);
}
/*获得第m个样本的数据*/
return 1;
}/*子程序input_P(m)结束*/
/*/////////////////////////////////////////*/
/*第m个样本教师信号子程序*/
/*/////////////////////////////////////////*/
input_T(int m)
{int k;
for(k=0;kT=Study_Data.teach;
return 1;
}/*子程序input_T(m)结束*/
H_I_O()
{
double sigma;
int i,j;
for(j=0;j{
sigma=0;
for(i=0;i {sigma+=W*P;/*求隐层内积*/
}
X=sigma-YU_HN;/*求隐层净输入,为什么减隐层的阀值*/
H=1.0/(1.0+exp(-X));/*求隐层输出 siglon算法*/
}
return 1;
}/*子程序H_I_O()结束*/
O_I_O()
{int k;
int j;
double sigma;
for(k=0;k {
sigma=0.0;
for(j=0;j{
sigma+=V*H;
}
Y=sigma-YU_ON;
O=1.0/(1.0+exp(-Y));
}
return 1;
}
int Err_O_H(int m)
{int k;
double abs_err;
double sqr_err=0;
for (k=0;k{
abs_err=T-O;
sqr_err+=(abs_err)*(abs_err);
d_err=abs_err*O*(1.0-O);
err_m=sqr_err/2;
}
return 1;
}
double e_err;
int Err_H_I()
{
int j,k;
double sigma;
for(j=0;j {
sigma=0.0;
for(k=0;k{
sigma=d_err*V;
}
e_err=sigma*H*(1-H);
}
return 1;
}
saveWV(int m)
{int i;
int ii;
int j;
int jj;
for(i=0;i{
for(j=0;j {
Old_WV.old_W = W;
}
}
for(ii=0;ii{
for(jj=0;jj {
Old_WV.old_V = V;
}
}
return 1;
}
int Delta_O_H(int n) /*(int m,int n)*/
{int k,j;
if(n<1)/*n<=1*/
{
for (k=0;k {
for (j=0;j {
V=V+a*d_err*H;
}
YU_ON+=a*d_err;
}
}
else if(n>1)
{
for (k=0;k {
for (j=0;j {
V=V+a*d_err*H+alpha*(V-Old_WV[(n-1)].old_V);
}
YU_ON+=a*d_err;
}
}
return 1;
}
Delta_H_I(int n) /*(int m,int n)*/
{ int i,j;
if(n<=1) /*n<=1*/
{
for (j=0;j {
for (i=0;i {
W=W+b*e_err*P;
}
YU_HN+=b*e_err;
}
}
else if(n>1)
{
for(j=0;j {
for(i=0;i {
W=W+b*e_err*P+alpha*(W-Old_WV[(n-1)].old_W);
}
YU_HN+=b*e_err;
}
}
return 1;
}
double Err_Sum()
{int m;
double total_err=0;
for(m=0;m {
total_err+=err_m;
}
return total_err;
}
void savequan()
{ int i,j,k;
int ii,jj,kk;
if((fp=fopen("f:\\bp\\权值.txt","a"))==NULL) /*save the result at f:\hsz\bpc\*.txt*/
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
fprintf(fp,"Save the result of “权值”(quanzhi) as follows:\n");
for(i=0;i {
for(j=0;jfprintf(fp,"W[%d][%d]=%f\n",i,j,W);
}
fprintf(fp,"\n");
for(ii=0;ii {
for(jj=0;jjfprintf(fp,"V[%d][%d]=%f\n",ii,jj,V);
}
fclose(fp);
printf("\nThe result of “权值.txt”(quanzhi) has been saved successfully!\nPress any key to continue...");
getch();
if((fp=fopen("f:\\bp\\阈值.txt","a"))==NULL) /*save the result at f:\hsz\bpc\*/
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
fprintf(fp,"Save the result of “输出层的阈值”(huozhi) as follows:\n");
for(k=0;k fprintf(fp,"YU_ON[%d]=%f\n",k,YU_ON);
fprintf(fp,"\nSave the result of “隐层的阈值为”(huozhi) as follows:\n");
for(kk=0;kkfprintf(fp,"YU_HN[%d]=%f\n",kk,YU_HN);
fclose(fp);
printf("\nThe result of “阈值.txt”(huozhi) has been saved successfully!\nPress any key to continue...");
getch();
}
/**********************/
/**程序入口,即主程序**/
/**********************/
void main()
{double Pre_error;
double sum_err;
int study;
int flag;
flag=30000;
a=0.7;
b=0.7;
alpha=0.9;
study=0;
Pre_error=0.0001;/*实际值为Pre_error=0.0001;*/
Start_Show();
GetTrainingData();
initial();
do
{int m;
++study;
for(m=0;m {
input_P(m);
input_T(m);
H_I_O();
O_I_O();
Err_O_H(m);
Err_H_I();
saveWV(m); /****************/
Delta_O_H(m); /*(m,study)*/
Delta_H_I(m); /*(m,study)*/
}
sum_err=Err_Sum();
printf("sum_err=%f\n",sum_err);
printf("Pre_error=%f\n\n",Pre_error);
if(study>flag)
{
printf("\n*******************************\n");
printf("The program is ended by itself because of error!\nThe learning times is surpassed!\n");
printf("*****************************\n");
getch();
break;
}
}while (sum_err>Pre_error);
printf("\n****************\n");
printf("\nThe program have studyed for [%d] times!\n",study);
printf("\n****************\n");
savequan(); /*save the results*/
End_Show();
}
==========================
权值.txt
{Save the result of “权值”(quanzhi) as follows:
W=0.350578
W=-1.008697
W=-0.962250
W=0.055661
W=-0.372367
W=-0.890795
W=0.129752
W=-0.332591
W=-0.521561
V=-2.932654
V=-3.720583
V=-2.648183
V=2.938970
V=1.633281
V=1.944077
}
阈值.txt
{Save the result of “输出层的阈值”(huozhi) as follows:
YU_ON=-4.226843
YU_ON=1.501791
Save the result of “隐层的阈值为”(huozhi) as follows:
YU_HN=-0.431459
YU_HN=0.452127
YU_HN=0.258449
}
==================================
以上程序为VC++的程序改制而成!
[ 本帖最后由 风花雪月 于 2006-11-29 07:53 编辑 ] 算法很简单,编写和调试程序却不太容易,楼主辛苦了。
赞一个
向你学习
页:
[1]