




免費預(yù)覽已結(jié)束,剩余15頁可下載查看
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
bp神經(jīng)網(wǎng)絡(luò)算法D E C L A R A T I O N S /各參數(shù)量的聲明 */ #include #include #include typedef int BOOL;typedef int INT;typedef double REAL; #define FALSE 0#define TRUE 1#define NOT !#define AND define OR | #define MIN_REAL -HUGE_VAL#define MAX_REAL +HUGE_VAL#define MIN(x,y) (x)(y) ? (x) : (y) #define LO 0.1#define HI 0.9#define BIAS 1 #define sqr(x) (x)*(x) typedef struct /* A LAYER OF A NET: */ INT Units; /* - number of units in this layer /神經(jīng)元 */ REAL* Output; /* - output of ith unit /第i個神經(jīng)元的輸出 */ REAL* Error; /* - error term of ith unit /第i個神經(jīng)元的誤差 */ REAL* Weight; /* - connection weights to ith unit /權(quán)值 */ REAL* WeightSave; /* - saved weights for stopped training /閾值 */ REAL* dWeight; /* - last weight deltas for momentum /權(quán)值的增量 */ LAYER; typedef struct /* A NET: */ LAYER* Layer; /* - layers of this net /網(wǎng)絡(luò)的所有層 */ LAYER* InputLayer; /* - input layer /輸入層 */ LAYER* OutputLayer; /* - output layer /輸出層 */ REAL Alpha; /* - momentum factor /動量因子 */ REAL Eta; /* - learning rate /學(xué)習(xí)率 */ REAL Gain; /* - gain of sigmoid function /S型函數(shù)的權(quán)重 */ REAL Error; /* - total net error /輸出誤差 */ NET; /* R A N D O M S D R A W N F R O M D I S T R I B U T I O N S /隨機(jī)函數(shù)部分 */ void InitializeRandoms() srand(4711); INT RandomEqualINT(INT Low, INT High) return rand() % (High-Low+1) + Low; REAL RandomEqualREAL(REAL Low, REAL High) return (REAL) rand() / RAND_MAX) * (High-Low) + Low; /* A P P L I C A T I O N - S P E C I F I C C O D E /特殊數(shù)據(jù) */ #define NUM_LAYERS 3#define N 10#define M 1INT UnitsNUM_LAYERS = N, 20, M; #define FIRST_YEAR 1700#define NUM_YEARS 136 #define TRAIN_LWB (N)#define TRAIN_UPB (69)#define TRAIN_YEARS (TRAIN_UPB - TRAIN_LWB + 1)#define TEST_LWB (70)#define TEST_UPB (119)#define TEST_YEARS (TEST_UPB - TEST_LWB + 1)#define EVAL_LWB (120)#define EVAL_UPB (NUM_YEARS - 1)#define EVAL_YEARS (EVAL_UPB - EVAL_LWB + 1) REAL Sunspots_NUM_YEARS;REAL Sunspots NUM_YEARS = 0.421917808, 0.41369863 , 0.476712329, 0.460273973, 0.408219178, 0.378082192, 0.367123288, 0.356164384, 0.326027397, 0.37260274 , 0.350684932, 0.334246575, 0.284931507, 0.309589041, 0.260273973, 0.150684932, 0.139726027, 0.2 , 0.145205479, 0.104109589, 0.063013699, 0.093150685, 0.095890411, 0.043835616, 0.032876712, 0.046575342, 0.01369863 , 0 , 0.060273973, 0.167123288, 0.115068493, 0.191780822, 0.282191781, 0.257534247, 0.180821918, 0.197260274, 0.257534247, 0.238356164, 0.232876712, 0.268493151, 0.260273973, 0.224657534, 0.208219178, 0.260273973, 0.309589041, 0.342465753, 0.309589041, 0.287671233, 0.263013699, 0.210958904, 0.290410959, 0.306849315, 0.347945205, 0.306849315, 0.301369863, 0.257534247, 0.284931507, 0.232876712, 0.268493151, 0.345205479, 0.457534247, 0.473972603, 0.479452055, 0.375342466, 0.419178082, 0.416438356, 0.375342466, 0.394520548, 0.443835616, 0.512328767, 0.443835616, 0.550684932, 0.594520548, 0.643835616, 0.657534247, 0.780821918, 0.802739726, 0.843835616, 0.810958904, 0.780821918, 0.709589041, 0.805479452, 0.797260274, 0.78630137 , 0.802739726, 0.947945205, 0.890410959, 0.808219178, 0.723287671, 0.734246575, 0.739726027, 0.742465753, 0.764383562, 0.808219178, 0.931506849, 0.824657534, 0.753424658, 0.75890411 , 0.82739726 , 0.857534247, 0.821917808, 0.906849315, 0.887671233, 0.978082192, 0.915068493, 0.961643836, 1 , 0.936986301, 0.884931507, 0.882191781, 0.873972603, 0.789041096, 0.810958904, 0.821917808, 0.830136986, 0.84109589 , 0.802739726, 0.819178082, 0.780821918, 0.747945205, 0.706849315, 0.717808219, 0.61369863 , 0.536986301, 0.583561644, 0.57260274 , 0.463013699, 0.471232877, 0.509589041, 0.531506849, 0.471232877, 0.465753425, 0.416438356, 0.432876712, 0.432876712, 0.457534247, ; REAL Mean;REAL TrainError;REAL TrainErrorPredictingMean;REAL TestError;REAL TestErrorPredictingMean; FILE* f; void NormalizeSunspots() /規(guī)格化處理 INT Year; REAL Min, Max; Min = MAX_REAL; Max = MIN_REAL; for (Year=0; YearNUM_YEARS; Year+) Min = MIN(Min, SunspotsYear); /選出Sunspots中最小的數(shù)給Min Max = MAX(Max, SunspotsYear); /選出Sunspots中最大的數(shù)給Max Mean = 0; for (Year=0; YearAlpha = 0.5; Net-Eta = 0.05; Net-Gain = 1; NormalizeSunspots(); TrainErrorPredictingMean = 0; for (Year=TRAIN_LWB; Year=TRAIN_UPB; Year+) for (i=0; iM; i+) Out = SunspotsYear+i; Err = Mean - Out; TrainErrorPredictingMean += 0.5 * sqr(Err); /TrainErrorPredictingMean反映了神經(jīng)網(wǎng)絡(luò)期望輸出與計算輸出之間誤差大小 TestErrorPredictingMean = 0; for (Year=TEST_LWB; Year=TEST_UPB; Year+) for (i=0; iLayer = (LAYER*) calloc(NUM_LAYERS, sizeof(LAYER*); for (l=0; lLayerl = (LAYER*) malloc(sizeof(LAYER); Net-Layerl-Units = Unitsl; Net-Layerl-Output = (REAL*) calloc(Unitsl+1, sizeof(REAL); Net-Layerl-Error = (REAL*) calloc(Unitsl+1, sizeof(REAL); Net-Layerl-Weight = (REAL*) calloc(Unitsl+1, sizeof(REAL*); Net-Layerl-WeightSave = (REAL*) calloc(Unitsl+1, sizeof(REAL*); Net-Layerl-dWeight = (REAL*) calloc(Unitsl+1, sizeof(REAL*); Net-Layerl-Output0 = BIAS; /初始化Output0為1 if (l != 0) for (i=1; iLayerl-Weighti = (REAL*) calloc(Unitsl-1+1, sizeof(REAL); Net-Layerl-WeightSavei = (REAL*) calloc(Unitsl-1+1, sizeof(REAL); Net-Layerl-dWeighti = (REAL*) calloc(Unitsl-1+1, sizeof(REAL); Net-InputLayer = Net-Layer0; Net-OutputLayer = Net-LayerNUM_LAYERS - 1; Net-Alpha = 0.9; Net-Eta = 0.25; Net-Gain = 1; /隨機(jī)生成權(quán)重-0.50.5void RandomWeights(NET* Net) INT l,i,j; for (l=1; lNUM_LAYERS; l+) for (i=1; iLayerl-Units; i+) for (j=0; jLayerl-1-Units; j+) Net-Layerl-Weightij = RandomEqualREAL(-0.5, 0.5); /* S U P P O R T F O R S T O P P E D T R A I N I N G /臨界值的修改*/ /誤差在減小,保存當(dāng)前較小的權(quán)值到WeightSave中void SaveWeights(NET* Net) INT l,i,j; for (l=1; lNUM_LAYERS; l+) for (i=1; iLayerl-Units; i+) for (j=0; jLayerl-1-Units; j+) Net-Layerl-WeightSaveij = Net-Layerl-Weightij; /權(quán)值達(dá)到最小,把最小權(quán)值賦給Weightvoid RestoreWeights(NET* Net) INT l,i,j; for (l=1; lNUM_LAYERS; l+) for (i=1; iLayerl-Units; i+) for (j=0; jLayerl-1-Units; j+) Net-Layerl-Weightij = Net-Layerl-WeightSaveij; /* P R O P A G A T I N G S I G N A L S /輸入層、輸出層、隱層的傳遞處理 */ void SetInput(NET* Net, REAL* Input) INT i; for (i=1; iInputLayer-Units; i+) Net-InputLayer-Outputi = Inputi-1; void GetOutput(NET* Net, REAL* Output) INT i; for (i=1; iOutputLayer-Units; i+) Outputi-1 = Net-OutputLayer-Outputi; void PropagateLayer(NET* Net, LAYER* Lower, LAYER* Upper) /層與層之間的傳遞 INT i,j; REAL Sum; for (i=1; iUnits; i+) Sum = 0; for (j=0; jUnits; j+) /Sum為上一層的輸入和權(quán)重的乘積 Sum += Upper-Weightij * Lower-Outputj; /S型傳遞函數(shù) Upper-Outputi = 1 / (1 + exp(-Net-Gain * Sum); void PropagateNet(NET* Net) /完成一張網(wǎng)的傳播 INT l; for (l=0; lLayerl, Net-Layerl+1); /* B A C K P R O P A G A T I N G E R R O R S /反向傳播對誤差分析并調(diào)整 */ void ComputeOutputError(NET* Net, REAL* Target) /估計計算輸出值與期望輸出值的偏差 INT i; REAL Out, Err; Net-Error = 0; for (i=1; iOutputLayer-Units; i+) Out = Net-OutputLayer-Outputi; Err = Targeti-1-Out; Net-OutputLayer-Errori = Net-Gain * Out * (1-Out) * Err; Net-Error += 0.5 * sqr(Err); /層的反向傳播,并重新修改每個神經(jīng)元的誤差,使誤差逐次減少void BackpropagateLayer(NET* Net, LAYER* Upper, LAYER* Lower) INT i,j; REAL Out, Err; for (i=1; iUnits; i+) Out = Lower-Outputi; Err = 0; for (j=1; jUnits; j+) Err += Upper-Weightji * Upper-Errorj; Lower-Errori = Net-Gain * Out * (1-Out) * Err; void BackpropagateNet(NET* Net) /網(wǎng)絡(luò)的反向傳播 INT l; for (l=NUM_LAYERS-1; l1; l-) BackpropagateLayer(Net, Net-Layerl, Net-Layerl-1); void AdjustWeights(NET* Net) /修正權(quán)值 INT l,i,j; REAL Out, Err, dWeight; for (l=1; lNUM_LAYERS; l+) for (i=1; iLayerl-Units; i+) for (j=0; jLayerl-1-Units; j+) Out = Net-Layerl-1-Outputj; Err = Net-Layerl-Errori; dWeight = Net-Layerl-dWeightij; Net-Layerl-Weightij += Net-Eta * Err * Out + Net-Alpha * dWeight; Net-Layerl-dWeightij = Net-Eta * Err * Out; /* S I M U L A T I N G T H E N E T /神經(jīng)網(wǎng)絡(luò)仿真 */ /神經(jīng)網(wǎng)絡(luò)模擬void SimulateNet(NET* Net, REAL* Input, REAL* Output, REAL* Target, BOOL Training) SetInput(Net, Input); /輸入層的處理 PropagateNet(Net); /中間層的處理 GetOutput(Net, Output); /輸出層的處理 ComputeOutputError(Net, Target); /計算總的誤差 if (Training) BackpropagateNet(Net); /反向傳播,修改各神經(jīng)元的誤差 AdjustWeights(Net); /反向傳播,修正權(quán)值 void TrainNet(NET* Net, INT Epochs) /訓(xùn)練網(wǎng)絡(luò) INT Year, n; REAL OutputM; for (n=0; nEpochs*TRAIN_YEARS; n+) /訓(xùn)練次數(shù)為Epochs*TRAIN_YEARS /產(chǎn)生的隨機(jī)數(shù)在TRAIN_LWBTRAIN_UPB之間 Year = RandomEqualINT(TRAIN_LWB, TRAIN_UPB); /進(jìn)行神經(jīng)網(wǎng)絡(luò)仿真模擬 SimulateNet(Net, &(SunspotsYear-N), Output, &(SunspotsYear), TRUE); void TestNet(NET* Net) /測試網(wǎng)絡(luò) INT Year; REAL OutputM; TrainError = 0; for (Year=TRAIN_LWB; YearError; TestError = 0; for (Year=TEST_LWB; YearError; fprintf(f, nNMSE is %0.3f on Training Set and %0.3f on Test Set, TrainError / TrainErrorPredictingMean, TestError / TestErrorPredictingMean); void EvaluateNet(NET* Net)/預(yù)測 INT Year; REAL Output M; REAL Output_M; fprintf(f, nnn); fprintf(f, Year Sunspots Open-Loop Predict
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年數(shù)控超精密磨床項目申請報告
- 2025年炔烴項目立項申請報告
- 教育行業(yè)教學(xué)經(jīng)歷證明書(6篇)
- 品牌宣傳推廣合同協(xié)議
- 巧克力包裝機(jī)設(shè)計-課程設(shè)計
- 食品加工工藝與設(shè)備案例分析題
- 2025年電商數(shù)據(jù)分析與電商運(yùn)營管理專業(yè)電子商務(wù)師(初級)職業(yè)技能鑒定試卷
- 快樂讀書讀后感作文5篇
- 個人實習(xí)證明書標(biāo)題實習(xí)經(jīng)歷證明書(8篇)
- 2025年初中化學(xué)九年級上冊期中測試卷:化學(xué)與環(huán)境問題探究試題
- 液氨的管理及應(yīng)急救援處置
- 工程質(zhì)量驗收報告和竣工驗收報告
- 2022-2023學(xué)年內(nèi)蒙古赤峰市數(shù)學(xué)高一下期末統(tǒng)考模擬試題含解析
- 江西省建筑工程竣工備案表
- 幼兒園三年發(fā)展規(guī)劃第一年實施績效自評報告
- 中醫(yī)醫(yī)院中醫(yī)師帶徒協(xié)議模板范文
- GB/T 9081-2008機(jī)動車燃油加油機(jī)
- GB/T 17626.27-2006電磁兼容試驗和測量技術(shù)三相電壓不平衡抗擾度試驗
- GB/T 1185-2006光學(xué)零件表面疵病
- 2023年人社所半年工作總結(jié)
- 工業(yè)管道工程工程量清單項目設(shè)置及計價
評論
0/150
提交評論