internal void BackPropagate(double error, double[] labels, ErrorFunction.ErrorFunction lossFunction)
        {
            for (int j = 0; j < CostDerivatives.Length; j++)
            {
                CostDerivatives[j] = lossFunction.GetDerivativeValue(labels[j], Activations[j]);
                //2.0 * (this.Layers[l].Activations[j] - labels[j]);
            }

            for (int j = 0; j < Activations.Length; j++)
            {
                BiasVectorChangeRecord[j] += ActivationFunction.GetDerivativeValue(WeightedSum[j]) * CostDerivatives[j];
                for (int k = 0; k < WeightMatrix.GetLength(1); k++)
                {
                    WeightMatrixChangeRecord[j, k] += Previous.Activations[k]
                                                      * ActivationFunction.GetDerivativeValue(WeightedSum[j])
                                                      * CostDerivatives[j];
                }
            }
        }
Example #2
0
        public void Train2(DataSet.DataSet dataSet, int epochs)
        {
            Console.WriteLine("Initial Loss:" + CalculateMeanErrorOverDataSet(dataSet));
            for (int i = 0; i < epochs; i++)
            {
                dataSet.Shuffle();
                List <DataRow> batch = dataSet.NextBatch(this.Batching);

                int count = 0;
                foreach (DataRow example in batch)
                {
                    count++;

                    double[] result = this.FeedForward(example.GetFeatures());
                    double[] labels = example.GetLabels();
                    if (result.Length != labels.Length)
                    {
                        throw new Exception("Inconsistent array size, Incorrect implementation.");
                    }
                    else
                    {
                        double error = CalculateExampleLost(example);


                        for (int l = this.Layers.Count - 1; l > 0; l--)
                        {
                            if (l == this.Layers.Count - 1)
                            {
                                for (int j = 0; j < this.Layers[l].CostDerivatives.Length; j++)
                                {
                                    this.Layers[l].CostDerivatives[j] = ErrorFunction.GetDerivativeValue(labels[j], this.Layers[l].Activations[j]);
                                }
                            }
                            else
                            {
                                for (int j = 0; j < this.Layers[l].CostDerivatives.Length; j++)
                                {
                                    double acum = 0;
                                    for (int j2 = 0; j2 < Layers[l + 1].Size; j2++)
                                    {
                                        acum += Layers[l + 1].WeightMatrix[j2, j] * this.Layers[l + 1].ActivationFunction.GetDerivativeValue(Layers[l + 1].WeightedSum[j2]) * Layers[l + 1].CostDerivatives[j2];
                                    }
                                    this.Layers[l].CostDerivatives[j] = acum;
                                }
                            }

                            for (int j = 0; j < this.Layers[l].Activations.Length; j++)
                            {
                                this.Layers[l].BiasVectorChangeRecord[j] += this.Layers[l].ActivationFunction.GetDerivativeValue(Layers[l].WeightedSum[j]) * Layers[l].CostDerivatives[j];
                                for (int k = 0; k < Layers[l].WeightMatrix.GetLength(1); k++)
                                {
                                    this.Layers[l].WeightMatrixChangeRecord[j, k] += Layers[l - 1].Activations[k]
                                                                                     * this.Layers[l].ActivationFunction.GetDerivativeValue(Layers[l].WeightedSum[j])
                                                                                     * Layers[l].CostDerivatives[j];
                                }
                            }
                        }
                    }
                }
                TakeGradientDescentStep(batch.Count);

                if ((i + 1) % (epochs / 10) == 0)
                {
                    Console.WriteLine("Epoch " + (i + 1) + ", Avg.Loss:" + CalculateMeanErrorOverDataSet(dataSet));
                }
            }
        }