Пример #1
0
        public double[,] Train(double[] inputArray, double[] targetsArray)
        {
            var inputs  = Matrixs.FromArray(inputArray);
            var targets = Matrixs.FromArray(targetsArray);

            // Generating hidden outputs
            var hidden = Matrixs.Multiply(weights_ih, inputs);

            hidden.Add(bias_h);
            // Activation function
            hidden.Map(Sigmoid);


            // Generating output's output
            var outputs = Matrixs.Multiply(weights_ho, hidden);

            outputs.Add(bias_o);
            // Activation function
            outputs.Map(Sigmoid);


            // Convert array to matrix object
            var outputErrors = Matrixs.Subtract(targets, outputs);


            // Using stochastic gradient descent
            // Calculate gradient
            var gradients = Matrixs.Map(outputs, DSigmoid);

            //outputs.Map(DSigmoid);
            gradients.Multiply(outputErrors.data); // Element wise multuplication
            gradients.Multiply(learningRate);


            // Calculate deltas
            var hidden_T          = Matrixs.Transpose(hidden);
            var weights_ho_deltas = Matrixs.Multiply(gradients, hidden_T);

            // Adjust the weights by deltas
            weights_ho.Add(weights_ho_deltas);
            // Adjust the bias by its deltas
            bias_o.Add(gradients);


            // Calculate the error
            // ERROR = TARGETS - OUTPUTS

            // Calculate the hidden layer errors
            var weights_ho_t  = Matrixs.Transpose(weights_ho);
            var hidden_errors = Matrixs.Multiply(weights_ho_t, outputErrors);


            // Calculate the hidden gradient
            var hidden_gradient = Matrixs.Map(hidden, DSigmoid);

            hidden_gradient.Multiply(hidden_errors.data);
            hidden_gradient.Multiply(learningRate);

            // Calculate input->hidden deltas
            var inputs_T         = Matrixs.Transpose(inputs);
            var weight_ih_deltas = Matrixs.Multiply(hidden_gradient, inputs_T);

            // Adjust the weights by deltas
            weights_ih.Add(weight_ih_deltas);
            // Adjust the bias by its deltas
            bias_h.Add(hidden_gradient);

            /*
             * Console.Write("Inputs: ");
             * Matrixs.DisplayMatrix(inputArray);
             * Console.Write("Targets: ");
             * Matrixs.DisplayMatrix(targetsArray);
             * Console.Write("Errors: ");
             * Matrixs.DisplayMatrix(outputErrors);
             * Console.WriteLine();
             * Console.WriteLine();
             */


            return(outputs.data);
        }