//Back Propagate for Output Neuron public void BackPropag(vNeuron[,] net, List <double> expOut, double learnRate, double bLearnRate) { List <double> error = Enumerable.Repeat(0d, this.postActOutput.Count).ToList(); for (int i = 0; i < expOut.Count; i++) { error[i] = this.postActOutput[i] - expOut[i]; } //List<double> error = MyMath.colSub(this.postActOutput, expOut); List <double> fPrime = new List <double>(); if (this.actFunc == actFuncType.logistic) { for (int i = 0; i < this.output.Count; i++) { fPrime.Add(MyMath.logisticPrimeFunc(this.output[i])); } } else if (this.actFunc == actFuncType.identity) { for (int i = 0; i < this.output.Count; i++) { fPrime.Add(1d); } } List <List <double> > result = MyMath.pointwiseMatMulti(MyMath.makeHorizonColMat(error), MyMath.makeHorizonColMat(fPrime)); this.delta = result[0]; this.beta = MyMath.matSub(MyMath.makeHorizonColMat(this.beta), MyMath.scalarProd(bLearnRate, MyMath.makeHorizonColMat(this.delta)))[0]; for (int i = 0; i < this.inputNeurons.Count; i++) { int m = inputNeurons[i][0]; int n = inputNeurons[i][1]; if (net[m, n].handedDownDelta.Count > 0) { net[m, n].handedDownDelta[0] /*.Add(this.delta);*/ = MyMath.colAdd(net[m, n].handedDownDelta[0], this.delta); } else { net[m, n].handedDownDelta.Add(Enumerable.Repeat(0d, delta.Count).ToList()); net[m, n].handedDownDelta[0] /*.Add(this.delta);*/ = MyMath.colAdd(net[m, n].handedDownDelta[0], this.delta); } List <List <double> > matrix = this.weights[i]; this.newWeights.Add(MyMath.matSub(matrix, MyMath.scalarProd(learnRate, MyMath.matMulti(MyMath.makeVertiColMat(this.delta), MyMath.makeHorizonColMat(this.inputs[i]))))); } }
//Back Propagate for Hidden Neuron public void BackPropag(mNeuron[,] net, double learnRate, double bLearnRate) { List <List <double> > fPrime = new List <List <double> >(); if (this.actFunc == actFuncType.logistic) { for (int i = 0; i < this.output.Count; i++) { fPrime.Add(new List <double>()); for (int j = 0; j < this.output[0].Count; j++) { fPrime[i].Add(MyMath.logisticPrimeFunc(this.output[i][j])); } } } else if (this.actFunc == actFuncType.identity) { for (int i = 0; i < this.output.Count; i++) { fPrime.Add(new List <double>()); for (int j = 0; j < this.output[0].Count; j++) { fPrime[i].Add(1d); } } } for (int i = 0; i < this.inputNeurons.Count; i++) { List <List <double> > error = MyMath.matMulti(MyMath.transposeMat(this.weights[i]), this.handedDownDelta); List <List <double> > result = MyMath.pointwiseMatMulti(error, fPrime); this.delta = result; this.beta = MyMath.matSub(this.beta, MyMath.scalarProd(bLearnRate, this.delta)); int m = inputNeurons[i][0]; int n = inputNeurons[i][1]; net[m, n].handedDownDelta = MyMath.matAdd(net[m, n].handedDownDelta, this.delta); List <List <double> > matrix = this.weights[i]; List <List <double> > gradient = MyMath.matMulti(this.delta, MyMath.transposeMat(this.inputs[i])); this.newWeights.Add(MyMath.matSub(matrix, MyMath.scalarProd(learnRate, gradient))); } }