/// <summary>
        /// Construct a BackpropagationLayer object that corresponds to a specific neuron layer.
        /// </summary>
        /// <param name="backpropagation">The back propagation training object.</param>
        /// <param name="layer">The layer that this object corresponds to.</param>
        public BackpropagationLayer(Backpropagation backpropagation,
                 FeedforwardLayer layer)
        {
            this.backpropagation = backpropagation;
            this.layer = layer;

            int neuronCount = layer.NeuronCount;

            this.error = new double[neuronCount];
            this.errorDelta = new double[neuronCount];

            if (layer.Next != null)
            {
                this.accMatrixDelta = new Matrix.Matrix(layer.NeuronCount + 1, layer.Next.NeuronCount);
                this.matrixDelta = new Matrix.Matrix(layer.NeuronCount + 1, layer.Next.NeuronCount);
                this.biasRow = layer.NeuronCount;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Construct a BackpropagationLayer object that corresponds to a specific neuron layer.
        /// </summary>
        /// <param name="backpropagation">The back propagation training object.</param>
        /// <param name="layer">The layer that this object corresponds to.</param>
        public BackpropagationLayer(Backpropagation backpropagation,
                                    FeedforwardLayer layer)
        {
            this.backpropagation = backpropagation;
            this.layer           = layer;

            int neuronCount = layer.NeuronCount;

            this.error      = new double[neuronCount];
            this.errorDelta = new double[neuronCount];

            if (layer.Next != null)
            {
                this.accMatrixDelta = new Matrix.Matrix(layer.NeuronCount + 1, layer.Next.NeuronCount);
                this.matrixDelta    = new Matrix.Matrix(layer.NeuronCount + 1, layer.Next.NeuronCount);
                this.biasRow        = layer.NeuronCount;
            }
        }
Beispiel #3
0
        public void ThreadProc()
        {

            int update = 0;

            Train train = new Backpropagation(this.network, PruneSelectiveForm.XOR_INPUT,
                   this.obtainIdeal(), 0.7, 0.9);

            int max = 10000;
            for (int i = 0; i < max; i++)
            {
                train.Iteration();

                update++;
                if (update == 100)
                {
                    SetText("Cycles Left:" + (max - i) + ",Error:"
                            + train.Error);
                    update = 0;
                }
            }

        }
        private void TrainNetworkBackpropBackprop()
        {
            HeatonResearchNeural.Feedforward.Train.Train train = new Backpropagation(this.network, this.input,
                   this.ideal, 0.7, 0.5);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine("Backprop:Iteration #" + epoch + " Error:"
                        + train.Error);
                epoch++;
            } while ((train.Error > Config.ACCEPTABLE_ERROR));
        }
Beispiel #5
0
        /// <summary>
        /// Create, train and use a neural network for XOR.
        /// </summary>
        /// <param name="args">Not used</param>
        static void Main(string[] args)
        {
            /*XOR_INPUT = new double[256][];
            XOR_IDEAL = new double[256][];

            String[] lines = File.ReadAllLines("input.txt");
            for (int i = 0; i < lines.Length; i++)
            {
                String str = lines[i];
                double[] input = new double[8];
                for (int j = 0; j < 8; j++)
                {
                    input[j] = Double.Parse(str[j].ToString());
                }
                XOR_INPUT[i] = input;
            }

            lines = File.ReadAllLines("output.txt");
            for (int i = 0; i < lines.Length; i++)
            {
                String str = lines[i];
                double[] output = new double[1];

                output[0] = Double.Parse(str);

                XOR_IDEAL[i] = output;
            }
            */
            FeedforwardNetwork network = new FeedforwardNetwork();
            network.AddLayer(new FeedforwardLayer(4));
            network.AddLayer(new FeedforwardLayer(12));
            network.AddLayer(new FeedforwardLayer(4));
            network.Reset();

            // train the neural network
            Train train = new Backpropagation(network, XOR_INPUT, XOR_IDEAL,
                    0.2, 0.95);

            int epoch = 1;

            Stopwatch watch = new Stopwatch();
            watch.Start();
            do
            {
                train.Iteration();
                Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
                epoch++;
            } while ((epoch < 40000) && (train.Error > 0.001));
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds / 1000.0);
            // test the neural network
            #if DEBUG
            Console.WriteLine("Neural Network Results:");
            for (int i = 0; i < XOR_IDEAL.Length; i++)
            {
                double[] actual = network.ComputeOutputs(XOR_INPUT[i]);
                Console.WriteLine(XOR_INPUT[i][0] + "," + XOR_INPUT[i][1] + "," + XOR_INPUT[i][2] + "," + XOR_INPUT[i][3]
                        + ", actual=" + actual[0] + "," + actual[1] + "," + actual[2] + "," + actual[3] + ",ideal=" + XOR_IDEAL[i][0] + "," + XOR_IDEAL[i][1] + "," + XOR_IDEAL[i][2] + "," + XOR_IDEAL[i][3]);
            }
            #endif

            FeedforwardLayer inputLayer = network.InputLayer;
            Console.WriteLine("Input layer matrix:");
            Matrix layerMatrix = inputLayer.LayerMatrix;

            using (StreamWriter file = new StreamWriter("weights.txt"))
            {
                for (int i = 0; i < layerMatrix.Rows; i++)
                {
                    for (int j = 0; j < layerMatrix.Cols; j++)
                    {
                        Console.Write(Math.Round(layerMatrix[i, j], 3) + " ");
                        file.Write(layerMatrix[i, j]);
                        if (j != layerMatrix.Cols - 1)
                        {
                            file.Write(" ");
                        }
                    }
                    Console.WriteLine();
                    file.WriteLine();
                }

                file.WriteLine();

                FeedforwardLayer hiddenLayer = network.HiddenLayers.ToList()[0];
                Console.WriteLine("Hidden layer matrix:");
                layerMatrix = hiddenLayer.LayerMatrix;
                for (int i = 0; i < layerMatrix.Rows; i++)
                {
                    for (int j = 0; j < layerMatrix.Cols; j++)
                    {
                        Console.Write(Math.Round(layerMatrix[i, j], 3) + " ");
                        file.Write(layerMatrix[i, j]);
                        if (j != layerMatrix.Cols - 1)
                        {
                            file.Write(" ");
                        }
                    }
                    Console.WriteLine();
                    file.WriteLine();
                }
            }

            #if SHOW_MATRIX
            FeedforwardLayer outputLayer = network.OutputLayer;
            Console.WriteLine("Output layer matrix:");
            layerMatrix = outputLayer.LayerMatrix;
            for (int i = 0; i < layerMatrix.Rows; i++)
            {
                for (int j = 0; j < layerMatrix.Cols; j++)
                {
                    Console.Write(layerMatrix[i, j] + " ");
                }
                Console.WriteLine();
            }

            foreach (FeedforwardLayer feedforwardLayer in network.Layers)
            {
                Matrix layerMatrix = feedforwardLayer.LayerMatrix;
                Console.WriteLine(feedforwardLayer.Next);
                for (int i = 0; i < layerMatrix.Rows; i++)
                {
                    for (int j = 0; j < layerMatrix.Cols; j++)
                    {
                        Console.Write(layerMatrix[i, j] + " ");
                    }
                    Console.WriteLine();
                }
            }
            #endif

            Console.ReadKey();
        }
