bp.h
#pragma once
#include
#include
#include
#include
#include
using std:
:vector;
using std:
:exp;
using std:
:cout;
using std:
:endl;
class bp
;
bp.cpp
#include
"bp.h"
bp::
bp(vector<
int>
const
& layer_num, vectordouble
>>
const
& input,
vectordouble
>>
const
& output_y,
double hh,
double allerror,
int studynum)
a[0]= input;
studynum = studynum;
}bp::bp
(); a =
; y =
; h =0;
allowerror =0;
}bp::~
bp()void bp:
:setlayernuminput
(vector<
int>
const
& layer_num, vectordouble
>>
const
& input)
a[0]= input;
}void bp:
:setoutputy
(vectordouble
>>
const
& output_y)
void bp:
:setherrorstudynum
(double hh,
double allerror,
int studynum)
//初始化權重矩陣
void bp:
:iniwb()
}}void bp:
:inidwdb()
}}//啟用函式
double bp:
:sigmoid
(double z)
void bp:
:forward()
}}}void bp:
:backward()
//處理剩下層的權重w的增量dw
for(
int l = layernum.
size()
-2; l >
0; l--)}
}//計算dw與db平均值
for(
int l =
0; l < layernum.
size()
-1; l++)}
//更新引數w與b
for(
int l =
0; l < layernum.
size()
-1; l++)}
}double bp:
:error()
error = error / x[0]
.size()
;//求對每一組樣本的誤差平均
error = error /2;
cout << error << endl;
return error;
}//執行神經網路
void bp:
:run()
backward()
;}if(i ==
10000
) cout <<
"study failed!"
<< endl;
}vector<
double
> bp:
:predict
(vector<
double
>
& input)
return a1[layernum.
size()
-1];
}
驗證程式:
#include
"bp.h"
intmain()
; vectordouble
>> input_a0 =};
vectordouble
>> output_y =};
bp bp
(layer_num, input_a0,output_y,
0.6,
0.001
,2000);
bp.run();
for(
int j =
0; j <
30; j++);
vector<
double
> output = bp.
predict
(input)
;for
(auto i : output)
cout <<
"j:"
<<
0.5*j <<
" pridict:"
<< i <<
" ";
cout << endl;
}system
("pause");
return0;
}
C 實現神經BP神經網路
bp.h pragma once include include include include include using std vector using std exp using std cout using std endl class bp bp.cpp include bp.h bp ...
BP神經網路 c 實現
bp神經網路是通過反向傳播來逐漸修正層之間的權值和每個節點的閾值,可以通過學習率避免走入區域性最優解。include include include include include include include using namespace std define layer 3 三層神經網路 d...
BP神經網路
基本bp神經網路演算法包括 訊號的前向傳播 誤差的反向傳播 也即計算實際輸出時按照輸入到輸出的方向進行,權值閾值調整則相反。bp是一種多層前饋神經網路,由輸入層 隱含層和輸出層組成。層與層之間有兩種訊號在流動 一種是從輸入流向輸出的工作訊號,是輸入和權值的函式 另一種是輸入流向輸出的訊號,即誤差。隱...