bp神经网络详细步骤C#实现Word下载.docx
- 文档编号:18586559
- 上传时间:2022-12-28
- 格式:DOCX
- 页数:14
- 大小:17.01KB
bp神经网络详细步骤C#实现Word下载.docx
《bp神经网络详细步骤C#实现Word下载.docx》由会员分享,可在线阅读,更多相关《bp神经网络详细步骤C#实现Word下载.docx(14页珍藏版)》请在冰豆网上搜索。
double[]o2;
//输出层的输入
publicdouble[,]w;
//权值矩阵w,这是输入层与隐藏层之间的权值矩阵
publicdouble[,]v;
//权值矩阵V,这是隐藏层与输出层之间的权值矩阵
publicdouble[,]dw;
//权值矩阵w
publicdouble[,]dv;
//权值矩阵V
publicdoublerate;
//学习率
publicdouble[]b1;
//隐层阈值矩阵
publicdouble[]b2;
//输出层阈值矩阵
publicdouble[]db1;
publicdouble[]db2;
double[]pp;
//隐藏层的误差
double[]qq;
//输出层的误差
double[]yd;
//输出层的教师数据,所谓教师数据就是实际数据而已!
publicdoublee;
//均方误差
doublein_rate;
//归一化比例系数
//用于确定隐藏层的神经细胞数
publicintcomputeHideNum(intm,intn)
doubles=Math.Sqrt(0.43*m*n+0.12*n*n+2.54*m+0.77*n+0.35)+0.51;
intss=Convert.ToInt32(s);
return((s-(double)ss)>
0.5)?
ss+1:
ss;
}
publicBpNet(double[,]p,double[,]t)
//构造函数逻辑
R=newRandom();
this.inNum=p.GetLength
(1);
this.outNum=t.GetLength
(1);
this.hideNum=computeHideNum(inNum,outNum);
//this.hideNum=18;
this.sampleNum=p.GetLength(0);
Console.WriteLine("
输入节点数目:
"
+inNum);
隐层节点数目:
+hideNum);
输出层节点数目:
+outNum);
Console.ReadLine();
//将这些矩阵规定好矩阵大小
x=newdouble[inNum];
x1=newdouble[hideNum];
x2=newdouble[outNum];
o1=newdouble[hideNum];
o2=newdouble[outNum];
w=newdouble[inNum,hideNum];
v=newdouble[hideNum,outNum];
dw=newdouble[inNum,hideNum];
dv=newdouble[hideNum,outNum];
//阈值
b1=newdouble[hideNum];
b2=newdouble[outNum];
db1=newdouble[hideNum];
db2=newdouble[outNum];
//误差
pp=newdouble[hideNum];
qq=newdouble[outNum];
yd=newdouble[outNum];
//输出层的教师数据
//初始化w
for(inti=0;
i<
inNum;
i++)
for(intj=0;
j<
hideNum;
j++)
//NextDouble返回一个介于0.0和1.0之间的随机数。
w[i,j]=(R.NextDouble()*2-1.0)/2;
//初始化v
outNum;
v[i,j]=(R.NextDouble()*2-1.0)/2;
rate=0.8;
e=0.0;
in_rate=1.0;
?
//训练函数
publicvoidtrain(double[,]p,double[,]t)
//★求p,t中的最大值
doublepMax=0.0;
//sampleNum为样本总数
for(intisamp=0;
isamp<
sampleNum;
isamp++)
//inNum是输入层的节点数(即神经细胞数)
if(Math.Abs(p[isamp,i])>
pMax)
pMax=Math.Abs(p[isamp,i]);
if(Math.Abs(t[isamp,j])>
pMax=Math.Abs(t[isamp,j]);
in_rate=pMax;
}//endisamp
//★数据归一化
x[i]=p[isamp,i]/in_rate;
yd[i]=t[isamp,i]/in_rate;
//计算隐层的输入和输出
o1[j]=0.0;
o1[j]+=w[i,j]*x[i];
//“权值”*“输入”的那个累加的过程
//这个b1[j]就是隐藏层的阈值,阈值就是一个输入为“-1”的累加值
x1[j]=1.0/(1.0+Math.Exp(-o1[j]-b1[j]));
//计算输出层的输入和输出
for(intk=0;
k<
k++)
o2[k]=0.0;
o2[k]+=v[j,k]*x1[j];
x2[k]=1.0/(1.0+Math.Exp(-o2[k]-b2[k]));
//计算输出层误差和均方差
//yd[k]是输出层的教师数据,所谓教师数据就是实际应该输出的数据而已
qq[k]=(yd[k]-x2[k])*x2[k]*(1.0-x2[k]);
e+=(yd[k]-x2[k])*(yd[k]-x2[k]);
//更新V,V矩阵是隐藏层与输出层之间的权值
v[j,k]+=rate*qq[k]*x1[j];
//计算隐层误差
//PP矩阵是隐藏层的误差
pp[j]=0.0;
//算法参考我的视频截图
pp[j]+=qq[k]*v[j,k];
pp[j]=pp[j]*x1[j]*(1-x1[j]);
//更新W
w[i,j]+=rate*pp[j]*x[i];
//更新b2,输出层的阈值
b2[k]+=rate*qq[k];
//更新b1,隐藏层的阈值
b1[j]+=rate*pp[j];
e=Math.Sqrt(e);
//均方差
//adjustWV(w,dw);
//adjustWV(v,dv);
}//endtrain
publicvoidadjustWV(double[,]w,double[,]dw)
w.GetLength(0);
w.GetLength
(1);
w[i,j]+=dw[i,j];
publicvoidadjustWV(double[]w,double[]dw)
w.Length;
w[i]+=dw[i];
//数据仿真函数
publicdouble[]sim(double[]psim)
x[i]=psim[i]/in_rate;
//in_rate为归一化系数
o1[j]=o1[j]+w[i,j]*x[i];
o2[k]=o2[k]+v[j,k]*x1[j];
x2[k]=in_rate*x2[k];
}?
returnx2;
}//endsim
//保存矩阵w,v
publicvoidsaveMatrix(double[,]w,stringfilename)
StreamWritersw=File.CreateText(filename);
sw.Write(w[i,j]+"
);
sw.WriteLine();
sw.Close();
//保存矩阵b1,b2
publicvoidsaveMatrix(double[]b,stringfilename)
b.Length;
sw.Write(b[i]+"
//读取矩阵W,V
publicvoidreadMatrixW(double[,]w,stringfilename)
StreamReadersr;
try?
sr=newStreamReader(filename,Encoding.GetEncoding("
gb2312"
));
Stringline;
inti=0;
while((line=sr.ReadLine())!
=null)?
string[]s1=line.Trim().Split('
'
s1.Length;
w[i,j]=Convert.ToDouble(s1[j]);
i++;
sr.Close();
catch(Exceptione)?
//Lettheuserknowwhatwentwrong.
Thefilecouldnotberead:
Console.WriteLine(e.Message);
//读取矩阵b1,b2
publicvoidreadMatrixB(double[]b,stringfilename)
{?
b[i]=Convert.ToDouble(line);
}//endbpnet
}//endnamespace
//主调用程序
///Class1的摘要说明。
classClass1
///应用程序的主入口点。
[STAThread]
staticvoidMain(string[]args)
//0.1399,0.1467,0.1567,0.1595,0.1588,0.1622,0.1611,0.1615,0.1685,0.1789,0.1790
//double[,]p1=newdouble[,]{{0.05,0.02},{0.09,0.11},{0.12,0.20},{0.15,0.22},{0.20,0.25},{0.75,0.75},{0.80,0.83},{0.82,0.80},{0.90,0.89},{0.95,0.89},{0.09,0.04},{0.1,0.1},{0.14,0.21},{0.18,0.24},{0.22,0.28},{0.77,0.78},{0.79,0.81},{0.84,0.82},{0.94,0.93},{0.98,0.99}};
//double[,]t1=newdouble[,]{{1,0},{1,0},{1,0},{1,0},{1,0},{0,1},{0,1},{0,1},{0,1},{0,1},{1,0},{1,0},{1,0},{1,0},{1,0},{0,1},{0,1},{0,1},{0,1},{0,1}};
//p1是输入的信息,一共5组,输入层为六个节点,p1[5][6]
double[,]p1=newdouble[,]{
{0.1399,0.1467,0.1567,0.1595,0.1588,0.1622},
{0.1467,0.1567,0.1595,0.1588,0.1622,0.1611},
{0.1567,0.1595,0.1588,0.1622,0.1611,0.1615},
{0.1595,0.1588,0.1622,0.1611,0.1615,0.1685},
{0.1588,0.1622,0.1611,0.1615,0.1685,0.1789}};
//t1是输出信息,一共6组,t1[6][1]
double[,]t1=newdouble[,]{
{0.1622},
{0.1611},
{0.1615},
{0.1685},
{0.1789},
{0.1790}};
BpNetbp=newBpNet(p1,t1);
intstudy=0;
do
study++;
bp.train(p1,t1);
//bp.rate=0.95-(0.95-0.3)*study/50000;
//Console.Write("
第"
+study+"
次学习:
//Console.WriteLine("
均方差为"
+bp.e);
}while(bp.e>
0.001&
&
study<
50000);
Console.Write("
bp.saveMatrix(bp.w,"
w.txt"
bp.saveMatrix(bp.v,"
v.txt"
bp.saveMatrix(bp.b1,"
b1.txt"
bp.saveMatrix(bp.b2,"
b2.txt"
//double[,]p2=newdouble[,]{{0.05,0.02},{0.09,0.11},{0.12,0.20},{0.15,0.22},{0.20,0.25},{0.75,0.75},{0.80,0.83},{0.82,0.80},{0.90,0.89},{0.95,0.89},{0.09,0.04},{0.1,0.1},{0.14,0.21},{0.18,0.24},{0.22,0.28},{0.77,0.78},{0.79,0.81},{0.84,0.82},{0.94,0.93},{0.98,0.99}};
double[,]p2=newdouble[,]{
{0.1622,0.1611,0.1615,0.1685,0.1789,0.1790}};
intaa=bp.inNum;
intbb=bp.outNum;
intcc=p2.GetLength(0);
double[]p21=newdouble[aa];
double[]t2=newdouble[bb];
for(intn=0;
n<
cc;
n++)
aa;
p21[i]=p2[n,i];
t2=bp.sim(p21);
t2.Length;
Console.WriteLine(t2[i]+"
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- bp 神经网络 详细 步骤 C# 实现