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 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; }
public void train(double[,] train_data, double[,] answer) { Matrix_Math Output = new Matrix_Math(FeedForward(new Matrix_Math(train_data))); // passes the training data threw the feed forward and puts the output into a matrix Matrix_Math errors = Matrix_Math.Subtract(new Matrix_Math(answer), Output); //creates a matrix of error by converting the answers to matrix and subtracting the output matrix from it //double herror1 = weights_Hidden_Output.data[0, 0] / (weights_Hidden_Output.data[0, 0] + weights_Hidden_Output.data[0, 1])*error; //the error of the first neuron of hidden layer //double herror2 = weights_Hidden_Output.data[0, 1] / (weights_Hidden_Output.data[0, 0] + weights_Hidden_Output.data[0, 1])*error;//the error of the second neuron of hidden layer Matrix_Math hidden_errors = new Matrix_Math(Num_hidden, 1); double[] sumOfWeights = new double[Num_output]; for (int l = 0; l < Num_output; l++) { sumOfWeights[l] = 0; for (int i = 0; i < Num_hidden; i++) { sumOfWeights[l] += weights_Hidden_Output.data[l, i]; //creates the sum of both weights of each output neuron } } //Output.PrintSelf(); //new Matrix_Math(answer).PrintSelf(); //errors.PrintSelf(); for (int l = 0; l < Num_output; l++) { for (int i = 0; i < Num_hidden; i++) { hidden_errors.data[i, 0] += weights_Hidden_Output.data[l, i] / sumOfWeights[l] * errors.data[l, 0];//creates the error for each hidden layer neuron } } // hidden_errors.PrintSelf(); // Keep the prints for testing }
public void TransposeSelf() { Matrix_Math temp = Transpose(this); this.cols = temp.cols; this.rows = temp.rows; this.data = temp.data; }
static void Main(string[] args) { Neural3Deep nn = new Neural3Deep(2, 2, 3); double[,] inp = { { 1, 0.3 } }; double[,] answers = { { 0.5 }, { 0.4 }, { 0.1 } }; Matrix_Math input = new Matrix_Math(inp); input.TransposeSelf(); nn.train(input.data, answers); }
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 * 100000, n * 100000 + 1) / 100000; } } return(m); }
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]; } } }
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); } } }
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; } } }
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]; } } }
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; } } }
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); }
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); }
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); }
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); }
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 Neural3Deep(int Num_input, int Num_hidden, int Num_output) { this.Num_hidden = Num_hidden; this.Num_input = Num_input; this.Num_output = Num_output; this.weights_Input_Hidden = new Matrix_Math(Num_hidden, Num_input); this.weights_Hidden_Output = new Matrix_Math(Num_output, Num_hidden); this.weights_Hidden_Output.RandomiseSelf(1); this.weights_Input_Hidden.RandomiseSelf(1); bias_hidden = new Matrix_Math(Num_hidden, 1); bias_hidden.RandomiseSelf(1); bias_output = new Matrix_Math(Num_output, 1); bias_output.RandomiseSelf(1); }
public double[,] FeedForward(Matrix_Math input) { Matrix_Math hidden = Matrix_Math.multiply(this.weights_Input_Hidden, input); //multiples the weights by the input hidden.AddSelf(this.bias_hidden); // adds the bias hidden.MapSelf(sigmoid); // sigmoids everything - that's now the output that is transfered to the next layer Matrix_Math output = Matrix_Math.multiply(weights_Hidden_Output, hidden); //multiples the weights by the input(that is the preivoius output output.AddSelf(bias_output); //adds bias output.MapSelf(sigmoid); //sigmoids everything //weights_Input_Hidden.PrintSelf(); //input.PrintSelf(); //Console.WriteLine(); //hidden.PrintSelf(); //weights_Hidden_Output.PrintSelf(); //Console.WriteLine(); //output.PrintSelf(); return(output.data);//gives out the output }
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); }