/// <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;
            }
        }
示例#2
0
文件: Hopfield.cs 项目: Vadim4Real/ML
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="pattern">The pattern to train on.</param>
        public void Train(bool[] pattern)
        {
            if (pattern.Length != this.weightMatrix.Rows)
            {
                throw new NeuralNetworkError("Can't train a pattern of size "
                                             + pattern.Length + " on a hopfield network of size "
                                             + this.weightMatrix.Rows);
            }

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

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

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

            // now add the calculated matrix, for this pattern, to the
            // existing weight matrix.
            this.weightMatrix = MatrixMath.Add(this.weightMatrix, m4);
        }
示例#3
0
文件: Hopfield.cs 项目: Vadim4Real/ML
        /// <summary>
        /// Present a pattern to the neural network and receive the result.
        /// </summary>
        /// <param name="pattern">The pattern to be presented to the neural network.</param>
        /// <returns>The output from the neural network.</returns>
        public bool[] Present(bool[] pattern)
        {
            bool[] output = new bool[pattern.Length];

            // convert the input pattern into a matrix with a single row.
            // also convert the boolean values to bipolar(-1=false, 1=true)
            Matrix.Matrix inputMatrix = Matrix.Matrix.CreateRowMatrix(BiPolarUtil
                                                                      .Bipolar2double(pattern));

            // Process each value in the pattern
            for (int col = 0; col < pattern.Length; col++)
            {
                Matrix.Matrix columnMatrix = this.weightMatrix.GetCol(col);
                columnMatrix = MatrixMath.Transpose(columnMatrix);

                // The output for this input element is the dot product of the
                // input matrix and one column from the weight matrix.
                double dotProduct = MatrixMath.DotProduct(inputMatrix,
                                                          columnMatrix);

                // Convert the dot product to either true or false.
                if (dotProduct > 0)
                {
                    output[col] = true;
                }
                else
                {
                    output[col] = false;
                }
            }

            return(output);
        }
示例#4
0
        /// <summary>
        /// Determine both the normalization factor and the synthetic input for the
        /// given input.
        /// </summary>
        /// <param name="input">The input to normalize.</param>
        protected void CalculateFactors(double[] input)
        {
            Matrix.Matrix inputMatrix = Matrix.Matrix.CreateColumnMatrix(input);
            double        len         = MatrixMath.vectorLength(inputMatrix);

            len = Math.Max(len, VERYSMALL);
            int numInputs = input.Length;

            if (this.type == NormalizationType.MULTIPLICATIVE)
            {
                this.normfac = 1.0 / len;
                this.synth   = 0.0;
            }
            else
            {
                this.normfac = 1.0 / Math.Sqrt(numInputs);
                double d = numInputs - Math.Pow(len, 2);
                if (d > 0.0)
                {
                    this.synth = Math.Sqrt(d) * this.normfac;
                }
                else
                {
                    this.synth = 0;
                }
            }
        }
示例#5
0
        public void Train(double[] inputArray, double[] targetArray)
        {
            //Matrix.Matrix inputMatrix = new Matrix.Matrix(inputArray);
            FillHiddenNeurons(inputArray);
            //hiddenOutput.DisplayMatrix();

            Matrix.Matrix outputsOutput = outputWeights * hiddenOutput;
            if (useBias)
            {
                outputsOutput += biasOutput;
            }
            Matrix.Matrix targetMatrix       = new Matrix.Matrix(targetArray);
            Matrix.Matrix outputErrorsMatrix = targetMatrix - outputsOutput;

            // mapMatrixLinearry(outputsOutput);   //!!! do zmiany w wariancie 2 na normalne mapowanie

            Matrix.Matrix gradients_output = outputErrorsMatrix * learningRate;

            //gradients_output.HadamardProduct(outputsOutput);
            //gradients_output.DisplayMatrix();
            Matrix.Matrix hiddenTransposed = hiddenOutput.TransposeMatrix();
            //hiddenTransposed.DisplayMatrix();
            Matrix.Matrix outputs_deltas = gradients_output * hiddenTransposed;
            //outputs_deltas.DisplayMatrix();

            outputWeights += outputs_deltas;
            biasOutput    += gradients_output;

            outputWeights += momentumMatrixOutput;
            biasOutput    += momentumMatrixOutputBias;

            momentumMatrixOutput     = outputs_deltas * momentumRate;
            momentumMatrixOutputBias = gradients_output * momentumRate;
        }
