From f836d7c51a3f5c1d5407bc8b3162817c47a376e5 Mon Sep 17 00:00:00 2001 From: JackinSac1 <87680958+JackinSac@users.noreply.github.com> Date: Tue, 20 Dec 2022 19:02:49 -0800 Subject: [PATCH 1/9] Update NeuralNetwork.java --- NeuralNetwork.java | 148 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 121 insertions(+), 27 deletions(-) diff --git a/NeuralNetwork.java b/NeuralNetwork.java index f52704a..b2a48a4 100644 --- a/NeuralNetwork.java +++ b/NeuralNetwork.java @@ -1,9 +1,14 @@ - +import java.io.FileNotFoundException; import java.util.ArrayList; +import java.io.PrintWriter; +import java.util.Scanner; +import java.util.Arrays; import java.util.List; +import java.io.File; public class NeuralNetwork { - + private static BasicUtils U = new BasicUtils(); + Matrix weights_ih, weights_ho, bias_h, bias_o; double l_rate = 0.01; boolean useMultiThreading = false; @@ -14,7 +19,6 @@ public NeuralNetwork(int i, int h, int o) { bias_h = new Matrix(h, 1); bias_o = new Matrix(o, 1); - } public NeuralNetwork(int i, int h, int o, boolean useMultiThreading) { @@ -25,7 +29,6 @@ public NeuralNetwork(int i, int h, int o, boolean useMultiThreading) { bias_o = new Matrix(o, 1); this.useMultiThreading = useMultiThreading; - } public NeuralNetwork(int i, int h, int o, double l_rate) { @@ -36,7 +39,6 @@ public NeuralNetwork(int i, int h, int o, double l_rate) { bias_o = new Matrix(o, 1); this.l_rate = l_rate; - } public NeuralNetwork(int i, int h, int o, double l_rate, boolean useMultiThreading) { @@ -48,7 +50,6 @@ public NeuralNetwork(int i, int h, int o, double l_rate, boolean useMultiThreadi this.l_rate = l_rate; this.useMultiThreading = useMultiThreading; - } public List predict(double[] X) { @@ -75,7 +76,7 @@ public void fit(double[][] X, double[][] Y, int epochs, int verbose) { switch (verbose) { case 0: { - System.out.println("Staring training with " + epochs + " epochs"); + U.println("Staring training with " + epochs + " epochs"); long start = System.currentTimeMillis(); for (int i = 0; i < epochs; i++) { int sampleN = (int) (Math.random() * X.length); @@ -83,27 +84,26 @@ public void fit(double[][] X, double[][] Y, int epochs, int verbose) { } long end = System.currentTimeMillis(); long elapsedTime = end - start; - System.out.println("Training took : " + (elapsedTime / 1000) + "s"); + U.println("Training took : " + (elapsedTime / 1000) + "s\n"); break; } case 1: { - System.out.println("Staring training with " + epochs + " epochs"); + U.println("Staring training with " + epochs + " epochs"); long start = System.currentTimeMillis(); for (int i = 0; i < epochs; i++) { - System.out.println("Epoch: " + (i + 1)); + U.println("Epoch: " + (i + 1)); int sampleN = (int) (Math.random() * X.length); this.train(X[sampleN], Y[sampleN], true); } long end = System.currentTimeMillis(); long elapsedTime = end - start; - System.out.println("Training took : " + (elapsedTime / 1000) + "s"); + U.println("Training took : " + (elapsedTime / 1000) + "s"); break; } } - } public void train(double[] X, double[] Y, Boolean showLoss) { @@ -144,7 +144,6 @@ public void train(double[] X, double[] Y, Boolean showLoss) { weights_ih.add(wih_delta); bias_h.add(h_gradient); - } private void printLoss(Matrix error) { @@ -156,12 +155,54 @@ private void printLoss(Matrix error) { } } - System.out.print("Average Error: " + avg + "\n"); + U.print("Average Error: " + avg + "\n"); + } + + public void testData(double[][] testing_in, double[][] testing_out) { + int i = 0; + List output; + for (double d[] : testing_in) { + output = predict(d); + U.printf("Q: %s\nIn: %s\nOut: %s\nPrediction: %s\n",i,U.arrStr(d),U.arrStr(testing_out[i++]),output.toString()); + } + } + + public double[][] getFileData(String fileName) { + List rawData = U.readFile(fileName); + List filteredData = new ArrayList(); + + for (String n : rawData) { + String[] s = n.split(","); + List temp = new ArrayList(); + for (String i : s) { + temp.add(Double.parseDouble(i)); + } + filteredData.add((ArrayList) temp); + } + + double[][] dataArr = nestedListToNestedArr(filteredData); + + return dataArr; + } + + private static double[][] nestedListToNestedArr(List dataList) { + double[][] dataArr = new double[dataList.size()][dataList.get(0).size()]; + int y = 0; + for (List n : dataList) { + int x = 0; + for (Object i : n) { + dataArr[y][x] = ((Double) i); + x++; + } + y++; + } + return dataArr; } - } class Matrix { + private static BasicUtils U = new BasicUtils(); + double[][] data; int rows, cols; @@ -179,9 +220,9 @@ public Matrix(int rows, int cols) { public void print() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { - System.out.print(this.data[i][j] + " "); + U.print(this.data[i][j] + " "); } - System.out.println(); + U.println(""); } } @@ -196,7 +237,7 @@ public void add(int scaler) { public void add(Matrix m) { if (cols != m.cols || rows != m.rows) { - System.out.println("Shape Mismatch"); + U.println("Shape Mismatch"); return; } @@ -212,7 +253,6 @@ public static Matrix fromArray(double[] x) { for (int i = 0; i < x.length; i++) temp.data[i][0] = x[i]; return temp; - } public List toArray() { @@ -285,7 +325,6 @@ public void multiply(double a) { this.data[i][j] *= a; } } - } public void sigmoid() { @@ -293,7 +332,6 @@ public void sigmoid() { for (int j = 0; j < cols; j++) this.data[i][j] = 1 / (1 + Math.exp(-this.data[i][j])); } - } public Matrix dsigmoid() { @@ -303,7 +341,6 @@ public Matrix dsigmoid() { temp.data[i][j] = this.data[i][j] * (1 - this.data[i][j]); } return temp; - } } @@ -357,7 +394,6 @@ public void run() { } class RowMultiplyWorker implements Runnable { - private final Matrix result; private Matrix matrix1; private Matrix matrix2; @@ -377,11 +413,69 @@ public void run() { result.data[row][i] = 0; for (int j = 0; j < matrix1.data[row].length; j++) { result.data[row][i] += matrix1.data[row][j] * matrix2.data[j][i]; - } - } - } +} -} \ No newline at end of file +final class BasicUtils { + private static PrintWriter writer = new PrintWriter(System.out); + + public static List readFile(String fileName) { + List out = new ArrayList(); + try { + File file = new File(fileName); + Scanner reader = new Scanner(file); + while (reader.hasNextLine()) { + String data = reader.nextLine(); + out.add(data); + } + reader.close(); + } catch (FileNotFoundException e) { + println("An error occurred."); + e.printStackTrace(); + } + + return out; + } + + public static String arrStr(int[] arr) { + return Arrays.toString(arr); + } + + public static String arrStr(double[] arr) { + return Arrays.toString(arr); + } + + public static String arrStr(boolean[] arr) { + return Arrays.toString(arr); + } + + public static String arrStr(Object[] arr) { + return Arrays.deepToString(arr); + } + + public static void print(Object str) { + writer.write(String.valueOf(str)); + writer.flush(); + } + + public static void println(Object str) { + print(str+"\n"); + } + + public static void printf(Object mainText, Object... x) { + String toPrint = ""; + + String[] y = String.valueOf(mainText).split("%s"); + int i = 0; + for (String n : y) { + toPrint += n; + if (i Date: Tue, 20 Dec 2022 19:04:01 -0800 Subject: [PATCH 2/9] Create NeuralNetwork.java --- Neural_Network/NeuralNetwork.java | 481 ++++++++++++++++++++++++++++++ 1 file changed, 481 insertions(+) create mode 100644 Neural_Network/NeuralNetwork.java diff --git a/Neural_Network/NeuralNetwork.java b/Neural_Network/NeuralNetwork.java new file mode 100644 index 0000000..b2a48a4 --- /dev/null +++ b/Neural_Network/NeuralNetwork.java @@ -0,0 +1,481 @@ +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.io.PrintWriter; +import java.util.Scanner; +import java.util.Arrays; +import java.util.List; +import java.io.File; + +public class NeuralNetwork { + private static BasicUtils U = new BasicUtils(); + + Matrix weights_ih, weights_ho, bias_h, bias_o; + double l_rate = 0.01; + boolean useMultiThreading = false; + + public NeuralNetwork(int i, int h, int o) { + weights_ih = new Matrix(h, i); + weights_ho = new Matrix(o, h); + + bias_h = new Matrix(h, 1); + bias_o = new Matrix(o, 1); + } + + public NeuralNetwork(int i, int h, int o, boolean useMultiThreading) { + weights_ih = new Matrix(h, i); + weights_ho = new Matrix(o, h); + + bias_h = new Matrix(h, 1); + bias_o = new Matrix(o, 1); + + this.useMultiThreading = useMultiThreading; + } + + public NeuralNetwork(int i, int h, int o, double l_rate) { + weights_ih = new Matrix(h, i); + weights_ho = new Matrix(o, h); + + bias_h = new Matrix(h, 1); + bias_o = new Matrix(o, 1); + + this.l_rate = l_rate; + } + + public NeuralNetwork(int i, int h, int o, double l_rate, boolean useMultiThreading) { + weights_ih = new Matrix(h, i); + weights_ho = new Matrix(o, h); + + bias_h = new Matrix(h, 1); + bias_o = new Matrix(o, 1); + + this.l_rate = l_rate; + this.useMultiThreading = useMultiThreading; + } + + public List predict(double[] X) { + Matrix input = Matrix.fromArray(X); + Matrix hidden = Matrix.multiply(weights_ih, input, useMultiThreading); + hidden.add(bias_h); + hidden.sigmoid(); + + Matrix output = Matrix.multiply(weights_ho, hidden, useMultiThreading); + output.add(bias_o); + output.sigmoid(); + + return output.toArray(); + } + + public void fit(double[][] X, double[][] Y, int epochs) { + for (int i = 0; i < epochs; i++) { + int sampleN = (int) (Math.random() * X.length); + this.train(X[sampleN], Y[sampleN], false); + } + } + + public void fit(double[][] X, double[][] Y, int epochs, int verbose) { + switch (verbose) { + + case 0: { + U.println("Staring training with " + epochs + " epochs"); + long start = System.currentTimeMillis(); + for (int i = 0; i < epochs; i++) { + int sampleN = (int) (Math.random() * X.length); + this.train(X[sampleN], Y[sampleN], i + 1 == epochs); + } + long end = System.currentTimeMillis(); + long elapsedTime = end - start; + U.println("Training took : " + (elapsedTime / 1000) + "s\n"); + + break; + } + + case 1: { + U.println("Staring training with " + epochs + " epochs"); + long start = System.currentTimeMillis(); + for (int i = 0; i < epochs; i++) { + U.println("Epoch: " + (i + 1)); + int sampleN = (int) (Math.random() * X.length); + this.train(X[sampleN], Y[sampleN], true); + } + long end = System.currentTimeMillis(); + long elapsedTime = end - start; + U.println("Training took : " + (elapsedTime / 1000) + "s"); + + break; + } + } + } + + public void train(double[] X, double[] Y, Boolean showLoss) { + Matrix input = Matrix.fromArray(X); + Matrix hidden = Matrix.multiply(weights_ih, input, useMultiThreading); + hidden.add(bias_h); + hidden.sigmoid(); + + Matrix output = Matrix.multiply(weights_ho, hidden, useMultiThreading); + output.add(bias_o); + output.sigmoid(); + + Matrix target = Matrix.fromArray(Y); + + Matrix error = Matrix.subtract(target, output); + Matrix gradient = output.dsigmoid(); + gradient.multiply(error); + gradient.multiply(l_rate); + + if (showLoss) + printLoss(error); + + Matrix hidden_T = Matrix.transpose(hidden); + Matrix who_delta = Matrix.multiply(gradient, hidden_T, useMultiThreading); + + weights_ho.add(who_delta); + bias_o.add(gradient); + + Matrix who_T = Matrix.transpose(weights_ho); + Matrix hidden_errors = Matrix.multiply(who_T, error, useMultiThreading); + + Matrix h_gradient = hidden.dsigmoid(); + h_gradient.multiply(hidden_errors); + h_gradient.multiply(l_rate); + + Matrix i_T = Matrix.transpose(input); + Matrix wih_delta = Matrix.multiply(h_gradient, i_T, useMultiThreading); + + weights_ih.add(wih_delta); + bias_h.add(h_gradient); + } + + private void printLoss(Matrix error) { + double avg = 0.0; + + for (int i = 0; i < error.rows; i++) { + for (int j = 0; j < error.cols; j++) { + avg += error.data[i][j]; + } + } + + U.print("Average Error: " + avg + "\n"); + } + + public void testData(double[][] testing_in, double[][] testing_out) { + int i = 0; + List output; + for (double d[] : testing_in) { + output = predict(d); + U.printf("Q: %s\nIn: %s\nOut: %s\nPrediction: %s\n",i,U.arrStr(d),U.arrStr(testing_out[i++]),output.toString()); + } + } + + public double[][] getFileData(String fileName) { + List rawData = U.readFile(fileName); + List filteredData = new ArrayList(); + + for (String n : rawData) { + String[] s = n.split(","); + List temp = new ArrayList(); + for (String i : s) { + temp.add(Double.parseDouble(i)); + } + filteredData.add((ArrayList) temp); + } + + double[][] dataArr = nestedListToNestedArr(filteredData); + + return dataArr; + } + + private static double[][] nestedListToNestedArr(List dataList) { + double[][] dataArr = new double[dataList.size()][dataList.get(0).size()]; + int y = 0; + for (List n : dataList) { + int x = 0; + for (Object i : n) { + dataArr[y][x] = ((Double) i); + x++; + } + y++; + } + return dataArr; + } +} + +class Matrix { + private static BasicUtils U = new BasicUtils(); + + double[][] data; + int rows, cols; + + public Matrix(int rows, int cols) { + data = new double[rows][cols]; + this.rows = rows; + this.cols = cols; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + data[i][j] = Math.random() * 2 - 1; + } + } + } + + public void print() { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + U.print(this.data[i][j] + " "); + } + U.println(""); + } + } + + public void add(int scaler) { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + this.data[i][j] += scaler; + } + + } + } + + public void add(Matrix m) { + if (cols != m.cols || rows != m.rows) { + U.println("Shape Mismatch"); + return; + } + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + this.data[i][j] += m.data[i][j]; + } + } + } + + public static Matrix fromArray(double[] x) { + Matrix temp = new Matrix(x.length, 1); + for (int i = 0; i < x.length; i++) + temp.data[i][0] = x[i]; + return temp; + } + + public List toArray() { + List temp = new ArrayList(); + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + temp.add(data[i][j]); + } + } + return temp; + } + + public static Matrix subtract(Matrix a, Matrix b) { + Matrix temp = new Matrix(a.rows, a.cols); + for (int i = 0; i < a.rows; i++) { + for (int j = 0; j < a.cols; j++) { + temp.data[i][j] = a.data[i][j] - b.data[i][j]; + } + } + return temp; + } + + public static Matrix transpose(Matrix a) { + Matrix temp = new Matrix(a.cols, a.rows); + for (int i = 0; i < a.rows; i++) { + for (int j = 0; j < a.cols; j++) { + temp.data[j][i] = a.data[i][j]; + } + } + return temp; + } + + // TODO Add Multi-threading + public static Matrix multiply(Matrix a, Matrix b, boolean useMultiThreading) { + Matrix temp = new Matrix(a.rows, b.cols); + if (!useMultiThreading) { + for (int i = 0; i < temp.rows; i++) { + for (int j = 0; j < temp.cols; j++) { + double sum = 0; + for (int k = 0; k < a.cols; k++) { + sum += a.data[i][k] * b.data[k][j]; + } + temp.data[i][j] = sum; + } + } + + } else { + + // return ParallelMatrixMultiplication.multiply(a, b); + ParallelThreadsCreator.multiply(a, b, temp); + + } + + return temp; + } + + public void multiply(Matrix a) { + for (int i = 0; i < a.rows; i++) { + for (int j = 0; j < a.cols; j++) { + this.data[i][j] *= a.data[i][j]; + } + } + + } + + public void multiply(double a) { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + this.data[i][j] *= a; + } + } + } + + public void sigmoid() { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) + this.data[i][j] = 1 / (1 + Math.exp(-this.data[i][j])); + } + } + + public Matrix dsigmoid() { + Matrix temp = new Matrix(rows, cols); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) + temp.data[i][j] = this.data[i][j] * (1 - this.data[i][j]); + } + return temp; + } +} + +class ParallelThreadsCreator { + + // creating 10 threads and waiting for them to complete then again repeat steps. + public static void multiply(Matrix matrix1, Matrix matrix2, Matrix result) { + List threads = new ArrayList<>(); + int rows1 = matrix1.rows; + for (int i = 0; i < rows1; i++) { + RowMultiplyWorker task = new RowMultiplyWorker(result, matrix1, matrix2, i); + Thread thread = new Thread(task); + thread.start(); + threads.add(thread); + if (threads.size() % 10 == 0) { + waitForThreads(threads); + } + } + } + + private static void waitForThreads(List threads) { + for (Thread thread : threads) { + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + threads.clear(); + } +} + +class WorkerThread extends Thread { + private int row; + private int col; + private int[][] A; + private int[][] B; + private int[][] C; + + public WorkerThread(int row, int col, int[][] A, int[][] B, int[][] C) { + this.row = row; + this.col = col; + this.A = A; + this.B = B; + this.C = C; + } + + public void run() { + C[row][col] = (A[row][0] * B[0][col]) + (A[row][1] * B[1][col]); + } +} + +class RowMultiplyWorker implements Runnable { + private final Matrix result; + private Matrix matrix1; + private Matrix matrix2; + private final int row; + + public RowMultiplyWorker(Matrix result, Matrix matrix1, Matrix matrix2, int row) { + this.result = result; + this.matrix1 = matrix1; + this.matrix2 = matrix2; + this.row = row; + } + + @Override + public void run() { + + for (int i = 0; i < matrix2.data[0].length; i++) { + result.data[row][i] = 0; + for (int j = 0; j < matrix1.data[row].length; j++) { + result.data[row][i] += matrix1.data[row][j] * matrix2.data[j][i]; + } + } + } +} + +final class BasicUtils { + private static PrintWriter writer = new PrintWriter(System.out); + + public static List readFile(String fileName) { + List out = new ArrayList(); + try { + File file = new File(fileName); + Scanner reader = new Scanner(file); + while (reader.hasNextLine()) { + String data = reader.nextLine(); + out.add(data); + } + reader.close(); + } catch (FileNotFoundException e) { + println("An error occurred."); + e.printStackTrace(); + } + + return out; + } + + public static String arrStr(int[] arr) { + return Arrays.toString(arr); + } + + public static String arrStr(double[] arr) { + return Arrays.toString(arr); + } + + public static String arrStr(boolean[] arr) { + return Arrays.toString(arr); + } + + public static String arrStr(Object[] arr) { + return Arrays.deepToString(arr); + } + + public static void print(Object str) { + writer.write(String.valueOf(str)); + writer.flush(); + } + + public static void println(Object str) { + print(str+"\n"); + } + + public static void printf(Object mainText, Object... x) { + String toPrint = ""; + + String[] y = String.valueOf(mainText).split("%s"); + int i = 0; + for (String n : y) { + toPrint += n; + if (i Date: Tue, 20 Dec 2022 19:14:05 -0800 Subject: [PATCH 3/9] Update NeuralNetwork.java --- Neural_Network/NeuralNetwork.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Neural_Network/NeuralNetwork.java b/Neural_Network/NeuralNetwork.java index b2a48a4..a7f3ce0 100644 --- a/Neural_Network/NeuralNetwork.java +++ b/Neural_Network/NeuralNetwork.java @@ -439,22 +439,10 @@ public static List readFile(String fileName) { return out; } - public static String arrStr(int[] arr) { - return Arrays.toString(arr); - } - public static String arrStr(double[] arr) { return Arrays.toString(arr); } - public static String arrStr(boolean[] arr) { - return Arrays.toString(arr); - } - - public static String arrStr(Object[] arr) { - return Arrays.deepToString(arr); - } - public static void print(Object str) { writer.write(String.valueOf(str)); writer.flush(); From 663fd17b3173c8fbbdd207ae93e4e242c1db3d74 Mon Sep 17 00:00:00 2001 From: JackinSac1 <87680958+JackinSac@users.noreply.github.com> Date: Tue, 20 Dec 2022 19:17:41 -0800 Subject: [PATCH 4/9] Create Main.java --- Neural_Network/Main.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Neural_Network/Main.java diff --git a/Neural_Network/Main.java b/Neural_Network/Main.java new file mode 100644 index 0000000..03712de --- /dev/null +++ b/Neural_Network/Main.java @@ -0,0 +1,16 @@ +class Main { + public static void main(String [] args) { + NeuralNetwork nn = new NeuralNetwork(2, 10, 1); + + double[][] training_data_in = nn.getFileData("training_data_in.txt"); + double[][] training_data_out = nn.getFileData("training_data_out.txt"); + + double[][] testing_data_in = nn.getFileData("testing_data_in.txt"); + double[][] testing_data_out = nn.getFileData("testing_data_out.txt"); + + int epochs = 50000; + nn.fit(training_data_in,training_data_out,epochs,0); + + nn.testData(training_data_in,training_data_out); + } +} From 76c4e15c73f48fb792e95353ca97b5a0be6f01f7 Mon Sep 17 00:00:00 2001 From: JackinSac1 <87680958+JackinSac@users.noreply.github.com> Date: Tue, 20 Dec 2022 19:34:41 -0800 Subject: [PATCH 5/9] Add files via upload --- Neural_Network/testing_data_in.txt | 4 ++++ Neural_Network/testing_data_out.txt | 4 ++++ Neural_Network/training_data_in.txt | 4 ++++ Neural_Network/training_data_out.txt | 4 ++++ 4 files changed, 16 insertions(+) create mode 100644 Neural_Network/testing_data_in.txt create mode 100644 Neural_Network/testing_data_out.txt create mode 100644 Neural_Network/training_data_in.txt create mode 100644 Neural_Network/training_data_out.txt diff --git a/Neural_Network/testing_data_in.txt b/Neural_Network/testing_data_in.txt new file mode 100644 index 0000000..0cf2a17 --- /dev/null +++ b/Neural_Network/testing_data_in.txt @@ -0,0 +1,4 @@ +0,0 +0,1 +1,0 +1,1 \ No newline at end of file diff --git a/Neural_Network/testing_data_out.txt b/Neural_Network/testing_data_out.txt new file mode 100644 index 0000000..1e35a27 --- /dev/null +++ b/Neural_Network/testing_data_out.txt @@ -0,0 +1,4 @@ +0 +1 +1 +0 \ No newline at end of file diff --git a/Neural_Network/training_data_in.txt b/Neural_Network/training_data_in.txt new file mode 100644 index 0000000..1db9b0a --- /dev/null +++ b/Neural_Network/training_data_in.txt @@ -0,0 +1,4 @@ +0,0 +1,0 +0,1 +1,1 \ No newline at end of file diff --git a/Neural_Network/training_data_out.txt b/Neural_Network/training_data_out.txt new file mode 100644 index 0000000..1e35a27 --- /dev/null +++ b/Neural_Network/training_data_out.txt @@ -0,0 +1,4 @@ +0 +1 +1 +0 \ No newline at end of file From 0cfeb7fe21fc4b358a61aeb5813e6b41b83c7997 Mon Sep 17 00:00:00 2001 From: JackinSac1 <87680958+JackinSac@users.noreply.github.com> Date: Tue, 20 Dec 2022 19:35:11 -0800 Subject: [PATCH 6/9] Update NeuralNetwork.java From 0cdb7fa4f2ad494a9c6cce0f47d60e25ca5ff89d Mon Sep 17 00:00:00 2001 From: JackinSac1 <87680958+JackinSac@users.noreply.github.com> Date: Tue, 20 Dec 2022 19:35:50 -0800 Subject: [PATCH 7/9] Update Main.java --- Neural_Network/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Neural_Network/Main.java b/Neural_Network/Main.java index 03712de..760fdc7 100644 --- a/Neural_Network/Main.java +++ b/Neural_Network/Main.java @@ -1,4 +1,4 @@ -class Main { +class Main { public static void main(String [] args) { NeuralNetwork nn = new NeuralNetwork(2, 10, 1); From 4384e83b38f8bcdf1f5a317686a209d4c0a6e0a8 Mon Sep 17 00:00:00 2001 From: JackinSac1 <87680958+JackinSac@users.noreply.github.com> Date: Tue, 20 Dec 2022 19:37:18 -0800 Subject: [PATCH 8/9] Delete NeuralNetwork.java --- NeuralNetwork.java | 481 --------------------------------------------- 1 file changed, 481 deletions(-) delete mode 100644 NeuralNetwork.java diff --git a/NeuralNetwork.java b/NeuralNetwork.java deleted file mode 100644 index b2a48a4..0000000 --- a/NeuralNetwork.java +++ /dev/null @@ -1,481 +0,0 @@ -import java.io.FileNotFoundException; -import java.util.ArrayList; -import java.io.PrintWriter; -import java.util.Scanner; -import java.util.Arrays; -import java.util.List; -import java.io.File; - -public class NeuralNetwork { - private static BasicUtils U = new BasicUtils(); - - Matrix weights_ih, weights_ho, bias_h, bias_o; - double l_rate = 0.01; - boolean useMultiThreading = false; - - public NeuralNetwork(int i, int h, int o) { - weights_ih = new Matrix(h, i); - weights_ho = new Matrix(o, h); - - bias_h = new Matrix(h, 1); - bias_o = new Matrix(o, 1); - } - - public NeuralNetwork(int i, int h, int o, boolean useMultiThreading) { - weights_ih = new Matrix(h, i); - weights_ho = new Matrix(o, h); - - bias_h = new Matrix(h, 1); - bias_o = new Matrix(o, 1); - - this.useMultiThreading = useMultiThreading; - } - - public NeuralNetwork(int i, int h, int o, double l_rate) { - weights_ih = new Matrix(h, i); - weights_ho = new Matrix(o, h); - - bias_h = new Matrix(h, 1); - bias_o = new Matrix(o, 1); - - this.l_rate = l_rate; - } - - public NeuralNetwork(int i, int h, int o, double l_rate, boolean useMultiThreading) { - weights_ih = new Matrix(h, i); - weights_ho = new Matrix(o, h); - - bias_h = new Matrix(h, 1); - bias_o = new Matrix(o, 1); - - this.l_rate = l_rate; - this.useMultiThreading = useMultiThreading; - } - - public List predict(double[] X) { - Matrix input = Matrix.fromArray(X); - Matrix hidden = Matrix.multiply(weights_ih, input, useMultiThreading); - hidden.add(bias_h); - hidden.sigmoid(); - - Matrix output = Matrix.multiply(weights_ho, hidden, useMultiThreading); - output.add(bias_o); - output.sigmoid(); - - return output.toArray(); - } - - public void fit(double[][] X, double[][] Y, int epochs) { - for (int i = 0; i < epochs; i++) { - int sampleN = (int) (Math.random() * X.length); - this.train(X[sampleN], Y[sampleN], false); - } - } - - public void fit(double[][] X, double[][] Y, int epochs, int verbose) { - switch (verbose) { - - case 0: { - U.println("Staring training with " + epochs + " epochs"); - long start = System.currentTimeMillis(); - for (int i = 0; i < epochs; i++) { - int sampleN = (int) (Math.random() * X.length); - this.train(X[sampleN], Y[sampleN], i + 1 == epochs); - } - long end = System.currentTimeMillis(); - long elapsedTime = end - start; - U.println("Training took : " + (elapsedTime / 1000) + "s\n"); - - break; - } - - case 1: { - U.println("Staring training with " + epochs + " epochs"); - long start = System.currentTimeMillis(); - for (int i = 0; i < epochs; i++) { - U.println("Epoch: " + (i + 1)); - int sampleN = (int) (Math.random() * X.length); - this.train(X[sampleN], Y[sampleN], true); - } - long end = System.currentTimeMillis(); - long elapsedTime = end - start; - U.println("Training took : " + (elapsedTime / 1000) + "s"); - - break; - } - } - } - - public void train(double[] X, double[] Y, Boolean showLoss) { - Matrix input = Matrix.fromArray(X); - Matrix hidden = Matrix.multiply(weights_ih, input, useMultiThreading); - hidden.add(bias_h); - hidden.sigmoid(); - - Matrix output = Matrix.multiply(weights_ho, hidden, useMultiThreading); - output.add(bias_o); - output.sigmoid(); - - Matrix target = Matrix.fromArray(Y); - - Matrix error = Matrix.subtract(target, output); - Matrix gradient = output.dsigmoid(); - gradient.multiply(error); - gradient.multiply(l_rate); - - if (showLoss) - printLoss(error); - - Matrix hidden_T = Matrix.transpose(hidden); - Matrix who_delta = Matrix.multiply(gradient, hidden_T, useMultiThreading); - - weights_ho.add(who_delta); - bias_o.add(gradient); - - Matrix who_T = Matrix.transpose(weights_ho); - Matrix hidden_errors = Matrix.multiply(who_T, error, useMultiThreading); - - Matrix h_gradient = hidden.dsigmoid(); - h_gradient.multiply(hidden_errors); - h_gradient.multiply(l_rate); - - Matrix i_T = Matrix.transpose(input); - Matrix wih_delta = Matrix.multiply(h_gradient, i_T, useMultiThreading); - - weights_ih.add(wih_delta); - bias_h.add(h_gradient); - } - - private void printLoss(Matrix error) { - double avg = 0.0; - - for (int i = 0; i < error.rows; i++) { - for (int j = 0; j < error.cols; j++) { - avg += error.data[i][j]; - } - } - - U.print("Average Error: " + avg + "\n"); - } - - public void testData(double[][] testing_in, double[][] testing_out) { - int i = 0; - List output; - for (double d[] : testing_in) { - output = predict(d); - U.printf("Q: %s\nIn: %s\nOut: %s\nPrediction: %s\n",i,U.arrStr(d),U.arrStr(testing_out[i++]),output.toString()); - } - } - - public double[][] getFileData(String fileName) { - List rawData = U.readFile(fileName); - List filteredData = new ArrayList(); - - for (String n : rawData) { - String[] s = n.split(","); - List temp = new ArrayList(); - for (String i : s) { - temp.add(Double.parseDouble(i)); - } - filteredData.add((ArrayList) temp); - } - - double[][] dataArr = nestedListToNestedArr(filteredData); - - return dataArr; - } - - private static double[][] nestedListToNestedArr(List dataList) { - double[][] dataArr = new double[dataList.size()][dataList.get(0).size()]; - int y = 0; - for (List n : dataList) { - int x = 0; - for (Object i : n) { - dataArr[y][x] = ((Double) i); - x++; - } - y++; - } - return dataArr; - } -} - -class Matrix { - private static BasicUtils U = new BasicUtils(); - - double[][] data; - int rows, cols; - - public Matrix(int rows, int cols) { - data = new double[rows][cols]; - this.rows = rows; - this.cols = cols; - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - data[i][j] = Math.random() * 2 - 1; - } - } - } - - public void print() { - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - U.print(this.data[i][j] + " "); - } - U.println(""); - } - } - - public void add(int scaler) { - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - this.data[i][j] += scaler; - } - - } - } - - public void add(Matrix m) { - if (cols != m.cols || rows != m.rows) { - U.println("Shape Mismatch"); - return; - } - - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - this.data[i][j] += m.data[i][j]; - } - } - } - - public static Matrix fromArray(double[] x) { - Matrix temp = new Matrix(x.length, 1); - for (int i = 0; i < x.length; i++) - temp.data[i][0] = x[i]; - return temp; - } - - public List toArray() { - List temp = new ArrayList(); - - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - temp.add(data[i][j]); - } - } - return temp; - } - - public static Matrix subtract(Matrix a, Matrix b) { - Matrix temp = new Matrix(a.rows, a.cols); - for (int i = 0; i < a.rows; i++) { - for (int j = 0; j < a.cols; j++) { - temp.data[i][j] = a.data[i][j] - b.data[i][j]; - } - } - return temp; - } - - public static Matrix transpose(Matrix a) { - Matrix temp = new Matrix(a.cols, a.rows); - for (int i = 0; i < a.rows; i++) { - for (int j = 0; j < a.cols; j++) { - temp.data[j][i] = a.data[i][j]; - } - } - return temp; - } - - // TODO Add Multi-threading - public static Matrix multiply(Matrix a, Matrix b, boolean useMultiThreading) { - Matrix temp = new Matrix(a.rows, b.cols); - if (!useMultiThreading) { - for (int i = 0; i < temp.rows; i++) { - for (int j = 0; j < temp.cols; j++) { - double sum = 0; - for (int k = 0; k < a.cols; k++) { - sum += a.data[i][k] * b.data[k][j]; - } - temp.data[i][j] = sum; - } - } - - } else { - - // return ParallelMatrixMultiplication.multiply(a, b); - ParallelThreadsCreator.multiply(a, b, temp); - - } - - return temp; - } - - public void multiply(Matrix a) { - for (int i = 0; i < a.rows; i++) { - for (int j = 0; j < a.cols; j++) { - this.data[i][j] *= a.data[i][j]; - } - } - - } - - public void multiply(double a) { - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - this.data[i][j] *= a; - } - } - } - - public void sigmoid() { - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) - this.data[i][j] = 1 / (1 + Math.exp(-this.data[i][j])); - } - } - - public Matrix dsigmoid() { - Matrix temp = new Matrix(rows, cols); - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) - temp.data[i][j] = this.data[i][j] * (1 - this.data[i][j]); - } - return temp; - } -} - -class ParallelThreadsCreator { - - // creating 10 threads and waiting for them to complete then again repeat steps. - public static void multiply(Matrix matrix1, Matrix matrix2, Matrix result) { - List threads = new ArrayList<>(); - int rows1 = matrix1.rows; - for (int i = 0; i < rows1; i++) { - RowMultiplyWorker task = new RowMultiplyWorker(result, matrix1, matrix2, i); - Thread thread = new Thread(task); - thread.start(); - threads.add(thread); - if (threads.size() % 10 == 0) { - waitForThreads(threads); - } - } - } - - private static void waitForThreads(List threads) { - for (Thread thread : threads) { - try { - thread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - threads.clear(); - } -} - -class WorkerThread extends Thread { - private int row; - private int col; - private int[][] A; - private int[][] B; - private int[][] C; - - public WorkerThread(int row, int col, int[][] A, int[][] B, int[][] C) { - this.row = row; - this.col = col; - this.A = A; - this.B = B; - this.C = C; - } - - public void run() { - C[row][col] = (A[row][0] * B[0][col]) + (A[row][1] * B[1][col]); - } -} - -class RowMultiplyWorker implements Runnable { - private final Matrix result; - private Matrix matrix1; - private Matrix matrix2; - private final int row; - - public RowMultiplyWorker(Matrix result, Matrix matrix1, Matrix matrix2, int row) { - this.result = result; - this.matrix1 = matrix1; - this.matrix2 = matrix2; - this.row = row; - } - - @Override - public void run() { - - for (int i = 0; i < matrix2.data[0].length; i++) { - result.data[row][i] = 0; - for (int j = 0; j < matrix1.data[row].length; j++) { - result.data[row][i] += matrix1.data[row][j] * matrix2.data[j][i]; - } - } - } -} - -final class BasicUtils { - private static PrintWriter writer = new PrintWriter(System.out); - - public static List readFile(String fileName) { - List out = new ArrayList(); - try { - File file = new File(fileName); - Scanner reader = new Scanner(file); - while (reader.hasNextLine()) { - String data = reader.nextLine(); - out.add(data); - } - reader.close(); - } catch (FileNotFoundException e) { - println("An error occurred."); - e.printStackTrace(); - } - - return out; - } - - public static String arrStr(int[] arr) { - return Arrays.toString(arr); - } - - public static String arrStr(double[] arr) { - return Arrays.toString(arr); - } - - public static String arrStr(boolean[] arr) { - return Arrays.toString(arr); - } - - public static String arrStr(Object[] arr) { - return Arrays.deepToString(arr); - } - - public static void print(Object str) { - writer.write(String.valueOf(str)); - writer.flush(); - } - - public static void println(Object str) { - print(str+"\n"); - } - - public static void printf(Object mainText, Object... x) { - String toPrint = ""; - - String[] y = String.valueOf(mainText).split("%s"); - int i = 0; - for (String n : y) { - toPrint += n; - if (i Date: Tue, 20 Dec 2022 19:37:44 -0800 Subject: [PATCH 9/9] Delete Driver.java --- Driver.java | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 Driver.java diff --git a/Driver.java b/Driver.java deleted file mode 100644 index e58022c..0000000 --- a/Driver.java +++ /dev/null @@ -1,22 +0,0 @@ -import java.util.List; - -public class Driver { - - static double[][] X = { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }; - static double[][] Y = { { 0 }, { 1 }, { 1 }, { 0 } }; - - public static void main(String[] args) { - - NeuralNetwork nn = new NeuralNetwork(2, 10, 1, 0.01, true); - - List output; - nn.fit(X, Y, 500, 0); - double[][] input = { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }; - for (double d[] : input) { - output = nn.predict(d); - System.out.println(output.toString()); - } - - } - -} \ No newline at end of file