导航:首页 > 网络营销 > 神经网络代码在哪里下载

神经网络代码在哪里下载

发布时间:2022-09-22 16:56:41

A. 谁有关于BP神经网络的编程代码或者原理介绍的文档

C++实现的BP神经网络代码
#pragma hdrstop #include<stdio.h>#include<iostream.h>const A=30.0; const B=10.0; const MAX=500; //最大训练次数 const COEF=0.0035; //网络的学习效率 const BCOEF=0.001;//网络的阀值调整效率 const ERROR=0.002 ; // 网络训练中的允许误差 const ACCURACY=0.0005;//网络要求精度 double sample[41][4]={{0,0,0,0},{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.680},{5,1,2,21.060}, {5,3,3,14.150},{5,5,4,12.680},{5,5,2,14.360}, {5,1,3,19.610},{5,3,4,13.650},{5,5,5,12.430}, {5,1,4,19.020},{5,1,4,19.020},{5,3,5,13.390}, {5,5,4,12.680},{5,1,3,19.610},{5,3,2,15.390}, {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},{1,1,5,9.545},{1,3,4,6.043}, {1,5,3,5.724},{1,1,2,11.250},{1,3,1,11.110}, {1,3,3,6.380},{1,5,2,6.521},{1,1,1,16.000}, {1,3,2,7.219},{1,5,3,5.724}}; double w[4][10][10],wc[4][10][10],b[4][10],bc[4][10]; double o[4][10],netin[4][10],d[4][10],differ;//单个样本的误差 double is; //全体样本均方差 int count,a; void netout(int m, int n);//计算网络隐含层和输出层的输出 void calculd(int m,int n); //计算网络的反向传播误差 void calcalwc(int m,int n);//计算网络权值的调整量 void calcaulbc(int m,int n); //计算网络阀值的调整量 void changew(int m,int n); //调整网络权值 void changeb(int m,int n);//调整网络阀值 void clearwc(int m,int n);//清除网络权值变化量 wc void clearbc(int m,int n);//清除网络阀值变化量 bc void initialw(void);//初始化 NN 网络权值 W void initialb(void); //初始化 NN 网络阀值 void calculdiffer(void);//计算 NN 网络单个样本误差 void calculis(void);//计算 NN 网络全体样本误差 void trainNN(void);//训练 NN 网络 /*计算 NN 网络隐含层和输出层的输出 */ void netout(int m,int n) {
int i,j,k; //隐含层各节点的的输出 for (j=1,i=2;j<=m;j++) //m 为隐含层节点个数 { netin[i][j]=0.0; for(k=1;k<=3;k++)//隐含层的每个节点均有三个输入变量 netin[i][j]=netin[i][j]+o[i-1][k]*w[i][k][j]; netin[i][j]=netin[i][j]-b[i][j]; o[i][j]=A/(1+exp(-netin[i][j]/B)); } //输出层各节点的输出 for (j=1,i=3;j<=n;j++) { netin[i][j]=0.0; for (k=1;k<=m;k++) netin[i][j]=netin[i][j]+o[i-1][k]*w[i][k][j]; netin[i][j]=netin[i][j]-b[i][j]; o[i][j]=A/(1+exp(-netin[i][j]/B)) ; } } /*计算 NN 网络的反向传播误差*/ void calculd(int m,int n) { int i,j,k; double t; a=count-1; d[3][1]=(o[3][1]-sample[a][3])*(A/B)*exp(-netin[3][1]/B)/pow(1+exp(-netin[3][1]/B),2); //隐含层的误差 for (j=1,i=2;j<=m;j++) { t=0.00; for (k=1;k<=n;k++) t=t+w[i+1][j][k]*d[i+1][k]; d[i][j]=t*(A/B)*exp(-netin[i][j]/B)/pow(1+exp(-netin[i][j]/B),2); } } /*计算网络权值 W 的调整量*/ void calculwc(int m,int n) { int i,j,k; // 输出层(第三层)与隐含层(第二层)之间的连接权值的调整 for (i=1,k=3;i<=m;i++) {
for (j=1;j<=n;j++) { wc[k][i][j]=-COEF*d[k][j]*o[k-1][i]+0.5*wc[k][i][j]; } // printf("\n"); } //隐含层与输入层之间的连接权值的调整 for (i=1,k=2;i<=m;i++) { for (j=1;j<=m;j++) { wc[k][i][j]=-COEF*d[k][j]*o[k-1][i]+0.5*wc[k][i][j]; } // printf("\n"); } } /*计算网络阀值的调整量*/ void calculbc(int m,int n) { int j; for (j=1;j<=m;j++) { bc[2][j]=BCOEF*d[2][j]; } for (j=1;j<=n;j++) { bc[3][j]=BCOEF*d[3][j]; } } /*调整网络权值*/ void changw(int m,int n) { int i,j; for (i=1;i<=3;i++) for (j=1;j<=m;j++) { w[2][i][j]=0.9*w[2][i][j]+wc[2][i][j]; //为了保证系统有较好的鲁棒性,计算权值时乘惯性系数 0.9 printf("w[2][%d][%d]=%f\n",i,j,w[2][i][j]); } for (i=1;i<=m;i++) for (j=1;j<=n;j++) { w[3][i][j]=0.9*w[3][i][j]+wc[3][i][j];
printf("w[3][%d][%d]=%f\n",i,j,w[3][i][j]); } } /*调整网络阀值*/ void changb(int m,int n) { int j; for (j=1;j<=m;j++) b[2][j]=b[2][j]+bc[2][j]; for (j=1;j<=n;j++) b[3][j]=b[3][j]+bc[3][j]; } /*清除网络权值变化量 wc*/ void clearwc(void) { for (int i=0;i<4;i++) for (int j=0;j<10;j++) for (int k=0;k<10;k++) wc[i][j][k]=0.00; } /*清除网络阀值变化量*/ void clearbc(void) { for (int i=0;i<4;i++) for (int j=0;j<10;j++) bc[i][j]=0.00; } /*初始化网络权值 W*/ void initialw(void) { int i,j,k,x; double weight; for (i=0;i<4;i++) for (j=0;j<10;j++) for (k=0;k<10;k++) { randomize(); x=100+random(400); weight=(double)x/5000.00; w[i][j][k]=weight; } } /*初始化网络阀值*/ void initialb(void)
{ int i,j,x; double fa; for (i=0;i<4;i++) for (j=0;j<10;j++) { randomize(); for (int k=0;k<12;k++) { x=100+random(400); } fa=(double)x/50000.00; b[i][j]=fa; } } /*计算网络单个样本误差*/ void calculdiffer(void) { a=count-1; differ=0.5*(o[3][1]-sample[a][3])*(o[3][1]-sample[a][3]); } void calculis(void) { int i; is=0.0; for (i=0;i<=19;i++) { o[1][1]=sample[i][0]; o[1][2]=sample[i][1]; o[1][3]=sample[i][2]; netout(8,1); is=is+(o[3][1]-sample[i][3])*(o[3][1]-sample[i][3]); } is=is/20; } /*训练网络*/ void trainNN(void) { long int time; int i,x[4]; initialw(); initialb(); for (time=1;time<=MAX;time++)
{ count=0; while(count<=40) { o[1][1]=sample[count][0]; o[1][2]=sample[count][1]; o[1][3]=sample[count][2]; count=count+1; clearwc(); clearbc(); netout(8,1); calculdiffer(); while(differ>ERROR) { calculd(8,1); calculwc(8,1); calculbc(8,1); changw(8,1); changb(8,1); netout(8,1); calculdiffer(); } } printf("This is %d times training NN...\n",time); calculis(); printf("is==%f\n",is); if (is<ACCURACY) break; } } //--------------------------------------------------------------------------#pragma argsused int main(int argc, char* argv[]) { double result; int m,test[4]; char ch='y'; cout<<"Please wait for the train of NN:"<<endl; trainNN(); cout<<"Now,this molar network can work for you."<<endl; while(ch=='y' || ch=='Y') { cout<<"Please input data to be tested."<<endl; for (m=1;m<=3;m++) cin>>test[m];
ch=getchar(); o[1][1]=test[1]; o[1][2]=test[2]; o[1][3]=test[3]; netout(8,1); result=o[3][1]; printf("Final result is %f.\n",result); printf("Still test?[Yes] or [No]\n"); ch=getchar(); } return 0; }

B. 想在github 上下载caffe的卷积神经网络 ,请问大神该怎么实现啊

github 上的代码是保存在代码库里,每个人可以创建自己的代码库。

你需要在其官网上搜对应的代码库,然后安装 git 客户端,通过命令来下载(clone)代码到本地即可。


由于不了解你的 “”,我尝试搜了一下“caffe”,随意选择一个代码库:

https://github.com/BVLC/caffe.git

下载命令:

cmd 下输入:

gitclonehttps://github.com/BVLC/caffe.git

C. 你好,想跟你要一下神经网络的代码,看到你之前解答过问题,谢谢了

// BP.cpp : Defines the entry point for the console application.

//该程序实现神经网络的BP算法,输入节点数,输出节点数,隐层数,隐层节点数任意,由用户决定。
//其中隐层数指的是总共层数包含输出层,比如说异或算法为2层,第一层节点数为2,第二层也即输出层节点数为1,输入点数为2 。
//但是该程序对异或算法实现并不理想,对多层多节点的神经网络有较好的结果

#include "stdafx.h"
#include "iostream.h"
#include <time.h>
#include <stdlib.h>
#include <fstream>
#include <math.h>
#include "stdio.h "

#define MAXCOUNT 1e5 //迭代训练次数上限 1的10的正5次幂

//精度0.001的随机浮点数,范围在-0.5——0.5
//rand()取0到32767,最大为2147483647. %模运算表示余数为0到1000之间,所以乘上浮点数0。001f就是0-1之间的数,再减去0.5,就是-0.5到+0.5
float randf()
{
return (float)((rand() % 1001) * 0.001f-0.5);
}

//高斯随机数产生函数
//这样生成的高斯分布随机数序列的期望为0.0,方差为1.0。若指定期望为E,方差为V,则只需增加:X = X * V + E;
double gaussrand()
{
static double V1, V2, S;
static int phase = 0;
double X;
if(phase == 0) {
do {
double U1 = (double)rand() / RAND_MAX;
double U2 = (double)rand() / RAND_MAX;
V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
} while(S >= 1 || S == 0);
X = V1 * sqrt(-2 * log(S) / S);
} else
X = V2 * sqrt(-2 * log(S) / S );
phase = 1 - phase;
return X;
}