示例#6
0
        private static void Init()
        {
            R = new Matrix.Matrix(10, 1);
            K = new Matrix.Matrix(10, 10);

            // 3
            R[0, 0] = -4.0748217690E+00;
            R[1, 0] = -4.9251244659E+00;
            R[2, 0] = -5.3650596817E+00;
            R[3, 0] = -5.5725335790E+00;
            R[4, 0] = -5.6489380624E+00;
            R[5, 0] = -5.6516105985E+00;
            R[6, 0] = -5.6127699478E+00;
            R[7, 0] = -5.5501570365E+00;
            R[8, 0] = -5.4736066877E+00;
            R[9, 0] = -5.3882442310E+00;

            // 4
            K[0, 0] = 1.6603723851E-09;
            K[1, 1] = 2.4255934705E-09;
            K[2, 2] = 2.8783795974E-09;
            K[3, 3] = 3.1052955589E-09;
            K[4, 4] = 3.1910050747E-09;
            K[5, 5] = 3.1940896540E-09;
            K[6, 6] = 3.1503514453E-09;
            K[7, 7] = 3.0804600582E-09;
            K[8, 8] = 2.9960384237E-09;
            K[9, 9] = 2.9035548030E-09;
        }
示例#7
0
        private static void Run(double x, double y, double eps)
        {
            Matrix.Matrix s, S1, S2, k1, M, L, dR, dQ, Tmp;
            double        d1, d2;
            int           kh = 0;

            s  = new Matrix.Matrix(10, 1);
            S1 = new Matrix.Matrix(10, 2);
            S2 = new Matrix.Matrix(10, 2);
            M  = new Matrix.Matrix(10, 2);
            dR = new Matrix.Matrix(10, 1);

            k1 = ~K; kh = 0;

            do
            {
                num_du_rungekutt(f, g, x, y, 0, ref s);
                num_du_rungekutt(f, g, x + d, y, 0, ref S1);
                num_du_rungekutt(f, g, x - d, y, 0, ref S2);
                num_du_rungekutt(f, g, x, y + d, 1, ref S1);
                num_du_rungekutt(f, g, x, y - d, 1, ref S2);
                M  = (S1 - S2) / (2 * d);
                L  = !M;
                dR = R - s;

                Tmp = L * k1;
                dQ  = (~(Tmp * M)) * Tmp * dR;

                d1 = dQ[0, 0]; d2 = dQ[1, 0];
                x += d1; y += d2;

                kh++;
                Console.WriteLine(kh + " : x1 = " + x + "  x2 = " + y);
            } while ((Math.Abs(d1) > eps) && (Math.Abs(d2) > eps));
        }
示例#8
0
        /// <summary>
        /// Determine the winner for the specified input. This is the number of the
        /// winning neuron.
        /// </summary>
        /// <param name="input">The input pattern.</param>
        /// <returns>The winning neuron.</returns>
        public int Winner(NormalizeInput input)
        {
            int win = 0;

            double biggest = Double.MinValue;

            for (int i = 0; i < this.outputNeuronCount; i++)
            {
                Matrix.Matrix optr = this.outputWeights.GetRow(i);
                this.output[i] = MatrixMath
                                 .DotProduct(input.InputMatrix, optr)
                                 * input.Normfac;

                this.output[i] = (this.output[i] + 1.0) / 2.0;

                if (this.output[i] > biggest)
                {
                    biggest = this.output[i];
                    win     = i;
                }

                if (this.output[i] < 0)
                {
                    this.output[i] = 0;
                }

                if (this.output[i] > 1)
                {
                    this.output[i] = 1;
                }
            }

            return(win);
        }