Beispiel #6
0
        private void Entrenar(int red, int pre)
        {
            string[] patrones = new string[ejemplos];

            switch (red)
            {
            case 1:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre1[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos1[i, 1];
                    }
                }
                break;

            case 2:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre2[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos2[i, 1];
                    }
                }
                break;

            case 3:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre3[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos3[i, 1];
                    }
                }
                break;

            case 4:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre4[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos4[i, 1];
                    }
                }
                break;

            case 5:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre5[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos5[i, 1];
                    }
                }
                break;
            }

            Double[][] entrada = new double[ejemplos + 3][];
            double[]   matriz  = new double[30];
            double[]   matriz2 = new double[30];
            double[]   matriz3 = new double[30];
            //Creamos las matrices de los patrones
            for (int i = 0; i < ejemplos; i++)
            {
                double[] matrix = new double[30];
                for (int o = 0; o < 30; o++)
                {
                    matrix[o] = double.Parse(patrones[i][o].ToString());
                }
                entrada[i] = matrix;
            }
            //Creamos 3 matrices orientativas
            bool control = true;

            for (int i = 0; i < 30; i++)
            {
                matriz3[i] = 0;
                if (control)
                {
                    matriz[i]  = 1;
                    matriz2[i] = 0;
                    control    = false;
                }
                else
                {
                    matriz[i]  = 0;
                    matriz2[i] = 1;
                    control    = true;
                }
            }
            entrada[ejemplos]     = matriz;
            entrada[ejemplos + 1] = matriz2;
            entrada[ejemplos + 2] = matriz3;
            //Creamos la matriz ideal
            Double[][] ideal = new double[ejemplos + 3][];
            for (int i = 0; i < ejemplos; i++)
            {
                ideal[i] = new double[] { 1 }
            }
            ;
            for (int i = ejemplos; i < ejemplos + 3; i++)
            {
                ideal[i] = new double[] { 0 }
            }
            ;

            //COMENZAMOS A CREAR LA RED NEURONAL!!!!
            FeedforwardNetwork neurosis = new FeedforwardNetwork();

            neurosis.AddLayer(new FeedforwardLayer(30));
            neurosis.AddLayer(new FeedforwardLayer(30));
            neurosis.AddLayer(new FeedforwardLayer(1));
            neurosis.Reset();
            Train entrenador = new HeatonResearchNeural.Feedforward.Train.Backpropagation.Backpropagation
                                   (neurosis, entrada, ideal, 0.09, 0.9);

            for (int e = 0; e < 1000; e++)
            {
                Console.WriteLine("Error: " + entrenador.Error);
                entrenador.Iteration();
                if (entrenador.Error < 0.012)
                {
                    break;
                }
            }
            redes[red - 1, pre] = neurosis;
        }