//定义一个多层前向BP网络
class BP
{
public:
double ***p;//记录所有的权值
double ***ddp;//记录所有的权值增量
int *pnode;//记录每一层的节点数
double **pnodey;//记录每组每一层的节点的输出值
double **ddlj;//记录每组每一层的节点的ddlj
double **pX; //记录输入样本
double **pY; //记录输入理想输出值
int Sidenum; //隐层数目
int Inputnodenum;
int outputnodenum;
int yangbenzushu;

BP()
{
Sidenum=0;
Inputnodenum=0;
outputnodenum=0;
yangbenzushu=0;
}

~BP()
{
for(int m=0;m<Sidenum;m++)
{
for(int n=0;n<pnode[m+1];n++)
{
delete[] p[m][n];
delete[] ddp[m][n];
}
delete[] p[m];
delete[] ddp[m];
}

delete[] p;
delete[] ddp;
p=NULL;
ddp=NULL;
if(p==NULL)
delete [] pnode;

for(int M=0;M<Sidenum;M++)
{
delete[] pnodey[M];
delete[] ddlj[M];
}
delete[] pnodey;
delete[] ddlj;
pnodey=NULL;
ddlj=NULL;
}
//完成所有权值的初始化
void getW(int sidenum,int inputnodenum,int outputnodenum1,int yangbenzu)
{
Sidenum=sidenum;
yangbenzushu= yangbenzu;//样本组数目
Inputnodenum=inputnodenum;
outputnodenum=outputnodenum1;
p=new double **[sidenum];//记录所有权值
ddp=new double **[sidenum];//权值增量
pnode=new int [sidenum+1];//包含输入层,输出层每一层的节点数.
for(int i=0;i<sidenum+1;i++)
{
int data=0;
cout<<"请输入第"<<i<<"层节点数"<<endl;
cin>>data;
pnode[i]=data;
}
for (int j=0;j<sidenum;j++) // 初始化权值, 不包含输入层,但是包含输出层.第0层表示第一个隐层
{
p[j]=new double* [pnode[j+1]]; //首先P[j]层有多少个节点,就有多少个指针,每个指针指向一个权值数组.因为p[j]是二级指针,存放的是某指针的地址,某指针可以指向一维数组.
ddp[j]=new double*[pnode[j+1]];//同上
for (int k=0;k<pnode[j+1];k++)
{
ddp[j][k]=new double[pnode[j]+1];//表示第j层的第k个节点指向的是一个数组,这个数组里存的是这个节点的权值.
p[j][k]=new double[pnode[j]+1];
for (int t=0;t<pnode[j]+1;t++) //pnode[j]+1 表示第j层的输入点个数.
{
ddp[j][k][t]=0;//每一层的权值初始化为0 表示的是第j层的第k个节点,第t个输入的输入权值.
if(t==0)p[j][k][t]=-fabs(randf());//每一层的阀值初始化 第0个元素.
else p[j][k][t]=randf();//每一层的权值初始化
}
}
}
//为记录每一层的节点的输出值和ddlj的指针开辟内存
pnodey=new double *[Sidenum]; //一共有Sidenum层.
ddlj=new double *[Sidenum];
for(int p=0;p<Sidenum;p++)
{
pnodey[p] = new double [pnode[p+1]+1];//每层一共有节点数+1个输出
ddlj[p]=new double [pnode[p+1]];// 这个是做什么的??
pnodey[p][0]=1;//每组每层的首值为1,这个值是为了与阈值相乘,这也是为什么会有上面+1个输出

}
}
/**********************/
//每个节点输出函数
double fas(double s)
{
double t;
t=1.0/(exp(-s)+1);
return t;
}
/************************************************/
//该函数用来记录样本值和理想输出值
void INPUT(int yangbenzushu1 )
{
pY=new double*[yangbenzushu1];//yangbenzushu1数量个理想输出
pX=new double*[yangbenzushu1];//yangbenzushu1数量个样本
for(int yu=0;yu<yangbenzushu1;yu++)
{
pX[yu]=new double[Inputnodenum+1];//每个样本的维数是输入点个数+1
pY[yu]=new double[outputnodenum+1];//输出的维数也是输出点个数+1
}
//每组样本的首值赋为1, 这样就可以使下标对应的比较好
for(int yu1=0;yu1<yangbenzushu1;yu1++)
{
pX[yu1][0]=1;
pY[yu1][0]=1;
}
cout<<"请输入样本输入值"<<endl;
for(int yuy=0;yuy<yangbenzushu1;yuy++)
for(int yy=1;yy<=Inputnodenum;yy++)
{
if(yy==Inputnodenum) cout<<endl;
cout<<"X["<<yuy<<"]"<<"["<<yy<<"]="<<' ';
cin>>pX[yuy][yy];
}

cout<<"请输入样本理想输出值"<<endl;
for(int yuy1=0;yuy1<yangbenzushu1;yuy1++)
for(int yy1=1;yy1<=outputnodenum;yy1++)
{ //if(yy==Inputnodenum) cout<<endl;
cout<<"Y["<<yuy1<<"]"<<"["<<yy1<<"]="<<' ';
cin>>pY[yuy1][yy1];
}

}
/****************************************************************************/
//计算每个节点的输出值
double computeYl(int KK)//KK代表第几组组号
{
double sum1=0;
//把所有的层的每一个节点的输出值算出来并记录在pnodey里,不包含输入点值
for(int y=0;y<Sidenum;y++)//层数
{
for(int r=1;r<pnode[y+1]+1;r++)//本节点数,加1是为了下标好看
{
double sum=0;
for(int z=0;z<pnode[y]+1;z++)//前一层的节点数
{
if(y==0)sum+= pX[KK][z]*p[y][r-1][z];
else
sum+=pnodey[y-1][z]*p[y][r-1][z];
}
pnodey[y][r]=fas(sum);
}
}
for(int j=1;j<=outputnodenum;j++)
sum1+=pow(pY[KK][j]-pnodey[Sidenum-1][j],2);
return sum1;
}
/**********************************************************/
//Compute Back-Propagation-Errors
void ComputeBackPropagationErrors(int gf)//gf代表组号
{//计算所有的ddlj[][]
//for(int gf=0;gf<yangbenzushu;gf++)//组数
for(int q=Sidenum-1;q>=0;q--)//从最后一层开始
{
if(q==Sidenum-1)//如果是最外一层的话
{
for(int rt=0;rt<pnode[q+1];rt++)//每层的节点数
ddlj[q][rt]=pnodey[q][rt+1]*(1-pnodey[q][rt+1])*(pY[gf][rt+1]-pnodey[q][rt+1]) ;
}
else
{
for(int ry=0;ry<pnode[q+1];ry++)
{
double sumtemp=0;
for(int fg=0;fg<pnode[q+2];fg++)
sumtemp+=ddlj[q+1][fg]*p[q+1][fg][ry+1];
ddlj[q][ry] = pnodey[q][ry+1]*(1-pnodey[q][ry+1])* sumtemp;
}

}

}
//计算所有的ddp[][]
//for(int gf1=0;gf1<yangbenzushu;gf1++)//组数
for(int l=0;l<Sidenum;l++)//层数
for(int JJ=0;JJ<pnode[l+1];JJ++)//每一层的节点数
for(int i=0;i<pnode[l]+1;i++)//前一层的节点数
{
if(l==0)//如果是第一层的话,y值为输入的X值
ddp[l][JJ][i]=ddlj[l][JJ]*pX[gf][i];
else
ddp[l][JJ][i]=ddlj[l][JJ]*pnodey[l-1][i];
}

}
/*************************************************************************/
void ()
{
for(int cent=0;cent<Sidenum;cent++)//层数
for(int J=0;J<pnode[cent+1];J++)//每一层的节点数
for(int i=0;i<pnode[cent]+1;i++)//前一层的节点数
p[cent][J][i]+=0.2*ddp[cent][J][i];
}
/***************************************************************************/
double xunlianErrors()//定义训练误差函数
{
double error=0;
double sum=0;
double temp=0;
double temp1=0;
for(int gf1=0;gf1<yangbenzushu;gf1++)//组数
{
temp= computeYl(gf1);
//temp1=zhengquelv(gf1);
//sum+=temp1;
for(int jj=1;jj<=outputnodenum;jj++)
cout<<pnodey[Sidenum-1][jj];
error+=temp;
}
// sum=sum/yangbenzushu;
cout<<"用训练集所得到的正确率:"<<sum<<endl;
return error/yangbenzushu;
}
/****************************************************************************/
double jiaoyanErrors(int yangbenzushu1 )//定义校验误差函数
{
double error=0;
double sum=0;
double temp=0;
double temp1=0;
for(int gf1=0;gf1<yangbenzushu1;gf1++)//组数
{
temp= computeYl(gf1);
for(int jj=1;jj<=outputnodenum;jj++)
cout<<pnodey[Sidenum-1][jj];
//temp1=zhengquelv(gf1);
//sum+=temp1;
error+=temp;
}
//sum=sum/yangbenzushu1;
//cout<<"用校验集所得到的正确率:"<<sum<<endl;
return error/yangbenzushu1;
}
/********************************************************************/
double zhengquelv(int KK)
{
int count=0;
double av=0;
//for(int gf1=0;gf1<yangbenzushu;gf1++)//组数
for(int jj=1;jj<=outputnodenum;jj++)
{
if (pnodey[Sidenum-1][jj]>0) pnodey[Sidenum-1][jj]=1;
else pnodey[Sidenum-1][jj]=0;
if(pY[KK][jj]==pnodey[Sidenum-1][jj])count++;
}

av=(double)count/outputnodenum;
return av;
}
/***********************************************************************/
void freeINput()
{
if(pX!=NULL)
{
for(int u=0;u<yangbenzushu;u++)
delete []pX[u];
delete []pX;
pX=NULL;
}

if(pY!=NULL)
{
for(int u1=0;u1<yangbenzushu;u1++)
delete []pY[u1];
delete []pY;
pY=NULL;
}

}
/***************************************************************/
//输出所有的权值
void wputout()
{
for (int j=0;j<Sidenum;j++)
{
cout<<"第["<<j+1<<"]层权值为:"<<endl;
for (int k=0;k<pnode[j+1];k++)
{
//if(k==pnode[j+1]-1) cout<<endl;
for (int t=0;t<pnode[j]+1;t++)
{
cout<<p[j][k][t]<<' ';
if(t==pnode[j]) cout<<endl;
}
}
}
}
/**********************************************************/
};

