Home Database Mysql Tutorial BP神经网络算法(2)

BP神经网络算法(2)

Jun 07, 2016 pm 03:49 PM
Neural Networks algorithm

// BpNet.h:interfacefortheBpclass. // // E-Mail:zengzhijun369@163.com /**/ ///////////////////////////////////////////////////////////////////// / #include stdafx.h #include BpNet.h #include math.h #ifdef_DEBUG #undef THIS_FILE static char

 

//BpNet.h: interface for the Bp class.
BP神经网络算法(2)
//
BP神经网络算法(2)
//E-Mail:zengzhijun369@163.com
BP神经网络算法(2)BP神经网络算法(2)
/**///////////////////////////////////////////////////////////////////////
BP神经网络算法(2)#include "stdafx.h"
BP神经网络算法(2)#include 
"BpNet.h"
BP神经网络算法(2)#include 
"math.h"
BP神经网络算法(2)
BP神经网络算法(2)#ifdef _DEBUG
BP神经网络算法(2)
#undef THIS_FILE
BP神经网络算法(2)
static char THIS_FILE[]=__FILE__;
BP神经网络算法(2)
#define new DEBUG_NEW
BP神经网络算法(2)
#endif
BP神经网络算法(2)
BP神经网络算法(2)BP神经网络算法(2)
/**///////////////////////////////////////////////////////////////////////
BP神经网络算法(2)// Construction/Destruction
BP神经网络算法(2)BP神经网络算法(2)
/**///////////////////////////////////////////////////////////////////////
BP神经网络算法(2)
BP神经网络算法(2)BpNet::BpNet()
BP神经网络算法(2)BP神经网络算法(2)
BP神经网络算法(2){       
BP神经网络算法(2)    error
=1.0;
BP神经网络算法(2)    e
=0.0;
BP神经网络算法(2)    
BP神经网络算法(2)    rate_w
=0.05;  //权值学习率(输入层--隐含层)
BP神经网络算法(2)
    rate_w1=0.047//权值学习率 (隐含层--输出层)
BP神经网络算法(2)
    rate_b1=0.05//隐含层阀值学习率
BP神经网络算法(2)
    rate_b2=0.047//输出层阀值学习率
BP神经网络算法(2)
    error=1.0;
BP神经网络算法(2)    e
=0.0;
BP神经网络算法(2)    
BP神经网络算法(2)    rate_w
=0.05;  //权值学习率(输入层--隐含层)
BP神经网络算法(2)
    rate_w1=0.047//权值学习率 (隐含层--输出层)
BP神经网络算法(2)
    rate_b1=0.05//隐含层阀值学习率
BP神经网络算法(2)
    rate_b2=0.047//输出层阀值学习率
BP神经网络算法(2)
}

BP神经网络算法(2)
BP神经网络算法(2)BpNet::
~BpNet()
BP神经网络算法(2)BP神经网络算法(2)
BP神经网络算法(2){
BP神经网络算法(2)    
BP神经网络算法(2)}

BP神经网络算法(2)
BP神经网络算法(2)
void winit(double w[],int sl)//权值初始化
BP神经网络算法(2)BP神经网络算法(2)
BP神经网络算法(2){int i;
BP神经网络算法(2)
double randx();
BP神经网络算法(2)BP神经网络算法(2)
for(i=0;isl;i++)BP神经网络算法(2){
BP神经网络算法(2)    
*(w+i)=0.2*randx();
BP神经网络算法(2)}

BP神经网络算法(2)}

BP神经网络算法(2)
BP神经网络算法(2)
double randx()//kqy error
BP神经网络算法(2)BP神经网络算法(2)
BP神经网络算法(2){double d;
BP神经网络算法(2)d
=(double) rand()/32767.0;
BP神经网络算法(2)
return d;
BP神经网络算法(2)}

