BP神经网络C语言代码_可以直接VC上运行
// 个完整的C 语言的BP 神经网络代码,可以直接在VC++上运行。
#include
#include
#include
#include
#define Data 820 //样本数据量
#define In 2 // 输入
#define Out 1 //输出
#define Neuron 8 //神经元
#define TrainC 20000 //训练次数
#define A 0.2
#define B 0.4
#define a 0.2
#define b 0.3
double d_in[Data][In],d_out[Data][Out]; //样本输出和输出
double w [Neuron][In],o[Neuron],v[Out][Neuron]; //神经元的权值
double Maxin[In],Minin[In],Maxout[Out],Minout[Out];
double OutputData[Out]; //神经元的输出
double dv[Out][Neuron],dw[Neuron][In] ; //神经元的权值的修正量
double e;
//生成随机样本输入数据,输出数据根据输入来设置
void writeTest()
{
FILE *fp1,*fp2;
double r1,r2;
int i;
srand ((unsigned)time(NULL));
if((fp1=fopen ("in.txt","w"))==NULL)
{
printf("can not open 1 the in file\n");
exit(0);
}
if((fp2=fopen ("out.txt","w"))==NULL)
{
printf("can not open the out file\n");
exit(0);
}
for (i=0;i
{
r1=rand()%1000/100.0;
r2=rand()%1000/100.0;
fprintf(fp1,"%lf %lf\n",r1,r2);
fprintf(fp2,"%lf \n",r1*r2);
}
fclose(fp1);
fclose(fp2);
}
void readData(){
FILE *fp1,*fp2;
int i,j;
if((fp1=fopen ("in.txt","r"))==NULL)
{
printf("can not open the 2 in file\n");
exit(0);
}
for (i=0;i
for (j=0; j
fscanf(fp1,"%lf",&d_in[i][j]);
fclose(fp1);
if((fp2=fopen ("out.txt","r"))==NULL)
{
printf("can not open the out file\n");
exit(0);
}
for (i=0;i
for (j=0; j
fscanf(fp1,"%lf",&d_out[i][j]);
fclose(fp2);
}
//初始化神经网络
void initBPNework ()
{
int i,j;
//输入数据最大值,最小值
for (i=