示例#9
0
        private static void num_du_rungekutt(function f, function g,
                                             double x0, double y0, int iColumn2Write,
                                             ref Matrix.Matrix Matr)
        {
            double t = 0, x1 = x0, y1 = y0;
            int    j = 0;
            double k1, k2, k3, k4, m1, m2, m3, m4;

            for (int i = 1; i <= 500; i++)             // max 5
            {
                t += h_rungekutt;
                k1 = f(t, x1, y1);
                m1 = g(t, x1, y1);
                k2 = f(t + h_rungekutt / 2, x1 + h_rungekutt * k1 / 2, y1 + h_rungekutt * m1 / 2);
                m2 = g(t + h_rungekutt / 2, x1 + h_rungekutt * k1 / 2, y1 + h_rungekutt * m1 / 2);
                k3 = f(t + h_rungekutt / 2, x1 + h_rungekutt * k2 / 2, y1 + h_rungekutt * m2 / 2);
                m3 = g(t + h_rungekutt / 2, x1 + h_rungekutt * k2 / 2, y1 + h_rungekutt * m2 / 2);
                k4 = f(t + h_rungekutt, x1 + h_rungekutt * k3, y1 + h_rungekutt * m3);
                m4 = g(t + h_rungekutt, x1 + h_rungekutt * k3, y1 + h_rungekutt * m3);
                x1 = x1 + h_rungekutt * (k1 + 2 * k2 + 2 * k3 + k4) / 6;
                y1 = y1 + h_rungekutt * (m1 + 2 * m2 + 2 * m3 + m4) / 6;

                if ((i == 100) || (i == 200) || (i == 300) || (i == 400) ||
                    (i == 500) || (i == 600) || (i == 700) ||
                    (i == 800) || (i == 900) || (i == 1000))                // 5
                {
                    Matr[j++, iColumn2Write] = x1 + y1;                     // 2.3
                }
            }
        }
示例#10
0
        public NeuralNetwork(int numberOfInputs, int numberOfHidden, int numberOfOutput, Matrix.Matrix inputs)
        {
            learningRate = 0.1;
            momentumRate = 0.5;

            numberOfHiddenNeurons = numberOfHidden;

            inp = inputs;

            hiddenOutput  = new Matrix.Matrix(numberOfHidden, numberOfInputs);
            outputWeights = new Matrix.Matrix(numberOfOutput, numberOfHidden);

            centre = new Matrix.Matrix(numberOfHidden, numberOfInputs);
            range  = new Matrix.Matrix(numberOfHidden, 1);

            //hiddenOutput.RandomizeMatrix(-1, 1);
            outputWeights.RandomizeMatrix(-5, 5);

            Random rnd = new Random();

            for (int i = 0; i < numberOfHidden; ++i)
            {
                //wypełnianie macierzy c - centrów
                int index = rnd.Next(0, inputs.row);
                for (int j = 0; j < numberOfInputs; ++j)
                {
                    centre.tab[i, j] = inputs.tab[index, j];
                    //Console.WriteLine(centre.tab[i, j]);
                }
                //wypełnianie macierzy r - zasięgu
                range.tab[i, 0] = rnd.NextDouble() * 1;
            }
        }
示例#11
0
        public Matrix.Matrix FeedForward(double[] input_x)
        {
            //Matrix.Matrix x = new Matrix.Matrix(input_x);
            FillHiddenNeurons(input_x);   //fills hiddenNeurons matrix
            //hiddenOutput.DisplayMatrix();

            Matrix.Matrix outputsOutput = outputWeights * hiddenOutput;
            return(outputsOutput);
        }
示例#12
0
 /// <summary>
 /// The constructor.
 /// </summary>
 /// <param name="inputCount">Number of input neurons.</param>
 /// <param name="outputCount">Number of output neurons.</param>
 /// <param name="normalizationType">The normalization type.</param>
 public SelfOrganizingMap(int inputCount, int outputCount,
                          NormalizationType normalizationType)
 {
     this.inputNeuronCount  = inputCount;
     this.outputNeuronCount = outputCount;
     this.outputWeights     = new Matrix.Matrix(this.outputNeuronCount,
                                                this.inputNeuronCount + 1);
     this.output            = new double[this.outputNeuronCount];
     this.normalizationType = normalizationType;
 }
 /// <summary>
 /// The constructor.
 /// </summary>
 /// <param name="inputCount">Number of input neurons.</param>
 /// <param name="outputCount">Number of output neurons.</param>
 /// <param name="normalizationType">The normalization type.</param>
 public SelfOrganizingMap(int inputCount, int outputCount,
          NormalizationType normalizationType)
 {
     this.inputNeuronCount = inputCount;
     this.outputNeuronCount = outputCount;
     this.outputWeights = new Matrix.Matrix(this.outputNeuronCount,
             this.inputNeuronCount + 1);
     this.output = new double[this.outputNeuronCount];
     this.normalizationType = normalizationType;
 }
示例#14
0
 private void mapMatrixLinearry(Matrix.Matrix m)
 {
     for (int i = 0; i < m.row; ++i)
     {
         for (int j = 0; j < m.column; ++j)
         {
             m.tab[i, j] = 1;
         }
     }
 }
示例#15
0
 public void activationFunction(Matrix.Matrix m)
 {
     for (int i = 0; i < m.row; ++i)
     {
         for (int j = 0; j < m.column; ++j)
         {
             m.tab[i, j] = sigmoid(m.tab[i, j]);
         }
     }
 }
示例#16
0
 public void mapMatrix(Matrix.Matrix m)
 {
     for (int i = 0; i < m.row; ++i)
     {
         for (int j = 0; j < m.column; ++j)
         {
             m.tab[i, j] = dSigmoid(m.tab[i, j]);
         }
     }
 }
示例#17
0
        static void Main(string[] args)
        {
            var matrix = new Matrix.Matrix();

            while (true)
            {
                matrix.Render();
                matrix.Update();
                Thread.Sleep(125);
            }
        }
示例#18
0
        public void WorldComplete(Matrix.Matrix <byte> world)
        {
            var img = world.ToBitmap();

            Dispatcher.CurrentDispatcher.Invoke(() =>
            {
                _modelView.Phase           = "Complete";
                _modelView.LastWorldUpdate = _frameNo;
                _modelView.WorldImage      = img;
            });
        }
示例#19
0
文件: Entity.cs 项目: grimm004/shmup
        public RotatedRectangle GetRectangle(Rectangle rectangle, float rotation)
        {
            Matrix.Matrix rotationMatrix = Matrix.Matrix.Rotation(rotation);
            Vector2       centreOffset   = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
            Vector2       centre         = new Vector2(rectangle.X, rectangle.Y) + centreOffset;

            TopLeft     = (rotationMatrix * new Matrix.Matrix2(centre.X - rectangle.Left, centre.Y - rectangle.Top)).ToVector2();
            TopRight    = (rotationMatrix * new Matrix.Matrix2(centre.X - rectangle.Right, centre.Y - rectangle.Top)).ToVector2();
            BottomLeft  = (rotationMatrix * new Matrix.Matrix2(centre.X - rectangle.Left, centre.Y - rectangle.Bottom)).ToVector2();
            BottomRight = (rotationMatrix * new Matrix.Matrix2(centre.X - rectangle.Right, centre.Y - rectangle.Bottom)).ToVector2();
            return(this);
        }
示例#20
0
        /// <summary>
        /// Normalize the specified row in the weight matrix.
        /// </summary>
        /// <param name="matrix">The weight matrix.</param>
        /// <param name="row">The row to normalize.</param>
        protected void NormalizeWeight(Matrix.Matrix matrix, int row)
        {
            double len = MatrixMath.vectorLength(matrix.GetRow(row));

            len = Math.Max(len, VERYSMALL);

            len = 1.0 / len;
            for (int i = 0; i < this.inputNeuronCount; i++)
            {
                matrix[row, i] = matrix[row, i] * len;
            }
            matrix[row, this.inputNeuronCount] = 0;
        }
示例#21
0
文件: Entity.cs 项目: grimm004/shmup
 /// <summary>
 /// Fire a laser
 /// </summary>
 public void FireLaser()
 {
     foreach (Laser laser in Lasers)
     {
         if (laser.canFire)
         {
             Matrix.Matrix mat     = Matrix.Matrix.Rotation(MathHelper.ToRadians(direction));
             Vector2       firePos = position + (mat * new Matrix.Matrix2(19, 0)).ToVector2();
             laser.Fire(firePos, direction);
             break;
         }
     }
 }
示例#22
0
        public void WorldFrame(int frameNo, Matrix.Matrix <byte> frame)
        {
            _frameNo = frameNo;

            var img = frame.ToBitmap();

            Dispatcher.CurrentDispatcher.Invoke(() =>
            {
                _modelView.Phase      = "World";
                _modelView.FrameNo    = frameNo;
                _modelView.FrameImage = img;
            });
        }
示例#23
0
        /// <summary>
        /// Create an input matrix that has enough space to hold the extra synthetic
        /// input.
        /// </summary>
        /// <param name="pattern">The input pattern to create.</param>
        /// <param name="extra">The synthetic input.</param>
        /// <returns>A matrix that contains the input pattern and the synthetic input.</returns>
        protected Matrix.Matrix CreateInputMatrix(double[] pattern,
                                                  double extra)
        {
            Matrix.Matrix result = new Matrix.Matrix(1, pattern.Length + 1);
            for (int i = 0; i < pattern.Length; i++)
            {
                result[0, i] = pattern[i];
            }

            result[0, pattern.Length] = extra;

            return(result);
        }
示例#24
0
        /// <summary>
        /// Force a win, if no neuron won.
        /// </summary>
        protected void ForceWin()
        {
            int best, which = 0;

            Matrix.Matrix outputWeights = this.som.OutputWeights;

            // Loop over all training sets.  Find the training set with
            // the least output.
            double dist = Double.MaxValue;

            for (int tset = 0; tset < this.train.Length; tset++)
            {
                best = this.som.Winner(this.train[tset]);
                double[] output = this.som.Output;

                if (output[best] < dist)
                {
                    dist  = output[best];
                    which = tset;
                }
            }

            NormalizeInput input = new NormalizeInput(this.train[which],
                                                      this.som.NormalizationType);

            best = this.som.Winner(input);
            double[] output2 = this.som.Output;

            dist = Double.MinValue;
            int i = this.outputNeuronCount;

            while ((i--) > 0)
            {
                if (this.won[i] != 0)
                {
                    continue;
                }
                if (output2[i] > dist)
                {
                    dist  = output2[i];
                    which = i;
                }
            }

            for (int j = 0; j < input.InputMatrix.Cols; j++)
            {
                outputWeights[which, j] = input.InputMatrix[0, j];
            }

            NormalizeWeight(outputWeights, which);
        }
示例#25
0
 /// <summary>
 /// Learn from the last error calculation.
 /// </summary>
 /// <param name="learnRate">The learning rate.</param>
 /// <param name="momentum">The momentum.</param>
 public void Learn(double learnRate, double momentum)
 {
     // process the matrix
     if (this.layer.HasMatrix())
     {
         Matrix.Matrix m1 = MatrixMath.Multiply(this.accMatrixDelta,
                                                learnRate);
         Matrix.Matrix m2 = MatrixMath.Multiply(this.matrixDelta, momentum);
         this.matrixDelta       = MatrixMath.Add(m1, m2);
         this.layer.LayerMatrix = (MatrixMath.Add(this.layer.LayerMatrix,
                                                  this.matrixDelta));
         this.accMatrixDelta.Clear();
     }
 }
示例#26
0
        private Matrix.Matrix GetSimilarityMatrix(List<Vector> vectors)
        {
            var similarityMatrix = new Matrix.Matrix(vectors.Count);
            for (int i = 0; i < vectors.Count; i++)
            {
                for (int j = i + 1; j < vectors.Count; j++)
                {
                    double similarity = vectors[i].GetGaussianKernelSimilarityTo(vectors[j]);
                    similarityMatrix[i, j] = similarity;
                }
            }

            return similarityMatrix;
        }
        /// <summary>
        /// Take a simple double array and turn it into a matrix that can be used to
        /// calculate the results of the input array. Also takes into account the
        /// threshold.
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        private Matrix.Matrix CreateInputMatrix(double[] pattern)
        {
            Matrix.Matrix result = new Matrix.Matrix(1, pattern.Length + 1);
            for (int i = 0; i < pattern.Length; i++)
            {
                result[0, i] = pattern[i];
            }

            // add a "fake" first column to the input so that the threshold is
            // always multiplied by one, resulting in it just being added.
            result[0, pattern.Length] = 1;

            return(result);
        }
示例#28
0
        public void WindowUpdate(int frameNo, Matrix.Matrix <byte> frame, Tuple <Tuple <int, int>, Tuple <int, int> > region, bool final)
        {
            Dispatcher.CurrentDispatcher.Invoke(() =>
            {
                _modelView.Phase      = "Window";
                _modelView.FrameNo    = frameNo;
                _modelView.FrameImage = frame.ToBitmap();

                var((top, left), (bottom, right)) = region;
                _modelView.WindowTop    = top;
                _modelView.WindowLeft   = left;
                _modelView.WindowHeight = bottom - top;
                _modelView.WindowWidth  = right - left;
            });
        }
示例#29
0
        public static BitmapSource ToBitmap(this Matrix.Matrix <byte> raw)
        {
            var img = Image.c64ToRgb(raw);

            unsafe
            {
                fixed(int *buffer = img.Rep)
                {
                    var bitmap = BitmapSource.Create(img.Width, img.Height, 96, 96, PixelFormats.Bgr32, null, (IntPtr)buffer, 4 * img.Width * img.Height, 4 * img.Width);

                    bitmap.Freeze();

                    return(bitmap);
                }
            }
        }
示例#30
0
        public NeuroLayer(int id, int countIn, int countOut, LayerRole role)
        {
            sync    = new object();
            Role    = role;
            LayerID = id;

            nucleus = new Dictionary <int, NeuroItem>();
            input   = new Dictionary <int, NeuroSignal>();
            output  = new Dictionary <int, NeuroSignal>();

            weights =
                Role == LayerRole.Input ? Matrix.Matrix.One(countIn, countOut) :
                Role == LayerRole.Output ? Matrix.Matrix.Half(countIn, countOut) :
                Matrix.Matrix.Half(countIn, countOut);

            ActivationFooType activation =
                Role == LayerRole.Input ? ActivationFooType.Identity : ActivationFooType.Sigmoid;

            #region -> TMP
            if (id == 2)
            {
                weights[0, 0] = 0.9; weights[0, 1] = 0.3; weights[0, 2] = 0.4;
                weights[1, 0] = 0.2; weights[1, 1] = 0.8; weights[1, 2] = 0.2;
                weights[2, 0] = 0.1; weights[2, 1] = 0.5; weights[2, 2] = 0.6;
            }
            if (id == 3)
            {
                weights[0, 0] = 0.3; weights[0, 1] = 0.7; weights[0, 2] = 0.5;
                weights[1, 0] = 0.6; weights[1, 1] = 0.5; weights[1, 2] = 0.2;
                weights[2, 0] = 0.8; weights[2, 1] = 0.1; weights[2, 2] = 0.9;
            }
            #endregion

            for (int num = 0; num < countIn; num++)
            {
                input.Add(num, new NeuroSignal(num));
            }

            for (int num = 0; num < countOut; num++)
            {
                NeuroItem ni =
                    new NeuroItem(num, TransmitInput, activation);

                nucleus.Add(num, ni);
                output.Add(num, new NeuroSignal(num));
            }
        }
示例#31
0
        public NeuralNetwork(int numberOfInputs, int numberOfHidden, int numberOfOutput, bool bias, Matrix.Matrix inputs)
        {
            learningRate = 0.001;
            momentumRate = 0.002;
            useBias      = bias;

            numberOfHiddenNeurons = numberOfHidden;

            inp = inputs;

            hiddenOutput  = new Matrix.Matrix(numberOfHidden, 1);
            outputWeights = new Matrix.Matrix(numberOfOutput, numberOfHidden);

            range = new Matrix.Matrix(numberOfHidden, 1);

            //hiddenOutput.RandomizeMatrix(-1, 1);
            outputWeights.RandomizeMatrix(-1, 1);

            Random rnd = new Random();

            List <Kmeans.Point> Data = new List <Kmeans.Point>();

            for (int i = 0; i < inputs.row; i++)
            {
                Data.Add(new Kmeans.Point(inputs.tab[i, 0], inputs.tab[i, 1], inputs.tab[i, 2], inputs.tab[i, 3]));
            }

            var km = new Kmeans.KMeans(numberOfHidden, Data, rnd);

            km.Train();
            Console.WriteLine("centroids done");

            centre = km.Centroids;

            for (int i = 0; i < numberOfHidden; ++i)
            {
                //wypełnianie macierzy r - zasięgu
                range.tab[i, 0] =
                    setRange(centre[i]) * beta;
            }
            momentumMatrixOutput     = new Matrix.Matrix(numberOfOutput, numberOfHidden);
            biasOutput               = new Matrix.Matrix(numberOfOutput, 1);
            momentumMatrixOutputBias = new Matrix.Matrix(numberOfOutput, 1);

            biasOutput.RandomizeMatrix(-1, 1);
        }
示例#32
0
        public void LoadFromFile()
        {
            var fileStream_train = new FileStream(@"..\..\classification_files\classification_train.txt", FileMode.Open, FileAccess.Read);
            var fileStream_test  = new FileStream(@"..\..\classification_files\classification_test.txt", FileMode.Open, FileAccess.Read);

            //var fileStream = new FileStream(@"D:\input2.txt", FileMode.Open, FileAccess.Read);

            int    i = 0;
            string line;

            inputs = new Matrix.Matrix(90, 4);

            using (var streamReader = new StreamReader(fileStream_train, Encoding.UTF8))
            {
                while ((line = streamReader.ReadLine()) != null)
                {
                    classification_train[i][0] = Convert.ToDouble(line.Split(' ')[0]);
                    classification_train[i][1] = Convert.ToDouble(line.Split(' ')[1]);
                    classification_train[i][2] = Convert.ToDouble(line.Split(' ')[2]);
                    classification_train[i][3] = Convert.ToDouble(line.Split(' ')[3]);
                    classification_train[i][4] = Convert.ToDouble(line.Split(' ')[4]);

                    inputs.tab[i, 0] = classification_train[i][0];
                    inputs.tab[i, 1] = classification_train[i][1];
                    inputs.tab[i, 2] = classification_train[i][2];
                    inputs.tab[i, 3] = classification_train[i][3];
                    i++;
                }
            }

            i = 0;

            using (var streamReader = new StreamReader(fileStream_test, Encoding.UTF8))
            {
                while ((line = streamReader.ReadLine()) != null)
                {
                    classification_test[i][0] = Convert.ToDouble(line.Split(' ')[0]);
                    classification_test[i][1] = Convert.ToDouble(line.Split(' ')[1]);
                    classification_test[i][2] = Convert.ToDouble(line.Split(' ')[2]);
                    classification_test[i][3] = Convert.ToDouble(line.Split(' ')[3]);
                    classification_test[i][4] = Convert.ToDouble(line.Split(' ')[4]);
                    i++;
                }
            }
        }
        static void Main(string[] args)
        {
            Matrix.Matrix<int> matr = new Matrix.Matrix<int>(2, 4);
            Console.WriteLine("Matrix");
            Initialize(matr);
            
            matr = new Matrix.SquareMatrix<int>(4);
            Console.WriteLine("Square matrix");
            Initialize(matr);

            matr = new Matrix.DiagonalMatrix<int>(5);
            Console.WriteLine("Diagonal matrix");
            Initialize(matr);

            matr = new Matrix.SymmetricMatrix<int>(5);
            Console.WriteLine("Symmetric matrix");
            Initialize(matr);
            Console.ReadKey();
        }
示例#34
0
        /// <summary>
        /// Create an input matrix that has enough space to hold the extra synthetic
        /// input.
        /// </summary>
        /// <param name="pattern">The input pattern to create.</param>
        /// <param name="extra">The synthetic input.</param>
        /// <returns>A matrix that contains the input pattern and the synthetic input.</returns>
        protected Matrix.Matrix CreateInputMatrix(double[] pattern,
                 double extra)
        {
            Matrix.Matrix result = new Matrix.Matrix(1, pattern.Length + 1);
            for (int i = 0; i < pattern.Length; i++)
            {
                result[0, i] = pattern[i];
            }

            result[0, pattern.Length] = extra;

            return result;
        }
示例#35
0
 /// <summary>
 /// Normalize an input array into a matrix. The resulting matrix will have
 /// one extra column that will be occupied by the synthetic input.
 /// </summary>
 /// <param name="input">The input array to be normalized.</param>
 /// <param name="type">What type of normalization to use.</param>
 public NormalizeInput(double[] input, NormalizationType type)
 {
     this.type = type;
     CalculateFactors(input);
     this.inputMatrix = this.CreateInputMatrix(input, this.synth);
 }
