1.初始化点层BpNodeLayer(ListBpNode,BpNode包含属性:阈值(threshold),输出(output)和修正值(deviation))和点层间的连接线层BpLineLayer(double[upperBpNodeLayer.size()][underBpNodeLayer.size()],大小为上层点数和下层点数),其中点层的每个点的阈值随机给定,其他的都初始化为0。注:输入层是可以只有输出值的,其他的不需要,但为程序方便,此处都设定了。2.根据训练数据设定输入值,即输入层的输出值。3.计算非输入层点j的激活值为:0.*[][]njiivalueBpNodeoutputBpLineij(1)其中,.iBpNodeoutput为上一层点i的输出值,n为上一层点的总数,[][]BpLineij为上一层的点i到这一层点j的连接线的值。则该点的输出值为:1/(11/.(.,,))jjjoutputMathpowMathethresholdValue(2)其中,jthreshold为该点当前的阈值。4.重复第3步,从输入层后面一层依次向后计算各点层的输出值,直至求得输出层的输出值。5.计算这次数据的偏差:0.((,2)nkjjjerrorMathpowtruevalueoutput(3)其中,jtruevalue为点j的真实输出值(由训练数据给定),n为输出层的总点数。6.计算输出层点j的修正值:()**(1)jjjjjdeviationtruevalueoutputoutputoutput(4)7.修正连接线的值:[][]_**ijBpLineijratelineoutputdeviation(5)其中,ioutput为前一层点i的输出值,jdeviation为后一层点j的修正值,_rateline为连接线的修正比率。8.隐藏层各点的修正值为:0*(1)**[][]njjjiideviationoutputoutputthresholdBpLineji(6)9.重复步骤6、7,从最后一层线层开始向前,直至修正完所有线层。注:输入层不需要修正。10.修正各点阈值:_*jjthresholdratethresholddeviation(7)其中,jthreshold和jdeviation为修正点的阈值和修正值,_ratethreshold为阈值修正比率。11.重复步骤9,直至修正完所有非输出层的点的阈值。12.重复2-9,直至训练完所有数据。13.计算当前偏差:0_Nkierrorallerror(8)其中,N为训练集数据个数,kerror为每条训练数据的偏差值。14.重复2-13,直至_errorall小于允许的最小偏差值,或者迭代次数大于允许的最大迭代次数。注:本文程序的最大偏差值为0.0001,最大迭代次数为1000000。程序:packagebpNN;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importbpNN.BpNodeLayer.BpNode;/***@ClassName:BpNN*@Description:TODOADDFUNCTION.*@date:2015-8-2*@authorMartinLiu*@version*@paramT*/publicclassBpNNT{/***inputdata=outputdatathesiteoftheinputelement=thevalueofthe*element*/protectedListTrainingEventevents=null;/***thenodelayer*/protectedListBpNodeLayerallNodeLayers=null;/***thelinelayer*/protectedListBpLineLayerallLineLayers=null;protecteddoublecurrent_deviation=1.0;/***themaxdeviationvaluecanbepermitted*/protecteddoublemaxDeviation_permitted=0.0001;/***themaxtimesforiteratecanbepermitted*/protecteddoublemaxTimes_iterate=1000000;/***thelistforthecontinuedeviations*/protectedListDoubledeviations=newArrayListDouble();/***therateforthelearning*/privatedoublerate=0.9;/***theresultoftheBpNN*/privatedouble[]result;/***p*Description:*/p*datecreate2015-8-2**@paramevents*thedatafortraining*@paramnumInputLayerNode*thenumberoftheinputlayer*@paramnumOutputLayerNode*thenumberoftheoutputlayer*@paramlayers*thehiddenlayers*/publicBpNN(ListTrainingEventevents,intnumInputLayerNode,intnumOutputLayerNode,int[]layers){this.events=events;this.allNodeLayers=newArrayListBpNodeLayer(layers.length+2);this.allLineLayers=newArrayListBpLineLayer(layers.length+1);this.allNodeLayers.add(newBpNodeLayer(numInputLayerNode));for(inti=0;ilayers.length;i++){this.allNodeLayers.add(newBpNodeLayer(layers[i]));this.allLineLayers.add(newBpLineLayer(allNodeLayers.get(i).getLength(),allNodeLayers.get(i+1).getLength()));}//forthis.allLineLayers.add(newBpLineLayer(allNodeLayers.get(allNodeLayers.size()-1).getLength(),numOutputLayerNode));this.allNodeLayers.add(newBpNodeLayer(numOutputLayerNode));this.result=newdouble[numOutputLayerNode];inttimes=0;for(inti=0;i10;i++)this.deviations.add(0.0);while(this.current_deviationthis.maxDeviation_permitted&×this.maxTimes_iterate&&!convergence(this.current_deviation)){training();times++;System.out.println(Times=+times+,current_deviation=+this.current_deviation);}//while}/***pDescription:/p*datecreate2015-8-3*@paramcurrentDeviation*@return*/privatebooleanconvergence(doublecurrentDeviation){for(inti=0;i10;i++)if(this.deviations.get(i)!=currentDeviation){this.deviations.remove(0);this.deviations.add(currentDeviation);returnfalse;}returntrue;}/***p*Description:trainingtheBpNN*/p*datecreate2015-8-2*/privatevoidtraining(){doublebiax=0;//theeventofthetrainingdatafor(inti=0;ithis.events.size();i++){setOutputValue(this.events.get(i).getInputLayer());//System.out.println(this.events.get(i).getOutputLayer()[0]);setDeviationValue(this.events.get(i).getOutputLayer());//computethebiaxfor(intj=0;jthis.allNodeLayers.get(this.allNodeLayers.size()-1).getLength();j++)biax+=Math.pow(this.events.get(i).getOutputLayer()[j]-this.allNodeLayers.get(this.allNodeLayers.size()-1).getNodes(j).getOutput(),2);for(intj=1;jthis.allNodeLayers.size();j++)setThreshold(this.allNodeLayers.get(j));}//forthis.current_deviation=biax/2.0;}/***pDescription:/p*datecreate2015-8-3*@paramnodeLayer*/privatevoidsetThreshold(BpNodeLayernodeLayer){for(inti=0;inodeLayer.getLength();i++)nodeLayer.getNodes(i).setThreshold(rate*nodeLayer.getNodes(i).getDeviation());}/***p*Description:setallnotedeviationfromtheoutputlayertotheinupt*layer*/p*datecreate2015-8-2**@paramds*/privatevoidsetDeviationValue(double[]ds){addOutputDeviation(ds,this.allNodeLayers.get(this.allNodeLayers.size()-1),this.allLineLayers.get(this.allLineLayers.size()-1),this.allNodeLayers.get(this.allNodeLayers.size()-2));//thelinelayersitefor(inti=this.allNodeLayers.size()-2;i0;i--)setDeviationValueI(this.allNodeLayers.get(i),this.allLineLayers.get(i),this.allNodeLayers.get(i+1),this.allLineLayers.get(i-1),this.allNodeLayers.get