導航:首頁 > 網路營銷 > 神經網路代碼在哪裡下載

神經網路代碼在哪裡下載

發布時間: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.

閱讀全文

與神經網路代碼在哪裡下載相關的資料

熱點內容
怎麼用手機看自己網路是sa還是nsa 瀏覽:447
怎麼看貓上沒有網路連接 瀏覽:966
如何使用手機設置網路連接 瀏覽:492
手機為啥網路連接上了不可上網 瀏覽:669
為什麼網路上行大於下行 瀏覽:812
網路共享官網 瀏覽:318
集美大學連接無線網路 瀏覽:755
農村用電信網路怎麼交費 瀏覽:909
手機如何分享網路wlan熱點 瀏覽:508
路由器突然顯示5G網路 瀏覽:711
手機怎麼改移動網路路由器密碼 瀏覽:623
保定網路託管業務你知道多少 瀏覽:994
連不上自己家的網路應該怎麼辦 瀏覽:256
風電安裝船上有網路信號嗎 瀏覽:369
共享網路怎麼添加 瀏覽:461
高中學習網路平台有哪些 瀏覽:126
群暉網路存儲設置 瀏覽:856
手機有網路信號連不上無線 瀏覽:876
網路玩手機不卡玩電腦卡 瀏覽:117
鎮江網路廣告設計怎麼樣 瀏覽:745

友情鏈接