# define MAX1 200
# define MAX2 10000
# define MAX3 3000 /*各个网络的最大训练次数*/
# define A 30.0 /*A,B为NN1网络输出层激励函数参数*/
# define B 10.0
# define COEF3 0.000035 /*NN2网络的学习效率*/
# define BCOEF3 0.00005 /*NN2网络的阈值调整效率*/
# define COEF2 0.0035 /*NN1网络的学习效率*/
# define COEF1 0.8 /*NNMF网络的学习效率*/
# define ERROR3 0.002 /*各网络训练的允许误差*/
# define ERROR2 0.002
# define ERROR1 0.000028
# define ACCURACY 0.0005 /*网络要求精度*/
# define ACCURACY1 0.00004
# include "STDIO.H"
# include "MATH.H"
# include "STDLIB.H"
/* NNMF网络学习样本及参数变量说明*/
int sample1[20][3]={{1,3,1},{1,5,2},{1,1,3},{1,3,4},{1,5,5},{5,1,4},{5,3,3},{5,5,2},{5,1,1},
{5,3,2},{1,5,3},{1,1,4},{1,3,5},{1,1,3},{5,3,2},{5,5,1},{5,1,2},{5,3,3},{5,5,4}};
double w1[5][10][10],wc1[5][10][10],b1[5][10],bc1[5][10];
double o1[5][10],netin1[5][10],d1[5][10],differ1;
int count1,a1;
double is1s;
/*NN1网络样本及参数变量说明*/
double sample2[10][4]={{1,3,1,11.110},{1,5,2,6.521},{1,1,3,10.190},{1,3,4,6.043},{1,5,5,5.242},
{1,5,3,5.724},{1,1,4,9.766},{1,3,5,5.870},{1,5,4,5.406},{1,1,3,10.190}};
double w2[5][10][10],wc2[5][10][10],b2[5][10],bc2[5][10];
double o2[5][10],netin2[5][10],d2[5][10],differ2;
int count2,a2;
double is2s;
/*NN2网络样本及参数变量说明*/
double sample3[10][4]={{5,1,4,19.020},{5,3,3,14.150},{5,5,2,14.360},{5,3,3,14.150},{5,3,2,15.390},
{5,3,2,15.390}, {5,5,1,19.6680},{5,1,2,21.060},{5,5,3,14.150},{5,5,4,12.680}};
double w3[5][10][10],wc3[5][10][10],b3[5][10],bc3[5][10];
double o3[5][10],netin3[5][10],d3[5][10],differ3;
int count3,a3;
double is3s;
/*计算NN2网络隐含层和输出层的输出*/
void netout3(int m,int n)
{
int i,j,l;
for(j=2,i=2;j<=m;j++)
{ netin3[i][j]=0.0;
for (l=1;l<=m;l++)
netin3[i][j]=netin3[i][j]+o3[i-1][l]*w3[i][l][j];
netin3[i][j]=netin3[i][j]-b3[i][j];
o3[i][j]=0.5*(exp(netin3[i][j])-exp(-netin3[i][j]));
}
for (j=1,i=3;j<=m;j++)
{ netin3[i][j]=0.0;
for (l=1;l<=m;l++)
netin3[i][j]=netin3[i][j]+o3[i-1][l]*w3[i][l][j];
netin3[i][j]=netin3[i][j]-b3[i][j];
o3[i][j]=0.5*(exp(netin3[i][j])-exp(-netin3[i][j]));
}
for (j=1,i=4;j<=m;j++)
{ netin3[i][j]=0.0;
for (l=1;l<=m;l++)
netin3[i][j]=netin3[i][j]+o3[i-1][l]*w3[i][l][j];
netin3[i][j]=netin3[i][j]-b3[i][j];
o3[i][j]=0.5*(exp(netin3[i][j])-exp(-netin3[i][j]));
}
}
/*计算NN2网络的反向传播误差*/
void calculd3(int m,int n)
{
int i,j,l;
double t;
a3=count3-1;
d3[4][1]=(o3[4][1]-sample3[a3][3])*0.5*(exp(netin3[4][1])+exp(-netin3[4][1]));
for (j=1,i=3;j<=m;j++)
{t=0.00;
for(l=1;l<=n;l++)
t=t+w3[i+1][j][l]*d3[i+1][l];
d3[i][j]=0.5*(exp(netin3[i][j])+exp(-netin3[i][j]))*t;
}
for (j=1,i=2;j<=m;j++)
{t=0.00;
for(l=1;l<=m;l++)
t=t+w3[i+1][j][l]*d3[i+1][l];
d3[i][j]=0.5*(exp(netin3[i][j])+exp(-netin3[i][j]))*t;
}
}
/*计算NN2网络权值W的调整量*/
void calculwc3(int m,int n)
{int i,j,l;
for (i=1,l=4;i<=m;i++)
for(j=1;j<=n;j++)
wc3[l][i][j]=-COEF3*d3[l][j]*o3[l-1][i]+0.5*wc3[l][i][j];
for (i=1,l=3;i<=m;i++)
for(j=1;j<=m;j++)
wc3[l][i][j]=-COEF3*d3[l][j]*o3[l-1][i]+0.5*wc3[l][i][j];
for (i=1,l=2;i<=m;i++)
for(j=1;j<=m;j++)
wc3[l][i][j]=-COEF3*d3[l][j]*o3[l-1][i]+0.5*wc3[l][i][j];
}
/*计算NN2网络阈值的调整量*/
void calculbc3(int m,int n)
{ int j;
for (j=1;j<=m;j++)
{
bc3[2][j]=BCOEF3*d3[2][j];
bc3[3][j]=BCOEF3*d3[3][j];
}
for(j=1;j<=n;j++)
bc3[4][j]=BCOEF3*d3[4][j];
}
/*计算NN2网络权值W*/
void changw3(int m,int n)
{int i,j;
for (i=1;i<=3;i++)
for(j=1;j<=m;j++)
w3[2][i][j]=w3[2][i][j]+wc3[2][i][j];
for (i=1;i<=m;i++)
for(j=1;j<=m;j++)
w3[3][i][j]=w3[3][i][j]+wc3[3][i][j];
for (i=1;i<=m;i++)
for(j=1;j<=n;j++)
w3[4][i][j]=w3[4][i][j]+wc3[4][i][j];
}
/*计算NN2网络阈值*/
void changb3(int m,int n)
{int j;
for (j=1;j<=m;j++)
{
b3[2][j]=b3[2][j]+bc3[2][j];
b3[3][j]=b3[3][j]+bc3[3][j];
}
for(j=1;j<=n;j++)
b3[4][j]=b3[4][j]+bc3[4][j];
}
/*清除NN2网络权值变化量Wc*/
void clearwc3(void)
{int i,j,l;
for (i=0;i<5;i++)
for (j=0;j<10;j++)
for (l=0;l<10;l++)
wc3[i][j][l]=0.00;
}
/*清除NN2网络阈值变化量bc*/
void clearbc3(void)
{int i,j;
for (i=0;i<5;i++)
for (j=0;j<10;j++)
bc3[i][j]=0.00;
}
/*初始化NN2网络权值W*/
void initialw3(void)
{ int i,j,l,x;
double weight;
for (i=0;i<5;i++)
for (j=0;j<10;j++)
for (l=0;l<10;l++)
{ x=100+random(400);
weight=(double)x/50000000.00;
w3[i][j][l]=weight;
}
}
/*初始化NN2网络阈值*/
void initialb3(void)
{int i,j,x;
double fazhi;
for (i=0;i<5;i++)
for (j=0;j<10;j++)
{x=100+random(400);
fazhi=(double)x/50000000.00;
b3[i][j]=fazhi;
}
}
/*计算NN2网络单个样本的误差*/
void calculdiffer3(void)
{a3=count3-1;
differ3=0.5*(o3[4][1]-sample3[a3][3])*(o3[4][1]-sample3[a3][3]);
}
/*计算NN2网络全体样本均方差*/
void calculis3(void)
{int i;
is3s=0.0;
for (i=0;i<=9;i++)
{o3[1][1]=sample3[i][0];o3[1][2]=sample3[i][1];o3[1][3]=sample3[i][2];
netout3(8,1);
is3s=is3s+(o3[4][1]-sample3[i][3])*(o3[4][1]-sample3[i][3]);
}
is3s=is3s/10;
}
/*训练NN2网络*/
void trainnn3(void)
{long int time;
int i,x3[4];
initialw3();
initialb3();
for (time=1;time<=MAX3;time++)
{count3=0;
while(count3<=9)
{o3[1][1]=sample3[count3][0];o3[1][2]=sample3[count3][1];
o3[1][3]=sample3[count3][2];
count3=count3+1;
clearwc3();
clearbc3();
netout3(8,1);
calculdiffer3();
while (differ3>ERROR3)
{
calculd3(8,1);
calculwc3(8,1);
calculbc3(8,1);
changw3(8,1);
changb3(8,1);
netout3(8,1);
calculdiffer3();
}
}
printf("This is %d times training NN2...\n",time);
calculis3();
printf("is3s==%f\n",is3s);
if(is3s<ACCURACY)
break;
}
}
/*计算NN1网络的各层输出*/
void netout2(int m,int n)
{
int i,j,l;
for (j=1,i=2;j<=m;j++)
{netin2[i][j]=0.0;
for(l=1;l<=3;l++)
netin2[i][j]=netin2[i][j]+o2[i-1][l]*w2[i][l][j];
netin2[i][j]=netin2[i][j]-b2[i][j];
o2[i][j]=A/(1+exp(-netin2[i][j]/B));
}
for (j=1,i=3;j<=m;j++)
{netin2[i][j]=0.0;
for(l=1;l<=m;l++)
netin2[i][j]=netin2[i][j]+o2[i-1][l]*w2[i][l][j];
netin2[i][j]=netin2[i][j]-b2[i][j];
o2[i][j]=A/(1+exp(-netin2[i][j]/B));
}
for (j=1,i=4;j<=n;j++)
{netin2[i][j]=0.0;
for(l=1;l<=m;l++)
netin2[i][j]=netin2[i][j]+o2[i-1][l]*w2[i][l][j];
netin2[i][j]=netin2[i][j]-b2[i][j];
o2[i][j]=A/(1+exp(-netin2[i][j]/B));
}
}
/*计算NN1网络的反向传播误差*/
void calculd2(int m,int n)
{
int i,j,l;
double t;
a2=count2-1;
d2[4][1]=(o2[4][1]-sample2[a2][3])*(A/B)*exp(-netin2[4][1]/B/pow(1+exp(-netin2[4][1]/B),2));
for (j=1,i=3;j<=m;j++)
{t=0.00;
for(l=1;l<=n;l++)
t=t+w2[i+1][j][l]*d2[i+1][l];
d2[i][j]=t*(A/B)*exp(-netin2[i][j]/B/pow(1+exp(-netin2[i][j]/B),2));
}
for (j=1,i=2;j<=m;j++)
{t=0.00;
for(l=1;l<=m;l++)
t=t+w2[i+1][j][l]*d2[i+1][l];
d2[i][j]=t*(A/B)*exp(-netin2[i][j]/B/pow(1+exp(-netin2[i][j]/B),2));
}
}
/*计算NN1网络的权值变化量*/
void calculwc2(int m,int n)
{
int i,j,l;
for (i=1,l=4;i<=m;i++)
for(j=1;j<=n;j++)
wc2[l][i][j]=-COEF2*d2[l][j]*o2[l-1][i]+0.5*wc2[l][i][j];
for (i=1,l=3;i<=m;i++)
for(j=1;j<=m;j++)
wc2[l][i][j]=-COEF2*d2[l][j]*o2[l-1][i]+0.5*wc2[l][i][j];
for (i=1,l=2;i<=3;i++)
for(j=1;j<=m;j++)
wc2[l][i][j]=-COEF2*d2[l][j]*o2[l-1][i]+0.5*wc2[l][i][j];
}
/*计算NN1网络各节点的阈值变化量*/
void calculbc2(int m,int n)
{int j;
for (j=1;j<=m;j++)
{bc2[2][j]=COEF2*d2[2][j];
bc2[3][j]=COEF2*d2[3][j];
}
for (j=1;j<=n;j++)
bc2[4][j]=COEF2*d2[4][j];
}
/*计算NN1网络的权值*/
void changw2(int m,int n)
{int k,i,j;
for (i=1,k=2;i<=3;i++)
w2[k][i][j]=w2[k][i][j]+wc2
评论0