void main()
{
BP bp;
int count=0;//用来统计所用的迭代次数
//FILE *fp;
int inputnodenum,outnodenum,sidenum,yangbenzunum;
double error;
cout<<"请输入:输入点数,输出点数,隐层数"<<endl;
cin>>inputnodenum>>outnodenum>>sidenum;
cout<<"请输入样本组数"<<endl;
cin>>yangbenzunum;
//第一步初始化所有的权值
bp.getW(sidenum,inputnodenum,outnodenum,yangbenzunum);
//第二步输入样本组
bp.INPUT(yangbenzunum);
for(;;count++)
{
double sum=0;
double temp=0;
for(int fuzu=0;fuzu<yangbenzunum;fuzu++)
{
//第三步计算所有y值
temp=bp.computeYl(fuzu);
//第四步Compute Back-Propagation-Errors
bp.ComputeBackPropagationErrors(fuzu);
//第五步Update the Weights using BP Algorithm
bp.();
sum+=temp;

}
//第六步判断是否收敛
error=sum/2*yangbenzunum;
//freopen("debug\\out.txt","w",stdout);
//fp=freopen( "out.txt", "w", stdout) ;

// cout<<count<<' '<<error<<endl;
// fclose(stdout);//关闭文件
/*if(count==1000)cout<<error<<endl;
if(count==1500)cout<<error<<endl;
if(count==1600)cout<<error<<endl;*/
//if(count==10000)cout<<error<<endl;
if(error<1.02)
{
cout<<"循环收敛"<<"迭代次数为:"<<count<<endl;
//bp.freeINput();//释放X Y空间
break;
}
}
cout<<"权值为:"<<endl;
bp.wputout();
double XUNLIANER=bp.xunlianErrors();
//cout<<"训练误差为:"<<XUNLIANER<<endl;
bp.freeINput();//释放X Y空间
/*
cout<<"请输入校验样本: "<<endl;
int jiaoyannum=0;
cin>>jiaoyannum;
bp.INPUT(jiaoyannum);
double jiaoyanER=bp.jiaoyanErrors(jiaoyannum);
cout<<"校验误差为:"<<jiaoyanER<<endl;
//fclose( stdout ) ;*/

}

