• 大小: 4.77MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-04
  • 语言: 其他
  • 标签: C++  

资源简介

利用BP神经网络实现f(x)=sin(x)+cos(x)的拟合逼近

资源截图

代码片段和文件信息

#include“BP.h“


//
BP::BP()
{
srand(time(NULL));
//BP网络结构
input_points=1;
output_points=1;
middle_points=yince;
//输入
for(int i=0;i {
W_ik[i]=rand()%((unsigned int)(1/calulate_error))*calulate_error-0.5;
B_ik[i]=rand()%((unsigned int)(1/calulate_error))*calulate_error-0.5;
}
//输出
for(int i=0;i {
W_kj[i]=rand()%((unsigned int)(1/calulate_error))*calulate_error-0.5;
}
B_kj=rand()%((unsigned int)(1/calulate_error))*calulate_error-0.5;
//学习步长
StudyStep_j=0.4;
StudyStep_k=0.4;
//
isfound=false;
//
get_jilei_E.open(“./matlab/jilei.txt“);
}
//
void BP::process(int counter)
{
double jilei_E=0;
for(int i=0;i {
//随机选取样本并赋值
int index=rand()%total_sample;
double source_X=-pi+2*(i+1)*pi/total_sample;
double source_goal=sin(source_X)+cos(source_X);
//归一化处理
X=(pi+source_X)/(2*pi);
double goal=(sqrt(2)+source_goal)/(2*sqrt(2));
//隐层计算
for(int j=0;j {
V_ik[j]=W_ik[j]*X-B_ik[j];//隐层第j个节点的输入
F_ik[j]=1/(1+exp(-V_ik[j]));//隐层第j个节点的输出
}
//输出层计算 就一个输出节点
V_kj=-B_kj;
for(int j=0;j {
V_kj+=W_kj[j]*F_ik[j];//输出层节点的输入
}
F_kj=1/(1+exp(-V_kj));//输出层节点的输出
//////////////////////////误差计算
jilei_E+=0.5*pow(goal-F_kj2);
//输出层校正误差
D_kj=(goal-F_kj)*F_kj*(1-F_kj);//输出神经元的校正误差
//隐层校正误差
for(int j=0;j {
D_ik[j]=D_kj*W_kj[j]*F_ik[j]*(1-F_ik[j]);
}
//对应权值阈值的修改
for(int j=0;j {
//输出层
W_kj[j]=W_kj[j]+StudyStep_j*D_kj*F_ik[j];//权值
//隐层
W_ik[j]=W_ik[j]+StudyStep_k*D_ik[j]*X;//权值
B_ik[j]=B_ik[j]-StudyStep_k*D_ik[j];//阈值
}
B_kj=B_kj-StudyStep_j*D_kj;//输出层阈值
}
jilei_E=jilei_E/total_sample;
get_jilei_E< if(sqrt(jilei_E) {
isfound=true;
}
}
//
void BP::validation()
{
//检验样本
for(int i=0;i {
valid[i]=-3*pi/2+2*i*pi/(2*total_sample);
goalout[i]=sin(valid[i])+cos(valid[i]);
}
//带入验证
ofstream nihe;
nihe.open(“nihe.txt“);
for(int i=0;i {
//样本赋值
//X=valid[i];
double source_X;//目标输入
double source_goal;//目标输出结果
double source_output;//最终输出结果
source_X=-pi+2*pi*(i+1)/(total_sample);
source_goal=cos(source_X)+sin(source_X);
//归一化处理
X=(source_X+pi)/(2*pi);
//隐层计算
for(int j=0;j {
V_ik[j]=W_ik[j]*X-B_ik[j];
F_ik[j]=1/(1+exp(-V_ik[j]));
}
//输出层计算
V_kj=-B_kj;
for(int j=0;j {
V_kj+=W_kj[j]*F_ik[j];
}
F_kj=1/(1+exp(-V_kj));
source_output=2*sqrt(2)*F_kj-sqrt(2);
cout< cout< cout< nihe< nihe< }
nihe.close();
}
//
void BP::get_param()
{
//
cout<<“隐层节点数:“< for(int i=0;i {
cout<<“从输入节点到“<<“第“< cout< cout<<“

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-01-13 21:01  FunctionApproximation\
     目录           0  2014-01-10 09:31  FunctionApproximation\Debug\
     文件      102912  2014-01-13 20:52  FunctionApproximation\Debug\FunctionApproximation.exe
     文件     1146732  2014-01-13 20:52  FunctionApproximation\Debug\FunctionApproximation.ilk
     文件     1035264  2014-01-13 20:52  FunctionApproximation\Debug\FunctionApproximation.pdb
     目录           0  2014-01-09 20:54  FunctionApproximation\FunctionApproximation\
     文件        4435  2014-01-10 10:19  FunctionApproximation\FunctionApproximation\BP.cpp
     文件        1320  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\BP.h
     目录           0  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\
     文件      266506  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\BP.obj
     文件        2794  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\cl.command.1.tlog
     文件       13628  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\CL.read.1.tlog
     文件        1606  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\CL.write.1.tlog
     文件          86  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\FunctionApproximation.lastbuildstate
     文件        1865  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\FunctionApproximation.log
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link-cvtres.read.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link-cvtres.write.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link-rc.read.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link-rc.write.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2244-cvtres.read.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2244-cvtres.write.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2244-rc.read.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2244-rc.write.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2244.read.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2244.write.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2364-cvtres.read.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2364-cvtres.write.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2364-rc.read.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2364-rc.write.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2364.read.1.tlog
     文件           2  2014-01-13 20:52  FunctionApproximation\FunctionApproximation\Debug\link.2364.write.1.tlog
............此处省略75个文件信息

评论

共有 条评论