//Forward Propagate this Neuron public void ForwardPropag(vNeuron[,] nn) { if (this.layer != 0) { this.GetInputs(nn); } this.output = Enumerable.Repeat(0d, this.inputs[0].Count).ToList(); this.postActOutput = Enumerable.Repeat(0d, this.inputs[0].Count).ToList(); this.handedDownDelta = main.createNewMatrix(inputs[0].Count, inputs[0].Count); for (int i = 0; i < this.inputs.Count; i++) { List <double> curInput = this.inputs[i]; List <List <double> > result = MyMath.matMulti(this.weights[i], MyMath.makeVertiColMat(curInput)); this.output = MyMath.transposeMat(MyMath.matAdd(MyMath.makeVertiColMat(this.output), result))[0]; } if (this.actFunc == actFuncType.logistic) { for (int j = 0; j < this.output.Count; j++) { this.postActOutput[j] = MyMath.logisticFunc(this.output[j], beta[j]); } } else if (this.actFunc == actFuncType.identity) { for (int j = 0; j < this.output.Count; j++) { this.postActOutput[j] = MyMath.identityFunc(this.output[j], beta[j]); } } }
//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))); } }
//Forward Propagate this Neuron public void ForwardPropag(mNeuron[,] nn) { if (this.layer != 0) { this.GetInputs(nn); } this.output = main.createNewMatrix(this.inputs[0].Count, this.inputs[0][0].Count); this.postActOutput = main.createNewMatrix(this.inputs[0].Count, this.inputs[0][0].Count); this.handedDownDelta = main.createNewMatrix(this.inputs[0].Count, this.inputs[0][0].Count); for (int i = 0; i < this.inputs.Count; i++) { List <List <double> > curInput = this.inputs[i]; List <List <double> > result = MyMath.matMulti(this.weights[i], curInput); this.output = MyMath.matAdd(this.output, result); } if (this.actFunc == actFuncType.logistic) { for (int m = 0; m < output.Count; m++) { for (int n = 0; n < output[0].Count; n++) { this.postActOutput[m][n] = MyMath.logisticFunc(this.output[m][n], this.beta[m][n]); } } } else if (this.actFunc == actFuncType.identity) { for (int m = 0; m < output.Count; m++) { for (int n = 0; n < output[0].Count; n++) { this.postActOutput[m][n] = MyMath.identityFunc(this.output[m][n], this.beta[m][n]); } } } }