D. 你好,我想问一下你有没有神经网络算法的matlab代码

我当时数学建模比赛时用到过
有两种方法

第一种:自己编代码
当然这种比较麻烦,但是比较灵活,你可以按照自己的要求在代码中修改。具体代码我这已经没了,因为过了好久了,你可以网络一下 MATLAB 神经网络算法代码。

第二种:直接使用MATLAB
MATLAB带有神经网络工具箱,在控制台输入命令即可打开。这个工具箱是MATLAB集成的,功能强大而且简单易用,不知道符合你的要求不。
给你个使用说明网址链接:http://wenku..com/view/23d6ab45b307e87101f696f2.html

E. 跪求som神经网络的MATLAB源代码,谢谢各位大哥大姐。如果运行结果代码正确,高分相送。

SOM:
close all
clf reset
figure(gcf);
echo on
pause
clc
p=zscore(data);%biaozhunhua
pause
clc
plot3(p(:,1),p(:,2),p(:,3),'*');
axis([0 1 0 1]);
title('Input data');
pause
clc
net=newsom([0 1;0 1],[9]);
pause
clc
net.trainParam.epochs=100;
net=train(net,p);
pause
clc
figure;
w=net.IW{1};
%IW 是输入层到第一层的权值矩阵
%LW 是中间层和输出层,也就是神经元到神经元的权值
%b 是第Ni层的偏向向量
plotsom(net.IW{1,1},net.layers{1}.distances);
pause
clc
a=sim(net,[0.6;0.8])
echo off

F. 谁有最简单的BP神经网络的可以的代码啊

#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <ctime>
//Rumelhart,1985,only one hidden layer
const double lr = 0.1;
const double goal = 1e-1;
const size_t SampleNum = 4;
const size_t HidNeuron = 10;
const size_t Input = 2;
const size_t Output = 1;

double Weight1[Input][HidNeuron] = {0};
double Bias1[HidNeuron] = {0};
double Weight2[HidNeuron][Output] = {0};
double Bias2[Output] = {0};

double InputValue[SampleNum][Input] ={0};
double OutputValue[SampleNum][Output] ={0};
double Error[SampleNum][Output] = {0};
double MSE = 1;

void InitWeight()
{
size_t i,j;
for( i=0; i<Input; i++)
{
for(j=0; j<HidNeuron; j++)
{
Weight1[i][j] = (rand()/(double)RAND_MAX)*2 - 1;
}
}
for( i=0; i<HidNeuron; i++)
Bias1[i] = (rand()/(double)RAND_MAX)*2 - 1;
for( i=0; i<HidNeuron; i++)
{
for(j=0; j<Output; j++)
{
Weight2[i][j] = (rand()/(double)RAND_MAX)*2 - 1;
}
}
for( i=0; i<Output; i++)
Bias2[i] = (rand()/(double)RAND_MAX)*2 - 1;
}

double LogSigmoid(double x)
{
return 1/(1+exp(-x));
}

double DetLogSigmoid(double x)
{
return LogSigmoid(x) - pow(LogSigmoid(x),2.0);
}

void Computing()
{
size_t i,j,k;
double sum=0;
double hidOutput[SampleNum][HidNeuron] = {0};
double finOutput[SampleNum][Output] = {0};

double detOutput[Output] = {0};
double detHid[HidNeuron] = {0};
double simHidError[HidNeuron] ={0};
for(i=0; i<SampleNum; i++)
{
//ForwardComputing
for(j=0; j<HidNeuron; j++)
{
sum =0;
for(k=0; k<Input+1; k++)
{
if(k == Input)
sum += (-1)*Bias1[j];
else
sum += InputValue[i][k]*Weight1[k][j];
}
hidOutput[i][j] = LogSigmoid(sum);
}
for(j=0; j<Output; j++)
{
sum =0;
for(k=0; k<HidNeuron+1; k++)
{
if(k == HidNeuron)
sum += (-1)*Bias2[j];
else
sum += hidOutput[i][k]*Weight2[k][j];
}
finOutput[i][j] = LogSigmoid(sum);
Error[i][j] = OutputValue[i][j] - finOutput[i][j];
}
//BackwardComputing
for(j=0; j<Output; j++)
{
detOutput[j] = finOutput[i][j]*(1-finOutput[i][j])*Error[i][j];
for(k=0; k<HidNeuron; k++)
{
Weight2[k][j] += (lr*detOutput[j]*hidOutput[i][k]);
}
Bias2[j] += (lr*detOutput[j]*(-1));
}

for(j=0; j<HidNeuron; j++)
{
simHidError[j] = 0;
for(k=0; k<Output; k++)
{
simHidError[j] += detOutput[k]*Weight2[j][k];
}
}

for(j=0; j<HidNeuron; j++)
{
detHid[j] = hidOutput[i][j]*(1-hidOutput[i][j])*simHidError[j];
for(k=0; k<Input; k++)
{
Weight1[k][j] += (lr*detHid[j]*InputValue[i][k]);
}
Bias1[j] += (lr*detHid[j]*(-1));
}
}
MSE=0;
for(i =0; i<Output; i++)
{
for(j=0; j<SampleNum; j++)
{
MSE += (Error[j][i]*Error[j][i]);
}
}
MSE = sqrt(MSE/SampleNum);
}