Beispiel #7
0
        private void trainNetworkBackprop()
        {
            Train train = new Backpropagation(this.network, this.input,
                   this.ideal, 0.001, 0.1);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine("Iteration #" + epoch + " Error:"
                        + train.Error);
                epoch++;
            } while ((epoch < 5000) && (train.Error > 0.01));
        }
        private void trainNetworkBackprop()
        {
            Train train = new Backpropagation(this.network, this.input,
                   this.ideal, 0.000001, 0.1);
            double lastError = Double.MaxValue;
            int epoch = 1;
            int lastAnneal = 0;

            do
            {
                train.Iteration();
                double error = train.Error;

                Console.WriteLine("Iteration(Backprop) #" + epoch + " Error:"
                        + error);

                if (error > 0.05)
                {
                    if ((lastAnneal > 100) && (error > lastError || Math.Abs(error - lastError) < 0.0001))
                    {
                        trainNetworkAnneal();
                        lastAnneal = 0;
                    }
                }

                lastError = train.Error;
                epoch++;
                lastAnneal++;
            } while (train.Error > MAX_ERROR);
        }
Beispiel #9
0
        /// <summary>
        /// Method that is called to start the incremental prune process.
        /// </summary>
        public void StartIncremental()
        {
            this.hiddenNeuronCount = 1;
            this.cycles = 0;
            this.done = false;

            this.currentNetwork = new FeedforwardNetwork();
            this.currentNetwork
                    .AddLayer(new FeedforwardLayer(this.train[0].Length));
            this.currentNetwork.AddLayer(new FeedforwardLayer(
                    this.hiddenNeuronCount));
            this.currentNetwork
                    .AddLayer(new FeedforwardLayer(this.ideal[0].Length));
            this.currentNetwork.Reset();

            this.backprop = new Backpropagation(this.currentNetwork, this.train,
                    this.ideal, this.rate, this.momentum);

        }
Beispiel #10
0
        /// <summary>
        /// Internal method that is called at the end of each incremental cycle.
        /// </summary>
        protected void Increment()
        {
            bool doit = false;

            if (this.markErrorRate == 0)
            {
                this.markErrorRate = this.error;
                this.sinceMark = 0;
            }
            else
            {
                this.sinceMark++;
                if (this.sinceMark > 10000)
                {
                    if ((this.markErrorRate - this.error) < 0.01)
                    {
                        doit = true;
                    }
                    this.markErrorRate = this.error;
                    this.sinceMark = 0;
                }
            }

            if (this.error < this.maxError)
            {
                this.done = true;
            }

            if (doit)
            {
                this.cycles = 0;
                this.hiddenNeuronCount++;

                this.currentNetwork = new FeedforwardNetwork();
                this.currentNetwork.AddLayer(new FeedforwardLayer(
                        this.train[0].Length));
                this.currentNetwork.AddLayer(new FeedforwardLayer(
                        this.hiddenNeuronCount));
                this.currentNetwork.AddLayer(new FeedforwardLayer(
                        this.ideal[0].Length));
                this.currentNetwork.Reset();

                this.backprop = new Backpropagation(this.currentNetwork,
                        this.train, this.ideal, this.rate, this.momentum);
            }
        }