Exemplo n.º 1
0
        public override void Fit(double[,] Xtrain, int[,] Ytrain, int epochs, double learningRate, double lambda)
        {
            var             amountOfExamples = Xtrain.GetLength(0);
            int             iteration        = 0;
            double          cost             = 0;
            List <double[]> actualOutputs    = new List <double[]>(Ytrain.GetLength(0));

            while (iteration < epochs)
            {
                iteration++;

                for (var i = 0; i < Xtrain.GetLength(0); i++)
                {
                    Feedforward(Xtrain.GetRow(i));
                    var output = Backpropagate(Ytrain.GetRow(i));
                    actualOutputs.Add(output);

                    if (i % 5000 == 0)
                    {
                        Console.WriteLine("Epoch: {0} step {1}/total {2}", iteration, i, amountOfExamples);
                    }
                }

                // update weights and biases
                optimizer.Run(layers, learningRate, lambda, Xtrain.GetLength(0));

                Clear();

                // calculate of cost function value
                try
                {
                    if (regularization != null)
                    {
                        var term1 = costFunction.Function(Ytrain, actualOutputs);
                        var reg   = regularization(lambda, amountOfExamples, layers);

                        cost = term1 + reg;
                    }
                    else
                    {
                        throw new InvalidOperationException("The regularization have not been defined.");
                    }
                }
                catch (Exception) { }

                actualOutputs.Clear();

                Console.WriteLine("Epoch: {0}, cost {1} ", iteration, cost);
            }
        }