int main()
{
srand(unsigned(time(NULL)));
static clock_t BeforeRunTime = clock();
InitWeight();
InputValue[0][0] = 0;
InputValue[0][1] = 0;
OutputValue[0][0] = 0;

InputValue[1][0] = 1;
InputValue[1][1] = 1;
OutputValue[1][0] = 0;

InputValue[2][0] = 0;
InputValue[2][1] = 1;
OutputValue[2][0] = 1;

InputValue[3][0] = 1;
InputValue[3][1] = 0;
OutputValue[3][0] = 1;

size_t cEpoch = 0;
while(MSE>goal)
{
cEpoch++;
Computing();
printf("MSE: %.6f\n",MSE);
}
printf("Epochs %d\n",cEpoch);

clock_t UsedTime = clock()-BeforeRunTime;
printf("UsedTime %dms\n",UsedTime);

getchar();
return 0;
}

G. 请问哪位高手有BP神经网络的源代码

发了,收
我下面那位,你要骗分请排在我后面,是我先来骗的

H. 求BP神经网络算法的C++源代码

// AnnBP.cpp: implementation of the CAnnBP class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "AnnBP.h"
#include "math.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAnnBP::CAnnBP()
{
eta1=0.3;
momentum1=0.3;

}

CAnnBP::~CAnnBP()
{

}

double CAnnBP::drnd()
{
return ((double) rand() / (double) BIGRND);
}

/*** 返回-1.0到1.0之间的双精度随机数 ***/
double CAnnBP::dpn1()
{
return (double) (rand())/(32767/2)-1;
}

/*** 作用函数,目前是S型函数 ***/
double CAnnBP::squash(double x)
{
return (1.0 / (1.0 + exp(-x)));
}

/*** 申请1维双精度实数数组 ***/
double* CAnnBP::alloc_1d_dbl(int n)
{
double *new1;

new1 = (double *) malloc ((unsigned) (n * sizeof (double)));
if (new1 == NULL) {
AfxMessageBox("ALLOC_1D_DBL: Couldn't allocate array of doubles\n");
return (NULL);
}
return (new1);
}

/*** 申请2维双精度实数数组 ***/
double** CAnnBP::alloc_2d_dbl(int m, int n)
{
int i;
double **new1;

new1 = (double **) malloc ((unsigned) (m * sizeof (double *)));
if (new1 == NULL) {
AfxMessageBox("ALLOC_2D_DBL: Couldn't allocate array of dbl ptrs\n");
return (NULL);
}

for (i = 0; i < m; i++) {
new1[i] = alloc_1d_dbl(n);
}

return (new1);
}

/*** 随机初始化权值 ***/
void CAnnBP::bpnn_randomize_weights(double **w, int m, int n)
{
int i, j;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
w[i][j] = dpn1();
}
}

}

/*** 0初始化权值 ***/
void CAnnBP::bpnn_zero_weights(double **w, int m, int n)
{
int i, j;

for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
w[i][j] = 0.0;
}
}

}

/*** 设置随机数种子 ***/
void CAnnBP::bpnn_initialize(int seed)
{
CString msg,s;
msg="Random number generator seed:";
s.Format("%d",seed);
AfxMessageBox(msg+s);
srand(seed);
}

/*** 创建BP网络 ***/
BPNN* CAnnBP::bpnn_internal_create(int n_in, int n_hidden, int n_out)
{
BPNN *newnet;

newnet = (BPNN *) malloc (sizeof (BPNN));
if (newnet == NULL) {
printf("BPNN_CREATE: Couldn't allocate neural network\n");
return (NULL);
}

newnet->input_n = n_in;
newnet->hidden_n = n_hidden;
newnet->output_n = n_out;
newnet->input_units = alloc_1d_dbl(n_in + 1);
newnet->hidden_units = alloc_1d_dbl(n_hidden + 1);
newnet->output_units = alloc_1d_dbl(n_out + 1);

newnet->hidden_delta = alloc_1d_dbl(n_hidden + 1);
newnet->output_delta = alloc_1d_dbl(n_out + 1);
newnet->target = alloc_1d_dbl(n_out + 1);

newnet->input_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);
newnet->hidden_weights = alloc_2d_dbl(n_hidden + 1, n_out + 1);

newnet->input_prev_weights = alloc_2d_dbl(n_in + 1, n_hidden + 1);
newnet->hidden_prev_weights = alloc_2d_dbl(n_hidden + 1, n_out + 1);

return (newnet);

}

/* 释放BP网络所占地内存空间 */
void CAnnBP::bpnn_free(BPNN *net)
{
int n1, n2, i;

n1 = net->input_n;
n2 = net->hidden_n;

free((char *) net->input_units);
free((char *) net->hidden_units);
free((char *) net->output_units);

free((char *) net->hidden_delta);
free((char *) net->output_delta);
free((char *) net->target);

for (i = 0; i <= n1; i++) {
free((char *) net->input_weights[i]);
free((char *) net->input_prev_weights[i]);
}
free((char *) net->input_weights);
free((char *) net->input_prev_weights);

for (i = 0; i <= n2; i++) {
free((char *) net->hidden_weights[i]);
free((char *) net->hidden_prev_weights[i]);
}
free((char *) net->hidden_weights);
free((char *) net->hidden_prev_weights);

free((char *) net);
}

