Encog.Neural.Thermal.HopfieldNetwork.AddPattern C# (CSharp) Method

AddPattern() public method

Train the neural network for the specified pattern. The neural network can be trained for more than one pattern. To do this simply call the train method more than once.
public AddPattern ( IMLData pattern ) : void
pattern IMLData The pattern to train for.
return void
        public void AddPattern(IMLData pattern)
        {
            if (pattern.Count != NeuronCount)
            {
                throw new NeuralNetworkError("Network with " + NeuronCount
                                             + " neurons, cannot learn a pattern of size "
                                             + pattern.Count);
            }

            // Create a row matrix from the input, convert boolean to bipolar
            Matrix m2 = Matrix.CreateRowMatrix(pattern.Data);
            // Transpose the matrix and multiply by the original input matrix
            Matrix m1 = MatrixMath.Transpose(m2);
            Matrix m3 = MatrixMath.Multiply(m1, m2);

            // matrix 3 should be square by now, so create an identity
            // matrix of the same size.
            Matrix identity = MatrixMath.Identity(m3.Rows);

            // subtract the identity matrix
            Matrix m4 = MatrixMath.Subtract(m3, identity);

            // now add the calculated matrix, for this pattern, to the
            // existing weight matrix.
            ConvertHopfieldMatrix(m4);
        }

Usage Example

        public void Execute(IExampleInterface app)
        {
            this.app = app;

            // Create the neural network.
            BasicLayer hopfield;
            var network = new HopfieldNetwork(4);

            // This pattern will be trained
            bool[] pattern1 = {true, true, false, false};
            // This pattern will be presented
            bool[] pattern2 = {true, false, false, false};
            IMLData result;

            var data1 = new BiPolarMLData(pattern1);
            var data2 = new BiPolarMLData(pattern2);
            var set = new BasicMLDataSet();
            set.Add(data1);

            // train the neural network with pattern1
            app.WriteLine("Training Hopfield network with: "
                          + FormatBoolean(data1));

            network.AddPattern(data1);
            // present pattern1 and see it recognized
            result = network.Compute(data1);
            app.WriteLine("Presenting pattern:" + FormatBoolean(data1)
                          + ", and got " + FormatBoolean(result));
            // Present pattern2, which is similar to pattern 1. Pattern 1
            // should be recalled.
            result = network.Compute(data2);
            app.WriteLine("Presenting pattern:" + FormatBoolean(data2)
                          + ", and got " + FormatBoolean(result));
        }