示例#36
0
        /// <summary>
        /// Construct the trainer for a self organizing map.
        /// </summary>
        /// <param name="som">The self organizing map.</param>
        /// <param name="train">The training method.</param>
        /// <param name="learnMethod">The learning method.</param>
        /// <param name="learnRate">The learning rate.</param>
        public TrainSelfOrganizingMap(SelfOrganizingMap som,
                 double[][] train, LearningMethod learnMethod, double learnRate)
        {
            this.som = som;
            this.train = train;
            this.totalError = 1.0;
            this.learnMethod = learnMethod;
            this.learnRate = learnRate;

            this.outputNeuronCount = som.OutputNeuronCount;
            this.inputNeuronCount = som.InputNeuronCount;

            this.totalError = 1.0;

            for (int tset = 0; tset < train.Length; tset++)
            {
                Matrix.Matrix dptr = Matrix.Matrix.CreateColumnMatrix(train[tset]);
                if (MatrixMath.vectorLength(dptr) < VERYSMALL)
                {
                    throw (new System.Exception(
                            "Multiplicative normalization has null training case"));
                }

            }

            this.bestnet = new SelfOrganizingMap(this.inputNeuronCount,
                    this.outputNeuronCount, this.som.NormalizationType);

            this.won = new int[this.outputNeuronCount];
            this.correc = new Matrix.Matrix(this.outputNeuronCount,
                    this.inputNeuronCount + 1);
            if (this.learnMethod == LearningMethod.ADDITIVE)
            {
                this.work = new Matrix.Matrix(1, this.inputNeuronCount + 1);
            }
            else
            {
                this.work = null;
            }

            Initialize();
            this.bestError = Double.MaxValue;
        }
        /// <summary>
        /// Learn from the last error calculation.
        /// </summary>
        /// <param name="learnRate">The learning rate.</param>
        /// <param name="momentum">The momentum.</param>
        public void Learn(double learnRate, double momentum)
        {
            // process the matrix
            if (this.layer.HasMatrix())
            {

                Matrix.Matrix m1 = MatrixMath.Multiply(this.accMatrixDelta,
                       learnRate);
                Matrix.Matrix m2 = MatrixMath.Multiply(this.matrixDelta, momentum);
                this.matrixDelta = MatrixMath.Add(m1, m2);
                this.layer.LayerMatrix = (MatrixMath.Add(this.layer.LayerMatrix,
                        this.matrixDelta));
                this.accMatrixDelta.Clear();
            }
        }
示例#38
0
        /// <summary>
        /// Take a simple double array and turn it into a matrix that can be used to
        /// calculate the results of the input array. Also takes into account the
        /// threshold.
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        private Matrix.Matrix CreateInputMatrix(double[] pattern)
        {
            Matrix.Matrix result = new Matrix.Matrix(1, pattern.Length + 1);
            for (int i = 0; i < pattern.Length; i++)
            {
                result[0, i] = pattern[i];
            }

            // add a "fake" first column to the input so that the threshold is
            // always multiplied by one, resulting in it just being added.
            result[0, pattern.Length] = 1;

            return result;
        }
        /// <summary>
        /// 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. 
        /// </summary>
        /// <param name="pattern">The pattern to train on.</param>
        public void Train(bool[] pattern)
        {
            if (pattern.Length != this.weightMatrix.Rows)
            {
                throw new NeuralNetworkError("Can't train a pattern of size "
                        + pattern.Length + " on a hopfield network of size "
                        + this.weightMatrix.Rows);
            }

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

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

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

            // now add the calculated matrix, for this pattern, to the
            // existing weight matrix.
            this.weightMatrix = MatrixMath.Add(this.weightMatrix, m4);
        }
 /// <summary>
 /// Construct a Hopfield neural network of the specified size.
 /// </summary>
 /// <param name="size">The number of neurons in the network.</param>
 public HopfieldNetwork(int size)
 {
     this.weightMatrix = new Matrix.Matrix(size, size);
 }