?? bpnet.cpp
字號:
// BPnet.cpp: implementation of the BPnet class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BPtoXorS.h"
#include "BPnet.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BPnet::BPnet()
{
//input[]={0.05 , 0.95}; //為什么這樣給數組賦值在vc++中就會出錯 ?
input[0] = 0.05;
input[1] = 0.95;
output = 1.0;
//threshold_m[] = {rand() , rand()};
/*double AverageRandom(double min,double max)
{
int minInteger = (int)(min*10000);
int maxInteger = (int)(max*10000);
int randInteger = rand()*rand();
int diffInteger = maxInteger - minInteger;
int resultInteger = randInteger % diffInteger + minInteger;
return resultInteger/10000.0;
}*/
srand( (unsigned)time( NULL ) );
for(int i=0; i<2; i++)
threshold_m[i] = AverageRandom(-1,1);
threshold_o = AverageRandom(-1,1);
//w_middle[] = {rand() , rand() , rand() , rand()};
for(i=0; i<4; i++)
w_middle[i] = AverageRandom(-1,1);
//w_output[] = {rand() , rand()};
for(i=0; i<2; i++)
w_output[i] = AverageRandom(-1,1);
for(i=0; i<2; i++)
{
middle_s[i] = 0; //zero
middle_o[i] = 0; //zero
}
output_s = 0;
output_o = 0;
u0 = 0.3;
a = 0.5;
b = 0.5;
error = 5.0;
}
BPnet::~BPnet()
{
}
void BPnet::train()
{
//計算中間層激活值
for(int i=0; i<2; i++)
middle_s[0] += w_middle[i] * input[i];
middle_s[0] -= threshold_m[0];
for(i=0; i<2; i++)
middle_s[1] += w_middle[i+2] * input[i];
middle_s[1] -= threshold_m[1];
//中間層的輸出值
for(i=0; i<2; i++)
middle_o[i] = 0.5 * (1 + tan(middle_s[i] / u0));
//輸出層的激活值
for(i=0; i<2; i++)
output_s +=w_output[i] * middle_o[i];
output_s -= threshold_o;
//輸出層的輸出值
output_o = 0.5 * (1 + tan(output_s / u0));
//輸出誤差的逆傳播
//輸出層的矯正誤差
error_o = (output - output_o) * output_o * (1 + output_o);
//中間層個單元的矯正誤差
for(i=0; i<2; i++)
error_m[i] = w_output[i] * error_o * middle_o[i] *(1 + middle_o[i]);
//重新計算輸出層到中間層的權值和輸出層的閾值
for(i=0; i<2; i++)
w_output[i] += a * error_o * middle_o[i];
threshold_o += a * error_o;
//重新計算中間層到輸入層的權值和中間層的閾值
w_middle[0] += b * error_m[0] * input[0];
w_middle[1] += b * error_m[1] * input[0];
w_middle[2] += b * error_m[0] * input[1];
w_middle[3] += b * error_m[1] * input[1];
threshold_m[0] += b * error_m[0];
threshold_m[1] += b * error_m[1];
//計算誤差
error = fabs(output - output_o);
}
//求隨機數
double BPnet::AverageRandom(double min, double max)
{
int minInteger = (int)(min*10000);
int maxInteger = (int)(max*10000);
int randInteger = rand()*rand();
int diffInteger = maxInteger - minInteger;
int resultInteger = randInteger % diffInteger + minInteger;
return resultInteger/10000.0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -