예제 #1
0
        public void multiplySelf(Matrix_Math n)
        {
            //dot product
            if (this.cols != n.rows)
            {
                Console.WriteLine("Columns of A must match rows of B");
            }
            Matrix_Math temp = new Matrix_Math(this.rows, n.cols);

            for (int i = 0; i < rows; i++)
            {
                for (int l = 0; l < cols; l++)
                {
                    double sum = 0;
                    for (int j = 0; j < cols; j++)
                    {
                        sum += this.data[i, j] * n.data[j, l];
                    }
                    temp.data[i, l] = sum;
                }
            }
            this.data = temp.data;
            this.cols = temp.cols;
            this.rows = temp.rows;
        }
예제 #2
0
        public static void PrintMatrix(Matrix_Math m)
        {
            string s = "[";

            for (int i = 0; i < m.rows; i++)
            {
                for (int l = 0; l < m.cols; l++)
                {
                    if (m.data[i, l] >= 0)
                    {
                        s += ' ';
                    }
                    s += m.data[i, l];
                    if (l < m.cols - 1)
                    {
                        s += " , ";
                    }
                }
                if (i < m.rows - 1)
                {
                    s += "\n ";
                }
            }
            Console.WriteLine(s + "]\n");
        }
        public void train(double[,] inputs_array, double[,] targets_array)
        {
            //converts the array into a useable matrices object
            Matrix_Math inputs  = new Matrix_Math(inputs_array);
            Matrix_Math targets = new Matrix_Math(targets_array);


            //Multiplie the input-hidden weight matrix with the inputs to create the input of the hidden layer
            Matrix_Math hidden_inputs = Matrix_Math.multiply(this.Winput_hidden, inputs);

            //Sigmoids the input of the hidden layer to create the output of the hidden layer
            Matrix_Math hidden_outputs = Matrix_Math.Map(hidden_inputs, sigmoid);

            //Multiplie the hidden-output weight matrix with the output of the hidden layer to create the input of the final layer
            Matrix_Math final_inputs = Matrix_Math.multiply(this.Whidden_output, hidden_outputs);

            //Sigmoids the input of the hidden layer to create the output of the final layer
            Matrix_Math final_outputs = Matrix_Math.Map(final_inputs, sigmoid);

            //Calculates the error for the output layer - (target - actual)
            Matrix_Math output_errors = Matrix_Math.Subtract(targets, final_outputs);

            //Calculates the errors of the hidden layer by multiplying the transposed weights of the hidden - output layer by the errors of the output layer
            Matrix_Math hidden_errors = Matrix_Math.multiply(Matrix_Math.Transpose(this.Whidden_output), output_errors);

            //OOF, this multiples the learning rate by the dot product of the multipication of the output erros, the output of the final layer and (1 - the output of the final layer) and the transposed output of the hidden layer
            this.Whidden_output.AddSelf(Matrix_Math.multiply(Matrix_Math.multiply(Matrix_Math.MultiplyHadamard(Matrix_Math.MultiplyHadamard(output_errors, final_outputs), Matrix_Math.Map(final_outputs, dsigmoid)), Matrix_Math.Transpose(hidden_outputs)), this.lr));

            //OOF, this multiples the learning rate by the dot product of the multipication of the hidden erros, the output of the hiddedn layer and (1 - the output of the hidden layer) and the transposed output of the input layer
            this.Winput_hidden.AddSelf(Matrix_Math.multiply(Matrix_Math.multiply(Matrix_Math.MultiplyHadamard(Matrix_Math.MultiplyHadamard(hidden_errors, hidden_outputs), Matrix_Math.Map(hidden_outputs, dsigmoid)), Matrix_Math.Transpose(inputs)), this.lr));
        }
예제 #4
0
        public void TransposeSelf()
        {
            Matrix_Math temp = Transpose(this);

            this.cols = temp.cols;
            this.rows = temp.rows;
            this.data = temp.data;
        }
예제 #5
0
 public static Matrix_Math Randomise(Matrix_Math m, int n)
 {
     for (int i = 0; i < m.rows; i++)
     {
         for (int l = 0; l < m.cols; l++)
         {
             m.data[i, l] = (double)r.Next(-n * 1000000, n * 1000000 + 1) / 10000000;
         }
     }
     return(m);
 }
예제 #6
0
        public void AddSelf(double n)
        {
            Matrix_Math Result = new Matrix_Math(this.rows, this.cols);

            for (int i = 0; i < this.rows; i++)
            {
                for (int l = 0; l < this.cols; l++)
                {
                    this.data[i, l] += n;
                }
            }
        }
예제 #7
0
        public void SubtractSelf(Matrix_Math m2)
        {
            Matrix_Math Result = new Matrix_Math(this.rows, this.cols);

            for (int i = 0; i < this.rows; i++)
            {
                for (int l = 0; l < this.cols; l++)
                {
                    this.data[i, l] -= m2.data[i, l];
                }
            }
        }
예제 #8
0
        public void multiplySelfHadamard(Matrix_Math n)
        {
            Matrix_Math Result = new Matrix_Math(this.rows, this.cols);

            for (int i = 0; i < this.rows; i++)
            {
                for (int l = 0; l < this.cols; l++)
                {
                    this.data[i, l] *= n.data[i, l];
                }
            }
        }
예제 #9
0
        public void multiplySelf(double n)
        {
            Matrix_Math temp = new Matrix_Math(this.rows, this.cols);

            for (int i = 0; i < rows; i++)
            {
                for (int l = 0; l < cols; l++)
                {
                    data[i, l] *= n;
                }
            }
        }
예제 #10
0
        public void MapSelf(MyFunction f)
        {
            Matrix_Math temp = new Matrix_Math(this.rows, this.cols);

            for (int i = 0; i < rows; i++)
            {
                for (int l = 0; l < cols; l++)
                {
                    data[i, l] = f(data[i, l], rows, cols);
                }
            }
        }
예제 #11
0
 public Matrix_Math(Matrix_Math n)
 {
     this.cols = n.cols;
     this.rows = n.rows;
     for (int i = 0; i < rows; i++)
     {
         for (int l = 0; l < cols; l++)
         {
             this.data[i, l] = n.data[i, l];
         }
     }
 }
예제 #12
0
        public static Matrix_Math Transpose(Matrix_Math m1)
        {
            Matrix_Math Result = new Matrix_Math(m1.cols, m1.rows);

            for (int i = 0; i < m1.rows; i++)
            {
                for (int j = 0; j < m1.cols; j++)
                {
                    Result.data[j, i] = m1.data[i, j];
                }
            }
            return(Result);
        }
예제 #13
0
        public static Matrix_Math Subtract(Matrix_Math m1, Matrix_Math m2)
        {
            Matrix_Math Result = new Matrix_Math(m1.rows, m1.cols);

            for (int i = 0; i < m1.rows; i++)
            {
                for (int l = 0; l < m1.cols; l++)
                {
                    Result.data[i, l] = m1.data[i, l] - m2.data[i, l];
                }
            }
            return(Result);
        }
예제 #14
0
        public static Matrix_Math Subtract(Matrix_Math m, double n)
        {
            Matrix_Math Result = new Matrix_Math(m.rows, m.cols);

            for (int i = 0; i < m.rows; i++)
            {
                for (int l = 0; l < m.cols; l++)
                {
                    Result.data[i, l] = m.data[i, l] - n;
                }
            }
            return(Result);
        }
예제 #15
0
        public static Matrix_Math MultiplyHadamard(Matrix_Math m1, Matrix_Math m2)
        {
            Matrix_Math Result = new Matrix_Math(m1.rows, m1.cols);

            for (int i = 0; i < m1.rows; i++)
            {
                for (int l = 0; l < m1.cols; l++)
                {
                    Result.data[i, l] = m1.data[i, l] * m2.data[i, l];
                }
            }
            return(Result);
        }
예제 #16
0
        public static Matrix_Math multiply(Matrix_Math m1, double n)
        {
            Matrix_Math Result = new Matrix_Math(m1.rows, m1.cols);

            for (int i = 0; i < m1.rows; i++)
            {
                for (int l = 0; l < m1.cols; l++)
                {
                    Result.data[i, l] = m1.data[i, l] * n;
                }
            }
            return(Result);
        }
예제 #17
0
        public static Matrix_Math Map(Matrix_Math m1, MyFunction f)
        {
            Matrix_Math Result = new Matrix_Math(m1.rows, m1.cols);

            for (int i = 0; i < m1.rows; i++)
            {
                for (int l = 0; l < m1.cols; l++)
                {
                    Result.data[i, l] = f(m1.data[i, l], m1.rows, m1.cols);
                }
            }
            return(Result);
        }
        public Matrix_Math query(double[,] inputs_array)
        {
            //converts the array into a useable matrix object
            Matrix_Math inputs = new Matrix_Math(inputs_array);
            //Multiplie the input-hidden weight matrix with the inputs to create the input of the hidden layer
            Matrix_Math hidden_inputs = Matrix_Math.multiply(this.Winput_hidden, inputs);
            //Sigmoids the input of the hidden layer to create the output of the hidden layer
            Matrix_Math hidden_outputs = Matrix_Math.Map(hidden_inputs, sigmoid);
            //Multiplie the hidden-output weight matrix with the output of the hidden layer to create the input of the final layer
            Matrix_Math final_inputs = Matrix_Math.multiply(this.Whidden_output, hidden_outputs);
            //Sigmoids the input of the hidden layer to create the output of the final layer
            Matrix_Math final_outputs = Matrix_Math.Map(final_inputs, sigmoid);

            return(final_outputs);
        }
        public Neural_Network(double lr, int input, int hidden, int output)
        {
            // Defines the basic information of a neural network: number of input nodes, hidden nodes, output nodes and learning rate
            this.NumOfinput  = input;
            this.NumOfhidden = hidden;
            this.NumOfoutput = output;
            this.lr          = lr;
            //creates a matrix for the weights between the input and hidden in the size of hidden*input
            this.Winput_hidden = new Matrix_Math(NumOfhidden, NumOfinput);
            //creates a matrix for the weights between the hidden and output in the size of output*hidden
            this.Whidden_output = new Matrix_Math(NumOfoutput, NumOfhidden);

            //randomises the matirxes values from -0.5 to 0.5
            this.Winput_hidden.RandomiseSelf(5);
            this.Whidden_output.RandomiseSelf(5);

            //this.input_hidden.PrintSelf();
            //this.hidden_output.PrintSelf();
        }
예제 #20
0
        public static Matrix_Math multiply(Matrix_Math m1, Matrix_Math m2)
        {
            //dot product
            if (m1.cols != m2.rows)
            {
                Console.WriteLine("Columns of A must match rows of B");
                return(null);
            }
            Matrix_Math temp = new Matrix_Math(m1.rows, m2.cols);

            for (int i = 0; i < m1.rows; i++)
            {
                for (int l = 0; l < m2.cols; l++)
                {
                    double sum = 0;
                    for (int j = 0; j < m1.cols; j++)
                    {
                        sum += m1.data[i, j] * m2.data[j, l];
                    }
                    temp.data[i, l] = sum;
                }
            }
            return(temp);
        }