/*** 创建一个BP网络,并初始化权值***/
BPNN* CAnnBP::bpnn_create(int n_in, int n_hidden, int n_out)
{
BPNN *newnet;

newnet = bpnn_internal_create(n_in, n_hidden, n_out);

#ifdef INITZERO
bpnn_zero_weights(newnet->input_weights, n_in, n_hidden);
#else
bpnn_randomize_weights(newnet->input_weights, n_in, n_hidden);
#endif
bpnn_randomize_weights(newnet->hidden_weights, n_hidden, n_out);
bpnn_zero_weights(newnet->input_prev_weights, n_in, n_hidden);
bpnn_zero_weights(newnet->hidden_prev_weights, n_hidden, n_out);

return (newnet);

}

void CAnnBP::bpnn_layerforward(double *l1, double *l2, double **conn, int n1, int n2)
{
double sum;
int j, k;

/*** 设置阈值 ***/
l1[0] = 1.0;

/*** 对于第二层的每个神经元 ***/
for (j = 1; j <= n2; j++) {

/*** 计算输入的加权总和 ***/
sum = 0.0;
for (k = 0; k <= n1; k++) {
sum += conn[k][j] * l1[k];
}
l2[j] = squash(sum);
}
}

/* 输出误差 */
void CAnnBP::bpnn_output_error(double *delta, double *target, double *output, int nj, double *err)
{
int j;
double o, t, errsum;

errsum = 0.0;
for (j = 1; j <= nj; j++) {
o = output[j];
t = target[j];
delta[j] = o * (1.0 - o) * (t - o);
errsum += ABS(delta[j]);
}
*err = errsum;

}

/* 隐含层误差 */
void CAnnBP::bpnn_hidden_error(double *delta_h, int nh, double *delta_o, int no, double **who, double *hidden, double *err)
{
int j, k;
double h, sum, errsum;

errsum = 0.0;
for (j = 1; j <= nh; j++) {
h = hidden[j];
sum = 0.0;
for (k = 1; k <= no; k++) {
sum += delta_o[k] * who[j][k];
}
delta_h[j] = h * (1.0 - h) * sum;
errsum += ABS(delta_h[j]);
}
*err = errsum;
}

/* 调整权值 */
void CAnnBP::bpnn_adjust_weights(double *delta, int ndelta, double *ly, int nly, double **w, double **oldw, double eta, double momentum)
{
double new_dw;
int k, j;

ly[0] = 1.0;
for (j = 1; j <= ndelta; j++) {
for (k = 0; k <= nly; k++) {
new_dw = ((eta * delta[j] * ly[k]) + (momentum * oldw[k][j]));
w[k][j] += new_dw;
oldw[k][j] = new_dw;
}
}

}

/* 进行前向运算 */
void CAnnBP::bpnn_feedforward(BPNN *net)
{
int in, hid, out;

in = net->input_n;
hid = net->hidden_n;
out = net->output_n;

/*** Feed forward input activations. ***/
bpnn_layerforward(net->input_units, net->hidden_units,
net->input_weights, in, hid);
bpnn_layerforward(net->hidden_units, net->output_units,
net->hidden_weights, hid, out);

}

/* 训练BP网络 */
void CAnnBP::bpnn_train(BPNN *net, double eta, double momentum, double *eo, double *eh)
{
int in, hid, out;
double out_err, hid_err;

in = net->input_n;
hid = net->hidden_n;
out = net->output_n;

/*** 前向输入激活 ***/
bpnn_layerforward(net->input_units, net->hidden_units,
net->input_weights, in, hid);
bpnn_layerforward(net->hidden_units, net->output_units,
net->hidden_weights, hid, out);

/*** 计算隐含层和输出层误差 ***/
bpnn_output_error(net->output_delta, net->target, net->output_units,
out, &out_err);
bpnn_hidden_error(net->hidden_delta, hid, net->output_delta, out,
net->hidden_weights, net->hidden_units, &hid_err);
*eo = out_err;
*eh = hid_err;

/*** 调整输入层和隐含层权值 ***/
bpnn_adjust_weights(net->output_delta, out, net->hidden_units, hid,
net->hidden_weights, net->hidden_prev_weights, eta, momentum);
bpnn_adjust_weights(net->hidden_delta, hid, net->input_units, in,
net->input_weights, net->input_prev_weights, eta, momentum);
}

/* 保存BP网络 */
void CAnnBP::bpnn_save(BPNN *net, char *filename)
{
CFile file;
char *mem;
int n1, n2, n3, i, j, memcnt;
double dvalue, **w;
n1 = net->input_n; n2 = net->hidden_n; n3 = net->output_n;
printf("Saving %dx%dx%d network to '%s'\n", n1, n2, n3, filename);
try
{
file.Open(filename,CFile::modeWrite|CFile::modeCreate|CFile::modeNoTruncate);
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

file.Write(&n1,sizeof(int));
file.Write(&n2,sizeof(int));
file.Write(&n3,sizeof(int));

memcnt = 0;
w = net->input_weights;
mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(double)));
// mem = (char *) malloc (((n1+1) * (n2+1) * sizeof(double)));
for (i = 0; i <= n1; i++) {
for (j = 0; j <= n2; j++) {
dvalue = w[i][j];
//fast(&mem[memcnt], &dvalue, sizeof(double));
fast(&mem[memcnt], &dvalue, sizeof(double));
memcnt += sizeof(double);

}
}

file.Write(mem,sizeof(double)*(n1+1)*(n2+1));
free(mem);

memcnt = 0;
w = net->hidden_weights;
mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(double)));
// mem = (char *) malloc (((n2+1) * (n3+1) * sizeof(double)));
for (i = 0; i <= n2; i++) {
for (j = 0; j <= n3; j++) {
dvalue = w[i][j];
fast(&mem[memcnt], &dvalue, sizeof(double));
// fast(&mem[memcnt], &dvalue, sizeof(double));
memcnt += sizeof(double);
}
}