BP神经网络算法(2)
BP神经网络算法(2)
void BpNet::init()
BP神经网络算法(2)BP神经网络算法(2)
BP神经网络算法(2){
BP神经网络算法(2)    winit((
double*)w,innode*hidenode);
BP神经网络算法(2)    winit((
double*)w1,hidenode*outnode);
BP神经网络算法(2)    winit(b1,hidenode);
BP神经网络算法(2)    winit(b2,outnode);
BP神经网络算法(2)}

BP神经网络算法(2)
BP神经网络算法(2)
BP神经网络算法(2)
void BpNet::train(double p[trainsample][innode],double t[trainsample][outnode])
BP神经网络算法(2)BP神经网络算法(2)
BP神经网络算法(2){
BP神经网络算法(2)    
double pp[hidenode];//隐含结点的校正误差
BP神经网络算法(2)
    double qq[outnode];//希望输出值与实际输出值的偏差
BP神经网络算法(2)
    double yd[outnode];//希望输出值
BP神经网络算法(2)
    
BP神经网络算法(2)    
double x[innode]; //输入向量
BP神经网络算法(2)
    double x1[hidenode];//隐含结点状态值
BP神经网络算法(2)
    double x2[outnode];//输出结点状态值
BP神经网络算法(2)
    double o1[hidenode];//隐含层激活值
BP神经网络算法(2)
    double o2[hidenode];//输出层激活值
BP神经网络算法(2)
    for(int isamp=0;isamptrainsample;isamp++)//循环训练一次样品
BP神经网络算法(2)BP神经网络算法(2)
    BP神经网络算法(2)
BP神经网络算法(2)        
for(int i=0;iinnode;i++)
BP神经网络算法(2)            x[i]
=p[isamp][i];
BP神经网络算法(2)        
for(i=0;ioutnode;i++)
BP神经网络算法(2)            yd[i]
=t[isamp][i];
BP神经网络算法(2)        
BP神经网络算法(2)        
//构造每个样品的输入和输出标准
BP神经网络算法(2)
        for(int j=0;jhidenode;j++)
BP神经网络算法(2)BP神经网络算法(2)        
BP神经网络算法(2){
BP神经网络算法(2)            o1[j]
=0.0;
BP神经网络算法(2)            
BP神经网络算法(2)            
for(i=0;iinnode;i++)
BP神经网络算法(2)                o1[j]
=o1[j]+w[i][j]*x[i];//隐含层各单元输入激活值
BP神经网络算法(2)
            x1[j]=1.0/(1+exp(-o1[j]-b1[j]));//隐含层各单元的输出kqy1
BP神经网络算法(2)            
//    if(o1[j]+b1[j]>0) x1[j]=1;
BP神经网络算法(2)            
//else x1[j]=0;
BP神经网络算法(2)
        }

BP神经网络算法(2)        
BP神经网络算法(2)        
for(int k=0;koutnode;k++)
BP神经网络算法(2)BP神经网络算法(2)        
BP神经网络算法(2){
BP神经网络算法(2)            o2[k]
=0.0;
BP神经网络算法(2)            
BP神经网络算法(2)            
for(j=0;jhidenode;j++)
BP神经网络算法(2)                o2[k]
=o2[k]+w1[j][k]*x1[j];//输出层各单元输入激活值
BP神经网络算法(2)
            x2[k]=1.0/(1.0+exp(-o2[k]-b2[k]));//输出层各单元输出
BP神经网络算法(2)            
//    if(o2[k]+b2[k]>0) x2[k]=1;
BP神经网络算法(2)            
//    else x2[k]=0;
BP神经网络算法(2)
        }

BP神经网络算法(2)        
BP神经网络算法(2)        
for(k=0;koutnode;k++)
BP神经网络算法(2)BP神经网络算法(2)        
BP神经网络算法(2){
BP神经网络算法(2)            e
=0.0;
BP神经网络算法(2)            qq[k]
=(yd[k]-x2[k])*x2[k]*(1.-x2[k]);//希望输出与实际输出的偏差
BP神经网络算法(2)
            e+=fabs(yd[k]-x2[k])*fabs(yd[k]-x2[k]);//计算均方差
BP神经网络算法(2)
            
BP神经网络算法(2)            
for(j=0;jhidenode;j++)
BP神经网络算法(2)                w1[j][k]
=w1[j][k]+rate_w1*qq[k]*x1[j];//下一次的隐含层和输出层之间的新连接权
BP神经网络算法(2)
            e=sqrt(e);
BP神经网络算法(2)            error
=e;
BP神经网络算法(2)        
BP神经网络算法(2)        }

BP神经网络算法(2)        
BP神经网络算法(2)        
for(j=0;jhidenode;j++)
BP神经网络算法(2)BP神经网络算法(2)        
BP神经网络算法(2){
BP神经网络算法(2)            pp[j]
=0.0;
BP神经网络算法(2)            
for(k=0;koutnode;k++)
BP神经网络算法(2)                pp[j]
=pp[j]+qq[k]*w1[j][k];
BP神经网络算法(2)            pp[j]
=pp[j]*x1[j]*(1-x1[j]);//隐含层的校正误差
BP神经网络算法(2)
            
BP神经网络算法(2)            
for(i=0;iinnode;i++)
BP神经网络算法(2)                w[i][j]
=w[i][j]+rate_w*pp[j]*x[i];//下一次的输入层和隐含层之间的新连接权
BP神经网络算法(2)
        }

BP神经网络算法(2)        
BP神经网络算法(2)        
for(k=0;koutnode;k++)
BP神经网络算法(2)            b2[k]
=b2[k]+rate_b2*qq[k];//下一次的隐含层和输出层之间的新阈值
BP神经网络算法(2)
        for(j=0;jhidenode;j++)
BP神经网络算法(2)            b1[j]
=b1[j]+rate_b1*pp[j];//下一次的输入层和隐含层之间的新阈值
BP神经网络算法(2)
        
BP神经网络算法(2)    }
//end isamp样品循环
BP神经网络算法(2)
    
