Пример #1
0
        //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])))));
            }
        }