file.Write(mem, (n2+1) * (n3+1) * sizeof(double));
// free(mem);

file.Close();
return;
}

/* 从文件中读取BP网络 */
BPNN* CAnnBP::bpnn_read(char *filename)
{
char *mem;
BPNN *new1;
int n1, n2, n3, i, j, memcnt;
CFile file;

try
{
file.Open(filename,CFile::modeRead|CFile::modeCreate|CFile::modeNoTruncate);
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

// printf("Reading '%s'\n", filename);// fflush(stdout);

file.Read(&n1, sizeof(int));
file.Read(&n2, sizeof(int));
file.Read(&n3, sizeof(int));

new1 = bpnn_internal_create(n1, n2, n3);

// printf("'%s' contains a %dx%dx%d network\n", filename, n1, n2, n3);
// printf("Reading input weights..."); // fflush(stdout);

memcnt = 0;
mem = (char *) malloc (((n1+1) * (n2+1) * sizeof(double)));

file.Read(mem, ((n1+1)*(n2+1))*sizeof(double));
for (i = 0; i <= n1; i++) {
for (j = 0; j <= n2; j++) {
//fast(&(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));
fast(&(new1->input_weights[i][j]), &mem[memcnt], sizeof(double));
memcnt += sizeof(double);
}
}
free(mem);

// printf("Done\nReading hidden weights..."); //fflush(stdout);

memcnt = 0;
mem = (char *) malloc (((n2+1) * (n3+1) * sizeof(double)));

file.Read(mem, (n2+1) * (n3+1) * sizeof(double));
for (i = 0; i <= n2; i++) {

for (j = 0; j <= n3; j++) {
//fast(&(new1->hidden_weights[i][j]), &mem[memcnt], sizeof(double));
fast(&(new1->hidden_weights[i][j]), &mem[memcnt], sizeof(double));
memcnt += sizeof(double);

}
}
free(mem);
file.Close();

printf("Done\n"); //fflush(stdout);

bpnn_zero_weights(new1->input_prev_weights, n1, n2);
bpnn_zero_weights(new1->hidden_prev_weights, n2, n3);

return (new1);
}

void CAnnBP::CreateBP(int n_in, int n_hidden, int n_out)
{
net=bpnn_create(n_in,n_hidden,n_out);
}

void CAnnBP::FreeBP()
{
bpnn_free(net);

}

void CAnnBP::Train(double *input_unit,int input_num, double *target,int target_num, double *eo, double *eh)
{
for(int i=1;i<=input_num;i++)
{
net->input_units[i]=input_unit[i-1];
}

for(int j=1;j<=target_num;j++)
{
net->target[j]=target[j-1];
}
bpnn_train(net,eta1,momentum1,eo,eh);

}

void CAnnBP::Identify(double *input_unit,int input_num,double *target,int target_num)
{
for(int i=1;i<=input_num;i++)
{
net->input_units[i]=input_unit[i-1];
}
bpnn_feedforward(net);
for(int j=1;j<=target_num;j++)
{
target[j-1]=net->output_units[j];
}
}

void CAnnBP::Save(char *filename)
{
bpnn_save(net,filename);

}

void CAnnBP::Read(char *filename)
{
net=bpnn_read(filename);
}

void CAnnBP::SetBParm(double eta, double momentum)
{
eta1=eta;
momentum1=momentum;

}

void CAnnBP::Initialize(int seed)
{
bpnn_initialize(seed);

}

I. 哪里有基于Hadoop平台,训练卷积神经网络的代码

卷积神经网络是近年发展起来,并引起广泛重视的一种高效识别方法。20世纪60年代,Hubel和Wiesel在研究猫脑皮层中用于局部敏感和方向选择的神经元时发现其独特的网络结构可以有效地降低反馈神经网络的复杂性,继而提出了卷积神经网络(Convolutional
Neural
Networks-简称CNN)。现在,CNN已经成为众多科学领域的研究热点之一,特别是在模式分类领域,由于该网络避免了对图像的复杂前期预处理,可以直接输入原始图像,因而得到了更为广泛的应用。
K.Fukushima在1980年提出的新识别机是卷积神经网络的第一个实现网络。随后,更多的科研工作者对该网络进行了改进。其中,具有代表性的研究成果是Alexander和Taylor提出的“改进认知机”,该方法综合了各种改进方法的优点并避免了耗时的误差反向传播。


采用matlab写的GPU版本卷积神经网络,使用了maxpooling等技术,matlab版本为2013a.

阅读全文

与神经网络代码在哪里下载相关的资料

热点内容
不用网络盒子看电视直播软件哪个好 浏览:937
网络渠道商有哪些 浏览:34
茂名手机网络稳定器 浏览:958
小米欠费停机后移动网络问题 浏览:887
计算机网络技术专业的行业岗位 浏览:50
最近江苏移动网络 浏览:152
红米a9能用移动网络吗 浏览:870
手机网络扩大器什么样的 浏览:245
苹果11支持移动3g网络吗 浏览:595
网络兼职水军去哪里找 浏览:222
息烽移动网络服务部 浏览:797
江门广电网络营业厅电话多少 浏览:562
台式电脑网络共享在哪 浏览:555
石家庄学网络工程师哪个学校好 浏览:578
请设置你的网络 浏览:847
摄像头怎么接入无线网络 浏览:220
wifi网络怎么一下子变得很差 浏览:679
软件加网络弹窗验证 浏览:966
荣耀usb共享网络失效 浏览:885
现在什么网络游戏最火 浏览:775

友情链接