BP神经网络算法(2)}

BP神经网络算法(2)BP神经网络算法(2)
/**////////////////////////////end train/////////////////////////////
BP神经网络算法(2)
BP神经网络算法(2)
/////////////////////////////////////////////////////////////////

BP神经网络算法(2)
BP神经网络算法(2)
double *BpNet::recognize(double *p)
BP神经网络算法(2)BP神经网络算法(2)
BP神经网络算法(2){   
BP神经网络算法(2)    
double x[innode]; //输入向量
BP神经网络算法(2)
    double x1[hidenode];//隐含结点状态值
BP神经网络算法(2)
    double x2[outnode];//输出结点状态值
BP神经网络算法(2)
    double o1[hidenode];//隐含层激活值
BP神经网络算法(2)
    double o2[hidenode];//输出层激活值
BP神经网络算法(2)

BP神经网络算法(2)    
for(int i=0;iinnode;i++)
BP神经网络算法(2)        x[i]
=p[i];
BP神经网络算法(2)    
for(int j=0;jhidenode;j++)
BP神经网络算法(2)BP神经网络算法(2)    
BP神经网络算法(2){
BP神经网络算法(2)        o1[j]
=0.0;
BP神经网络算法(2)        
BP神经网络算法(2)        
for(int i=0;iinnode;i++)
BP神经网络算法(2)            o1[j]
=o1[j]+w[i][j]*x[i];//隐含层各单元激活值
BP神经网络算法(2)
        x1[j]=1.0/(1.0+exp(-o1[j]-b1[j]));//隐含层各单元输出
BP神经网络算法(2)        
//if(o1[j]+b1[j]>0) x1[j]=1;
BP神经网络算法(2)        
//    else x1[j]=0;
BP神经网络算法(2)
    }

BP神经网络算法(2)    
BP神经网络算法(2)    
for(int k=0;koutnode;k++)
BP神经网络算法(2)BP神经网络算法(2)    
BP神经网络算法(2){
BP神经网络算法(2)        o2[k]
=0.0;
BP神经网络算法(2)        
for(int j=0;jhidenode;j++)
BP神经网络算法(2)            o2[k]
=o2[k]+w1[j][k]*x1[j];//输出层各单元激活值
BP神经网络算法(2)
        x2[k]=1.0/(1.0+exp(-o2[k]-b2[k]));//输出层各单元输出
BP神经网络算法(2)        
//if(o2[k]+b2[k]>0) x2[k]=1;
BP神经网络算法(2)        
//else x2[k]=0;
BP神经网络算法(2)
    }
 
BP神经网络算法(2)    
BP神经网络算法(2)    
for(k=0;koutnode;k++)
BP神经网络算法(2)BP神经网络算法(2)    
BP神经网络算法(2){
BP神经网络算法(2)        shuchu[k]
=x2[k];
BP神经网络算法(2)    }
 
BP神经网络算法(2)    
return shuchu;
BP神经网络算法(2)BP神经网络算法(2)}
/**/////////////////////////////end sim///////////////////////////
BP神经网络算法(2)
BP神经网络算法(2)
void BpNet::writetrain()
BP神经网络算法(2)BP神经网络算法(2)
BP神经网络算法(2){//曾志军 for 2006.7
BP神经网络算法(2)
    AfxMessageBox("你还没有训练呢,训练后再写吧!请不要乱写,除非你认为这次训练是最好的,否则会覆盖我训练好的权值,那样你又要花时间训练!");
BP神经网络算法(2)    AfxMessageBox(
"你认为这次训练结果是最好的,就存下来,下次就不要花时间训练了!",MB_YESNO,NULL);
BP神经网络算法(2)    FILE 
*stream0;
BP神经网络算法(2)    FILE 
*stream1;
BP神经网络算法(2)    FILE 
*stream2;
BP神经网络算法(2)    FILE 
*stream3;
BP神经网络算法(2)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance Mar 26, 2024 pm 12:41 PM

CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance

YOLO is immortal! YOLOv9 is released: performance and speed SOTA~ YOLO is immortal! YOLOv9 is released: performance and speed SOTA~ Feb 26, 2024 am 11:31 AM

YOLO is immortal! YOLOv9 is released: performance and speed SOTA~

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Jun 03, 2024 pm 01:25 PM

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions

1.3ms takes 1.3ms! Tsinghua's latest open source mobile neural network architecture RepViT 1.3ms takes 1.3ms! Tsinghua's latest open source mobile neural network architecture RepViT Mar 11, 2024 pm 12:07 PM

1.3ms takes 1.3ms! Tsinghua's latest open source mobile neural network architecture RepViT

Explore the underlying principles and algorithm selection of the C++sort function Explore the underlying principles and algorithm selection of the C++sort function Apr 02, 2024 pm 05:36 PM

Explore the underlying principles and algorithm selection of the C++sort function

Improved detection algorithm: for target detection in high-resolution optical remote sensing images Improved detection algorithm: for target detection in high-resolution optical remote sensing images Jun 06, 2024 pm 12:33 PM

Improved detection algorithm: for target detection in high-resolution optical remote sensing images

Can artificial intelligence predict crime? Explore CrimeGPT's capabilities Can artificial intelligence predict crime? Explore CrimeGPT's capabilities Mar 22, 2024 pm 10:10 PM

Can artificial intelligence predict crime? Explore CrimeGPT's capabilities

Exploring Siamese networks using contrastive loss for image similarity comparison Exploring Siamese networks using contrastive loss for image similarity comparison Apr 02, 2024 am 11:37 AM

Exploring Siamese networks using contrastive loss for image similarity comparison

See all articles