search
HomeDatabaseMysql TutorialBP神经网络算法(2)

// 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
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
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version