Content Type
Profiles
Forums
Events
Everything posted by Dietmar
-
@Mark-XP After hard work today, I make this program. May be, it has found something common in Primes! I make a hard train for this Network with the first 1000 primes. And now, it gives some correct answers for small and BIG primes Dietmar package bine; import org.neuroph.core.NeuralNetwork; import org.neuroph.core.data.DataSet; import org.neuroph.core.data.DataSetRow; import org.neuroph.nnet.MultiLayerPerceptron; import org.neuroph.util.TransferFunctionType; import java.util.Scanner; public class Bine { public static void main(String[] args) { // Generate and store the first 1000 prime numbers in an array int[] primeNumbers = generatePrimeNumbers(1000); // Create a multi-layer perceptron neural network with 10 input neurons, 10 hidden neurons, and 2 output neurons NeuralNetwork neuralNetwork = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 20, 20, 20, 2); // Create a training set to store input-output pairs for training the neural network DataSet trainingSet = new DataSet(20, 2); Scanner scanner = new Scanner(System.in); String inputStr; System.out.println("Prime Number Classifier"); System.out.println("Enter a number between 0 and 1048575 (or 'q' to quit): "); while (!(inputStr = scanner.nextLine()).equalsIgnoreCase("q")) { try { int input = Integer.parseInt(inputStr); // Convert the input number to binary representation String binaryInput = Integer.toBinaryString(input); binaryInput = String.format("%20s", binaryInput).replace(' ', '0'); System.out.println("Input: " + input); System.out.println("Binary Input: " + binaryInput); // Convert the binary input to an array of double values double[] inputArray = new double[20]; for (int i = 0; i < binaryInput.length(); i++) { inputArray[i] = Character.getNumericValue(binaryInput.charAt(i)); } // Fill remaining array elements with 0 for (int i = binaryInput.length(); i < 20; i++) { inputArray[i] = 0; } // Use the trained neural network to make a prediction neuralNetwork.setInput(inputArray); neuralNetwork.calculate(); double[] output = neuralNetwork.getOutput(); System.out.println("Output: " + output[0] + " " + output[1]); // Display the prediction String prediction = output[0] > output[1] ? "Prime" : "Not Prime"; System.out.println("Prediction: " + prediction); // Prompt for user input on whether the input number is actually prime or not System.out.print("Is it prime? (y/n): "); String isPrime = scanner.nextLine(); if (isPrime.equalsIgnoreCase("y")) { // Add the input-output pair to the training set as a prime number DataSetRow trainingSetRow = new DataSetRow(inputArray, new double[]{1, 0}); trainingSet.add(trainingSetRow); } else { // Add the input-output pair to the training set as a non-prime number DataSetRow trainingSetRow = new DataSetRow(inputArray, new double[]{0, 1}); trainingSet.add(trainingSetRow); } // Train the neural network with the updated training set neuralNetwork.learn(trainingSet); System.out.println("Enter a number between 0 and 1048575 (or 'q' to quit): "); } catch (NumberFormatException e) { System.out.println("Invalid input. Please enter a valid number between 0 and 1048575 (or 'q' to quit): "); } } // Save the trained neural network to a file neuralNetwork.save("trained_neural_network.nnet"); System.out.println("Trained neural network saved to file: trained_neural_network.nnet"); System.out.println("Program terminated."); } /** * Generates an array of prime numbers up to a specified limit. * * @param limit The upper limit for prime numbers generation. * @return An array of prime numbers. */ private static int[] generatePrimeNumbers(int limit) { int[] primes = new int[limit]; primes[0] = 2; int count = 1; int num = 3; while (count < limit) { boolean isPrime = true; for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { isPrime = false; break; } } if (isPrime) { primes[count] = num; count++; } num += 2; } return primes; } } run: Prime Number Classifier Enter a number between 0 and 1048575 (or 'q' to quit): 7919 Input: 7919 Binary Input: 00000001111011101111 Output: 0.5948178156516414 0.40897487989552267 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 7927 Input: 7927 Binary Input: 00000001111011110111 Output: 0.8982498273851647 0.09655551571874396 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 7907 Input: 7907 Binary Input: 00000001111011100011 Output: 0.9072894778480928 0.09231869767257159 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 7933 Input: 7933 Binary Input: 00000001111011111101 Output: 0.8989077406521399 0.09734911637879708 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 7937 Input: 7937 Binary Input: 00000001111100000001 Output: 0.9043350760591787 0.09614736963345205 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 1000 Input: 1000 Binary Input: 00000000001111101000 Output: 0.8899254682389591 0.10184634378010479 Prediction: Prime Is it prime? (y/n): n Enter a number between 0 and 1048575 (or 'q' to quit): 1000 Input: 1000 Binary Input: 00000000001111101000 Output: 0.1665975490914435 0.8151083424657837 Prediction: Not Prime Is it prime? (y/n): n Enter a number between 0 and 1048575 (or 'q' to quit): 2000 Input: 2000 Binary Input: 00000000011111010000 Output: 0.28750467198426777 0.6792374064713649 Prediction: Not Prime Is it prime? (y/n): n Enter a number between 0 and 1048575 (or 'q' to quit): 3000 Input: 3000 Binary Input: 00000000101110111000 Output: 0.20588325034563312 0.789819073942458 Prediction: Not Prime Is it prime? (y/n): n Enter a number between 0 and 1048575 (or 'q' to quit): 17389 Input: 17389 Binary Input: 00000100001111101101 Output: 0.12276962636373373 0.8798565969697876 Prediction: Not Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 17389 Input: 17389 Binary Input: 00000100001111101101 Output: 0.8110576316091068 0.2032560154260368 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 17393 Input: 17393 Binary Input: 00000100001111110001 Output: 0.43852962740457774 0.5431182165605312 Prediction: Not Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 17393 Input: 17393 Binary Input: 00000100001111110001 Output: 0.8034172912800364 0.20070059184113184 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 104723 Input: 104723 Binary Input: 00011001100100010011 Output: 0.9766085585945041 0.022881199994895813 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 104724 Input: 104724 Binary Input: 00011001100100010100 Output: 0.9411592225420404 0.05804579497623357 Prediction: Prime Is it prime? (y/n): n Enter a number between 0 and 1048575 (or 'q' to quit): 104724 Input: 104724 Binary Input: 00011001100100010100 Output: 0.21484292674011074 0.7599731192129849 Prediction: Not Prime Is it prime? (y/n): n Enter a number between 0 and 1048575 (or 'q' to quit): 17401 Input: 17401 Binary Input: 00000100001111111001 Output: 0.8248729136862214 0.19731821352791543 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 17402 Input: 17402 Binary Input: 00000100001111111010 Output: 0.25042646141880837 0.7694665916822424 Prediction: Not Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 17 Input: 17 Binary Input: 00000000000000010001 Output: 0.9147609290513441 0.07336348559716502 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 5 Input: 5 Binary Input: 00000000000000000101 Output: 0.9756104355881474 0.023320270188346374 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 1 Input: 1 Binary Input: 00000000000000000001 Output: 0.9532195348828819 0.04238520033787504 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 2 Input: 2 Binary Input: 00000000000000000010 Output: 0.6778584923010748 0.28103016635239125 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 0 Input: 0 Binary Input: 00000000000000000000 Output: 0.27281379291430097 0.6823362335383929 Prediction: Not Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 97 Input: 97 Binary Input: 00000000000001100001 Output: 0.995109554413573 0.005229627558735094 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 17401 Input: 17401 Binary Input: 00000100001111111001 Output: 0.9844012869157754 0.018723648793074754 Prediction: Prime Is it prime? (y/n): y Enter a number between 0 and 1048575 (or 'q' to quit): 17400 Input: 17400 Binary Input: 00000100001111111000 Output: 0.428308665635754 0.6069720955083222 Prediction: Not Prime Is it prime? (y/n): n Enter a number between 0 and 1048575 (or 'q' to quit): 17402 Input: 17402 Binary Input: 00000100001111111010 Output: 0.7872204761301146 0.240941722495304 Prediction: Prime Is it prime? (y/n): n
-
@Mark-XP I think, this is nearly best, what Neural Networks can offer for to find primes Dietmar package bine ; import org.neuroph.core.NeuralNetwork; import org.neuroph.core.data.DataSet; import org.neuroph.core.data.DataSetRow; import org.neuroph.nnet.MultiLayerPerceptron; import org.neuroph.util.TransferFunctionType; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class Bine { private static final String PRIME_FILE_PATH = "prime_numbers.txt"; private static final String NOT_PRIME_FILE_PATH = "not_prime_numbers.txt"; public static void main(String[] args) { // Create a multi-layer perceptron neural network with 10 input neurons, 10 hidden neurons, and 2 output neurons NeuralNetwork neuralNetwork = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, 10, 10, 2); // Create a training set to store input-output pairs for training the neural network DataSet trainingSet = new DataSet(10, 2); Scanner scanner = new Scanner(System.in); String inputStr; System.out.println("Prime Number Classifier"); System.out.println("Enter a number between 0 and 1023 (or 'q' to quit): "); while (!(inputStr = scanner.nextLine()).equalsIgnoreCase("q")) { try { int input = Integer.parseInt(inputStr); // Convert the input number to binary representation String binaryInput = Integer.toBinaryString(input); binaryInput = String.format("%10s", binaryInput).replace(' ', '0'); System.out.println("Input: " + input); System.out.println("Binary Input: " + binaryInput); // Convert the binary input to an array of double values double[] inputArray = new double[10]; for (int i = 0; i < 10; i++) { inputArray[i] = Character.getNumericValue(binaryInput.charAt(i)); } // Use the trained neural network to make a prediction neuralNetwork.setInput(inputArray); neuralNetwork.calculate(); double[] output = neuralNetwork.getOutput(); System.out.println("Output: " + output[0] + " " + output[1]); // Display the prediction String prediction = output[0] > output[1] ? "Prime" : "Not Prime"; System.out.println("Prediction: " + prediction); // Prompt for user input on whether the input number is actually prime or not System.out.print("Is it prime? (y/n): "); String isPrime = scanner.nextLine(); if (isPrime.equalsIgnoreCase("y")) { // Add the input-output pair to the training set as a prime number DataSetRow trainingSetRow = new DataSetRow(inputArray, new double[]{1, 0}); trainingSet.add(trainingSetRow); writeToFile(PRIME_FILE_PATH, input); // Write the prime number to prime_numbers.txt } else { // Add the input-output pair to the training set as a non-prime number DataSetRow trainingSetRow = new DataSetRow(inputArray, new double[]{0, 1}); trainingSet.add(trainingSetRow); writeToFile(NOT_PRIME_FILE_PATH, input); // Write the non-prime number to not_prime_numbers.txt } // Train the neural network with the updated training set neuralNetwork.learn(trainingSet); System.out.println("Enter a number between 0 and 1023 (or 'q' to quit): "); } catch (NumberFormatException e) { System.out.println("Invalid input. Please enter a valid number between 0 and 1023 (or 'q' to quit): "); } } // Save the trained neural network to a file neuralNetwork.save("prime_number_classifier.nnet"); System.out.println("Neural network trained and saved successfully."); } private static void writeToFile(String filePath, int number) { try { FileWriter fileWriter = new FileWriter(filePath, true); fileWriter.write(number + "\n"); fileWriter.close(); } catch (IOException e) { System.out.println("Failed to write to file: " + e.getMessage()); } } }
-
@Mark-XP Yes. This I want to demonstrate first. Neural Networks are blind as much as possible in Mathematics. Today I make next try. More help is impossible for Neural Network. I give it 10 Input Neurons. In them I store numbers between 0 and 1023 in Binary form. Only I decide, if a number is prime or not. From this information, it should learn. For example, when the last digit is a "1", it cant be a prime (only 2). If this Network does not learn what primes are, then there is a brutal limitation in the "intelligence" of Neural Networks Dietmar
-
@Mark-XP new Prime Number program. This is nice Dietmar package primzahlerkennung; import org.neuroph.core.data.DataSet; import org.neuroph.core.data.DataSetRow; import org.neuroph.nnet.MultiLayerPerceptron; import org.neuroph.util.TransferFunctionType; import java.util.HashMap; import java.util.Random; import java.util.Scanner; public class PrimzahlErkennung { public static void main(String[] args) { int inputSize = 101; int outputSize = 1; MultiLayerPerceptron neuralNetwork = new MultiLayerPerceptron(TransferFunctionType.SIGMOID, inputSize, 10,10, outputSize); neuralNetwork.randomizeWeights(new Random()); Scanner scanner = new Scanner(System.in); HashMap<Integer, Boolean> results = new HashMap<>(); // store recognized numbers and their predicted primality DataSet trainingSet = new DataSet(inputSize, outputSize); // create a training set for updating the neural network boolean quit = false; // flag to determine when to quit the program while (!quit) { System.out.println("Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen):"); String inputString = scanner.next(); if (inputString.equalsIgnoreCase("q")) { quit = true; // set flag to quit the loop continue; // continue to next iteration } else if (inputString.equalsIgnoreCase("liste")) { System.out.println("Die getesteten Zahlen sind:"); for (int num : results.keySet()) { System.out.println(num); } continue; // continue to next iteration } int input; try { input = Integer.parseInt(inputString); } catch (NumberFormatException e) { System.out.println("Ungültige Eingabe. Bitte eine Zahl zwischen 1 und 100 eingeben:"); continue; // continue to next iteration } while (input < 1 || input > 100) { System.out.println("Ungültige Eingabe. Bitte eine Zahl zwischen 1 und 100 eingeben:"); input = scanner.nextInt(); } if (results.containsKey(input)) { boolean isPrime = results.get(input); System.out.println("Ich denke, dass die eingegebene Zahl " + (isPrime ? "eine Primzahl ist." : "keine Primzahl ist.")); } else { double[] inputValues = new double[101]; for (int i = 0; i <= 100; i++) { inputValues[i] = (i == input) ? 1 : 0; } neuralNetwork.setInput(inputValues); neuralNetwork.calculate(); double[] output = neuralNetwork.getOutput(); boolean isPrime = output[0] > 0.5; String proposition = isPrime ? "eine Primzahl ist." : "keine Primzahl ist."; System.out.println("Ich denke, dass die eingegebene Zahl " + proposition); System.out.println("Ist " + input + " eine Primzahl? (y/n):"); String feedback = scanner.next(); while (!feedback.equalsIgnoreCase("y") && !feedback.equalsIgnoreCase("n")) { System.out.println("Ungültige Eingabe. Bitte 'y' oder 'n' eingeben:"); feedback = scanner.next(); } if (feedback.equalsIgnoreCase("y")) { trainingSet.add(new DataSetRow(inputValues, new double[]{1})); results.put(input, true); } else { trainingSet.add(new DataSetRow(inputValues, new double[]{0})); results.put(input, false); } neuralNetwork.learn(trainingSet); // update neural network with new training set } } System.out.println("Vielen Dank für die Nutzung der Primzahl-Erkennungs-Anwendung. Auf Wiedersehen!"); scanner.close(); } } run: Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 1 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 1 eine Primzahl? (y/n): y Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 2 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 2 eine Primzahl? (y/n): y Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 3 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 3 eine Primzahl? (y/n): y Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 4 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 4 eine Primzahl? (y/n): n Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 5 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 5 eine Primzahl? (y/n): y Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 4 Ich denke, dass die eingegebene Zahl keine Primzahl ist. Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 6 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 6 eine Primzahl? (y/n): n Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 7 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 7 eine Primzahl? (y/n): y Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 8 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 8 eine Primzahl? (y/n): n Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 8 Ich denke, dass die eingegebene Zahl keine Primzahl ist. Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 9 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 9 eine Primzahl? (y/n): n Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 10 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 10 eine Primzahl? (y/n): n Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 11 Ich denke, dass die eingegebene Zahl eine Primzahl ist. Ist 11 eine Primzahl? (y/n): y Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 12 Ich denke, dass die eingegebene Zahl keine Primzahl ist. Ist 12 eine Primzahl? (y/n): n Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): 12 Ich denke, dass die eingegebene Zahl keine Primzahl ist. Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen): liste Die getesteten Zahlen sind: 1 2 3 4 5 6 7 8 9 10 11 12 Bitte geben Sie eine Zahl zwischen 1 und 100 ein (oder 'q' zum Beenden oder 'liste' um die getesteten Zahlen anzuzeigen):
-
Best, what you can find in Internet about Neural Network. Funny, this Network here "solves" any problem, because until now it has no Backpropagation. Means, it does not know, what it has to learn. And you can make a check, what happens to those two input values here. In my thinking, the output is any value between 0 and 1 for each Output Neuron. Should be equally(!) distributed Dietmar PS: I read, that a Neural Network can solve the same problems as a Turing Machine. But I think, that it is not true. I think, that it depends on at least 2 Hidden Layers. And for any tasks in Mathematics, Neural Networks are bad. They even cant find the Multipliers of 3 during looking for pattern in the integer Input Neurons values. I also make a try to find pattern in primes with a Neural Network. No pattern is found in primes by a Neural Network. They are blind. This is my definition of a real Intelligence: 1.) Find NEW patterns without any help. 2.) Can explain this new discovered pattern clear and easy to anyone. And sees the need of a prove for this pattern. Until now, no Ai can do this. So, SkyNet with Terminator has to wait for > 100 years.
-
@George King The from Sources 2003 build acpi.sys 5.2 bit32 is much more instable than the acpi.sys from Sources for XP SP1,2,3. So, after some talk in messages, I changed the Source Code from the acpi.sys for XP SP1, that now it supports to full(?) the acpi.sys 5.2 bit32 Dietmar PS: All acpi.sys contains now the OS fakes, that you mentioned.
-
@Mark-XP Yes, neuroph is unstable. But the idea of neuroph is nice. So, I use it only for to test some concepts. I make 0 0 2 as input with 3 Neurons, for to run not in the Normalizations problem via Sigmoid. For me, the program works. And you can see, at which numbers of Neurons in Hidden Layers and at which numbers of Hidden Layers the program is overfitted. It then gives only answers as 0,00001 0,9999 even when this is wrong. But two bad things I noticed: Here, no output file was generated, outputfile works before in my Prime program. So I can use there a lot of Prime numbers for training. But the program is blind. I think, it is a fundamental problem of Neural Networks. A lot of problems from Mathematics they cant work well on. Take knowledge in Mathematics as IQ test, then ChatGPT and all other Neural Networks have an IQ = 0. And the even/odd program sometimes hangs up itself on the very first input Dietmar PS: Yesterday in the evening ChatGPT tolds me, that without mathematical formulas it can not decide, if a given number is a multiplier of 3. So, no SkyNet in this century.
-
This program learns the multipler of 2, nice . It then stores the learning in a file Dietmar package multi; import org.neuroph.core.data.DataSet; import org.neuroph.core.data.DataSetRow; import org.neuroph.nnet.MultiLayerPerceptron; import org.neuroph.nnet.learning.BackPropagation; import java.util.Scanner; public class Multi { public static void main(String[] args) { // create MultiLayerPerceptron neural network MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(3, 4,4, 2); neuralNet.setLearningRule(new BackPropagation()); // create training set DataSet trainingSet = new DataSet(3, 2); trainingSet.add(new DataSetRow(new double[]{1, 2, 3}, new double[]{0, 1})); //odd trainingSet.add(new DataSetRow(new double[]{2, 2, 2}, new double[]{1, 0})); //even trainingSet.add(new DataSetRow(new double[]{4, 6, 0}, new double[]{1, 0})); //even trainingSet.add(new DataSetRow(new double[]{0, 0, 1}, new double[]{0, 1})); //odd Scanner scanner = new Scanner(System.in); while (true) { System.out.println("Enter a comma-separated list of 3 numbers or 'exit' to quit:"); String inputStr = scanner.nextLine(); if (inputStr.equals("exit")) { break; } String[] inputStrArray = inputStr.split(","); double[] input = new double[3]; for (int i = 0; i < 3; i++) { input[i] = Double.parseDouble(inputStrArray[i].trim()); } neuralNet.setInput(input); neuralNet.calculate(); double[] output = neuralNet.getOutput(); System.out.printf("Network prediction: %.2f odd, %.2f even%n", output[0], output[1]); if (output[0] > output[1]) { System.out.print("I think this is odd. Is that correct? (y/n) "); String answer = scanner.nextLine(); if (answer.equals("y")) { trainingSet.add(new DataSetRow(input, new double[]{1, 0})); //correcting as odd } else { trainingSet.add(new DataSetRow(input, new double[]{0, 1})); //correcting as even } } else { System.out.print("I think this is even. Is that correct? (y/n) "); String answer = scanner.nextLine(); if (answer.equals("y")) { trainingSet.add(new DataSetRow(input, new double[]{0, 1})); //correcting as even } else { trainingSet.add(new DataSetRow(input, new double[]{1, 0})); //correcting as odd } } // train neural network with updated training set neuralNet.learn(trainingSet); } // save trained neural network neuralNet.save("oddoreven.nnet"); } }
-
By the way, without counting numbers together (Quersumme) and without factors (n Modulo 3 = 0), ChatGPT cant see, if a number is a Multiplier of 3 or not. I make this test today evening. ChatGPT tells me, that it has no idea, how to find this out without any given formula. At once I understand the same crazy result by my Neural Network today by 100% Dietmar
-
Today I find by myself a very precise definition, what Intelligence really is: 1.) You find only by yourself a pattern. 2.) Then you can describe this pattern to somebody other. Very careful and exact. And why this pattern always works. Maximum, what any kind of Artificially Intelligence can do today, April 2023 is, to find a pattern. But not to describe it and why this works. Few months ago the Ai find a new, better way to multiply matritzes. But mathematics needs months for to understand, what the Ai is doing. Because the Ai understands nothing and so cant help them. Roentgen does not understand the nature of X-rays. Einstein understands his theory. Here, you can see the big big difference at once. SkyNet with terminator moves via this way soso much far away to future, I think >>40 years Dietmar
-
I make an interesting observation. Until now, I do not succeed to train an Neural Network, so that it can recognice the multiplers of 3. Hm, interesting: ChatGPT cant find until now all multiplieres of 7. So, some Mathematics seems to be a cool Intelligence Test for any Artificial Intelligence. Soon, I understand most pupils at school.. The task for the Ai was: I train with numbers to 1000. The multiplers of 2 she found all correct. But even with loong training, that 1002 is a multipler from 3, I gave up with 1005^^, oh my Dietmar PS: No tools like modulo 3 = 0 are allowed. Only the naked input data.
-
The Prime Program produces a file "input_log.txt". This file is stored. Now I write another program, for to put correct information about all no primes and primes into this file. It works. All the numbers in this file are now correct identified as primes or no prime from the prim program above. To teach the prim program, you have to answer with "0" for no prime and "1" for prime. Neural Networks can find pattern. I check with even and not even numbers, works. Here is the generator program for a filled "input_log.txt" until 1000 with correct information, cool Dietmar package generateinputlog; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class GenerateInputLog { public static void main(String[] args) throws IOException { String logFileName = "input_log.txt"; BufferedWriter writer = new BufferedWriter(new FileWriter(logFileName)); for (int i = 0; i <= 1000; i++) { boolean isPrime = isPrime(i); int output = isPrime ? 1 : 0; writer.write(i + "," + output + "\n"); } writer.close(); } private static boolean isPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { return false; } } return true; } }
-
@Mark-XP I write a new Prime Number program with Neuroph. It is the best until now. But it cant find primes. If there is anything, that Neural Networks can found, that it is a pattern. So, even with really BIG learning, no pattern can be found in Primes via a Neural network Dietmar package geradeungeradenetzwerk; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import org.neuroph.core.NeuralNetwork; import org.neuroph.core.data.DataSet; import org.neuroph.core.data.DataSetRow; import org.neuroph.nnet.MultiLayerPerceptron; import org.neuroph.nnet.learning.BackPropagation; public class GeradeUngeradeNetzwerk { public static void main(String[] args) throws IOException { String logFileName = "input_log.txt"; Map<Integer, Double> userInputMap = new HashMap<>(); // Load input-output pairs from log file DataSet dataset = new DataSet(1, 1); if (Files.exists(Paths.get(logFileName))) { List<String> lines = Files.readAllLines(Paths.get(logFileName)); for (String line : lines) { String[] parts = line.split(","); int input = Integer.parseInt(parts[0]); double output = Double.parseDouble(parts[1]); double[] inputArr = new double[] { input }; double[] outputArr = new double[] { output }; dataset.add(new DataSetRow(inputArr, outputArr)); userInputMap.put(input, output); } } // Train neural network on input-output pairs MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(1, 300, 300,300,300,300,300,300, 1); BackPropagation learningRule = neuralNet.getLearningRule(); learningRule.setMaxError(0.1); learningRule.setMaxIterations(1000); neuralNet.learn(dataset); // Use trained neural network to classify new integers as prime or not prime Scanner scanner = new Scanner(System.in); while (true) { System.out.print("Enter an integer (or 'exit' to quit): "); String inputStr = scanner.nextLine(); if (inputStr.equals("exit")) { break; } int input; try { input = Integer.parseInt(inputStr); } catch (NumberFormatException e) { System.out.println("Invalid input. Please enter an integer."); continue; } if(userInputMap.containsKey(input)){ double result = userInputMap.get(input); if (result >= 0.5) { System.out.println(input + " is prime (according to the neural network)"); } else { System.out.println(input + " is not prime (according to the neural network)"); } } else{ double[] inputArr = new double[] { input }; neuralNet.setInput(inputArr); neuralNet.calculate(); double[] output = neuralNet.getOutput(); double result = output[0]; if (result >= 0.5) { System.out.println(input + " is prime (according to the neural network)"); } else { System.out.println(input + " is not prime (according to the neural network)"); } // Ask user if result is correct and store input-output pair in log file System.out.print("Is this result correct? (y/n): "); String answer = scanner.nextLine().toLowerCase(); if (answer.equals("y")) { double[] outputArr = new double[]{result}; dataset.add(new DataSetRow(inputArr, outputArr)); BufferedWriter writer = new BufferedWriter(new FileWriter(logFileName, true)); writer.write(input + "," + result + "\n"); writer.close(); userInputMap.put(input, result); } else if (answer.equals("n")) { // Change output to the correct result and add input-output pair to log file System.out.print("What is the correct result? "); String correctResultStr = scanner.nextLine(); try { double correctResult = Double.parseDouble(correctResultStr); double[] outputArr = new double[]{correctResult}; dataset.add(new DataSetRow(inputArr, outputArr)); BufferedWriter writer = new BufferedWriter(new FileWriter(logFileName, true)); writer.write(input + "," + correctResult + "\n"); writer.close(); userInputMap.put(input, correctResult); } catch (NumberFormatException e) { System.out.println("Invalid input, the result was not recorded"); } } } }}}
-
@Mark-XP Here is a new version. It shows all my helpless (or the helpless of Ai). First I use the Input 0 0 0. Program calculates all Bias and Weights, so that it reaches about 0.5. Then, after 20 Iterations, the last Bias and Values are stored. Now, without any Backpropagation, I use this set of Bias and weights, for the new Input 0 0 1. And now, the crazy things happen: After each run, the output for the 0 0 0 is about 0.5. But the output for the 0 0 1 changes at lot with each run. ChatGPT tells me, to take the Middle of all the used Bias and Weights. But I think, this is helpless as much as possible. So, I have no idea to really train my Network for primes Dietmar package neuralnetwork; import java.util.Arrays; import java.util.concurrent.ThreadLocalRandom; import java.util.Scanner; public class NeuralNetwork { private int numInputNodes; private int numHiddenNodes1; private int numHiddenNodes2; private int numOutputNodes; private double[][] weights1; private double[][] weights2; private double[][] weights3; private double[] bias1; private double[] bias2; private double[] bias3; public NeuralNetwork(int numInputNodes, int numHiddenNodes1, int numHiddenNodes2, int numOutputNodes) { this.numInputNodes = numInputNodes; this.numHiddenNodes1 = numHiddenNodes1; this.numHiddenNodes2 = numHiddenNodes2; this.numOutputNodes = numOutputNodes; this.weights1 = new double[numInputNodes][numHiddenNodes1]; for (int i = 0; i < numInputNodes; i++) { for (int j = 0; j < numHiddenNodes1; j++) { this.weights1[i][j] = ThreadLocalRandom.current().nextDouble(-1, 1); } } this.weights2 = new double[numHiddenNodes1][numHiddenNodes2]; for (int i = 0; i < numHiddenNodes1; i++) { for (int j = 0; j < numHiddenNodes2; j++) { this.weights2[i][j] = ThreadLocalRandom.current().nextDouble(-1, 1); } } this.weights3 = new double[numHiddenNodes2][numOutputNodes]; for (int i = 0; i < numHiddenNodes2; i++) { for (int j = 0; j < numOutputNodes; j++) { this.weights3[i][j] = ThreadLocalRandom.current().nextDouble(-1, 1); } } this.bias1 = new double[numHiddenNodes1]; for (int i = 0; i < numHiddenNodes1; i++) { this.bias1[i] = ThreadLocalRandom.current().nextDouble(-1, 1); } this.bias2 = new double[numHiddenNodes2]; for (int i = 0; i < numHiddenNodes2; i++) { this.bias2[i] = ThreadLocalRandom.current().nextDouble(-1, 1); } this.bias3 = new double[numOutputNodes]; for (int i = 0; i < numOutputNodes; i++) { this.bias3[i] = ThreadLocalRandom.current().nextDouble(-1, 1); } } public double[] feedForward(double[] inputs) { double[] hidden1 = new double[numHiddenNodes1]; double[] hidden2 = new double[numHiddenNodes2]; double[] outputs = new double[numOutputNodes]; // Calculate outputs of hidden layer 1 for (int j = 0; j < numHiddenNodes1; j++) { double sum = 0; for (int i = 0; i < numInputNodes; i++) { sum += inputs[i] * weights1[i][j]; } sum += bias1[j]; hidden1[j] = Math.tanh(sum); } // Calculate outputs of hidden layer 2 for (int j = 0; j < numHiddenNodes2; j++) { double sum = 0; for (int i = 0; i < numHiddenNodes1; i++) { sum += hidden1[i] * weights2[i][j]; } sum += bias2[j]; hidden2[j] = Math.tanh(sum);; } // Calculate outputs for (int j = 0; j < numOutputNodes; j++) { double sum = 0; for (int i = 0; i < numHiddenNodes2; i++) { sum += hidden2[i] * weights3[i][j]; } sum += bias3[j]; outputs[j] = Math.tanh(sum);; } return outputs; } public void setWeights1(double[][] weights) { this.weights1 = weights; } public void setBias1(double[] bias) { this.bias1 = bias; } public void setWeights2(double[][] weights) { this.weights2 = weights; } public void setBias2(double[] bias) { this.bias2 = bias; } public void setWeights3(double[][] weights) { this.weights3 = weights; } public void setBias3(double[] bias) { this.bias3 = bias; } public double[][] getWeights1() { return this.weights1; } public double[][] getWeights2() { return this.weights2; } public double[][] getWeights3() { return this.weights3; } public double[] getBias1() { return this.bias1; } public double[] getBias2() { return this.bias2; } public double[] getBias3() { return this.bias3; } // Backward Propagation public void backPropagate(double[] inputs, double[] expectedOutputs, double learningRate) { // Feed forward to get outputs double[] hidden1 = new double[numHiddenNodes1]; double[] hidden2 = new double[numHiddenNodes2]; double[] outputs = feedForward(inputs); // Calculate error in output layer double[] outputErrors = new double[numOutputNodes]; for (int i = 0; i < numOutputNodes; i++) { outputErrors[i] = expectedOutputs[i] - outputs[i]; } // Calculate error in hidden layer 2 double[] hidden2Errors = new double[numHiddenNodes2]; for (int i = 0; i < numHiddenNodes2; i++) { double error = 0; for (int j = 0; j < numOutputNodes; j++) { error += outputErrors[j] * weights3[i][j]; } hidden2Errors[i] = (1 - Math.pow(Math.tanh(hidden2[i]), 2)) * error; } // Calculate error in hidden layer 1 double[] hidden1Errors = new double[numHiddenNodes1]; for (int i = 0; i < numHiddenNodes1; i++) { double error = 0; for (int j = 0; j < numHiddenNodes2; j++) { error += hidden2Errors[j] * weights2[i][j]; } hidden1Errors[i] = (1 - Math.pow(Math.tanh(hidden1[i]), 2)) * error; } // Update weights and biases in output layer for (int i = 0; i < numHiddenNodes2; i++) { for (int j = 0; j < numOutputNodes; j++) { double delta = outputErrors[j] * tanhDerivative(outputs[j]) * hidden2[i]; weights3[i][j] += learningRate * delta; } } for (int i = 0; i < numOutputNodes; i++) { bias3[i] += learningRate * outputErrors[i] * tanhDerivative(outputs[i]); } // Calculate error in hidden layer 1 for (int i = 0; i < numHiddenNodes1; i++) { double error = 0; for (int j = 0; j < numHiddenNodes2; j++) { error += hidden2Errors[j] * weights2[i][j]; } hidden1Errors[i] = error * tanhDerivative(hidden1[i]); } // Update weights and biases in hidden layer 2 for (int i = 0; i < numHiddenNodes1; i++) { for (int j = 0; j < numHiddenNodes2; j++) { double delta = hidden2Errors[j] * tanhDerivative(hidden2[j]) * hidden1[i]; weights2[i][j] += learningRate * delta; } } for (int i = 0; i < numHiddenNodes2; i++) { bias2[i] += learningRate * hidden2Errors[i] * tanhDerivative(hidden2[i]); } // Update weights and biases in hidden layer 1 for (int i = 0; i < numInputNodes; i++) { for (int j = 0; j < numHiddenNodes1; j++) { double delta = hidden1Errors[j] * tanhDerivative(hidden1[j]) * inputs[i]; weights1[i][j] += learningRate * delta; } } for (int i = 0; i < numHiddenNodes1; i++) { bias1[i] += learningRate * hidden1Errors[i] * tanhDerivative(hidden1[i]); } } // Helper method to calculate the derivative of the hyperbolic tangent function private double tanhDerivative(double x) { double tanh = Math.tanh(x); return 1 - tanh * tanh; } public static void main(String[] args) { NeuralNetwork nn = new NeuralNetwork(3, 4, 4, 1); double[] inputs = {0, 0, 0}; double[] expectedOutputs = {0.5}; for (int i = 0; i < 20; i++) { nn.backPropagate(inputs, expectedOutputs, 0.1); double[] outputs = nn.feedForward(inputs); System.out.println("\nIteration " + (i+1)); System.out.println("Weights1:"); System.out.println(Arrays.deepToString(nn.getWeights1())); System.out.println("Bias1:"); System.out.println(Arrays.toString(nn.getBias1())); System.out.println("Weights2:"); System.out.println(Arrays.deepToString(nn.getWeights2())); System.out.println("Bias2:"); System.out.println(Arrays.toString(nn.getBias2())); System.out.println("Weights3:"); System.out.println(Arrays.deepToString(nn.getWeights3())); System.out.println("Bias3:"); System.out.println(Arrays.toString(nn.getBias3())); System.out.println("Output:"); System.out.println(Arrays.toString(outputs)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } Scanner scanner = new Scanner(System.in); System.out.println("\nEnter any key to continue:"); scanner.nextLine(); System.out.println("Initial weights and biases:"); System.out.println(Arrays.deepToString(nn.getWeights1())); System.out.println(Arrays.toString(nn.getBias1())); System.out.println(Arrays.deepToString(nn.getWeights2())); System.out.println(Arrays.toString(nn.getBias2())); System.out.println(Arrays.deepToString(nn.getWeights3())); System.out.println(Arrays.toString(nn.getBias3())); while (true) { System.out.println("\nEnter 'q' to quit or any other key to continue:"); String input = scanner.nextLine(); if (input.equals("q")) { break; } // Change weight and bias values nn.getWeights1()[1][2] += 0.5; nn.getBias2()[1] -= 0.5; nn.getWeights3()[0][0] *= 2; // Output modified weights and biases System.out.println("\nModified weights and biases:"); System.out.println(Arrays.deepToString(nn.getWeights1())); System.out.println(Arrays.toString(nn.getBias1())); System.out.println(Arrays.deepToString(nn.getWeights2())); System.out.println(Arrays.toString(nn.getBias2())); System.out.println(Arrays.deepToString(nn.getWeights3())); System.out.println(Arrays.toString(nn.getBias3())); // Feed-forward with new inputs using the last trained weights and biases double[] newInputs = {0, 0, 1}; nn.setWeights1(nn.getWeights1()); nn.setBias1(nn.getBias1()); nn.setWeights2(nn.getWeights2()); nn.setBias2(nn.getBias2()); nn.setWeights3(nn.getWeights3()); nn.setBias3(nn.getBias3()); double[] newOutputs = nn.feedForward(newInputs); System.out.println("New output:"); System.out.println(Arrays.toString(newOutputs)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
-
@Mark-XP I notice 2 things: Always the Wights and Bias are different, during different runs. And there appear Weights and Bias >1 and < -1 ??? Chatgpt tells, that this is normal. I would say, it is a big mistake. I really start to wonder, what an Ai is really learning. It can learn facts. But then? As long as there are 2 possible ways, with complete different Weights and Bias for to reach the same result, the Ai has no chance and takes randomally any way. Any "understanding" in such a situation is impossible, Dietmar PS: Those values >1 or <-1 are produced from the Backpropagate algorithmus, brr.. So I think, much more theoretical examination is necessary for Neural Networks.
-
@Mark-XP A very interesting thing is, to compare all the Bias and Weights after training. I mean, to compare the last Bias and Weights between 2 different runs of the program. As you can see, the values change stetig after some iterations in one and the same run. But I think, that the Neural Network only "understands" the problem, IF in each run at the end the weights are ALL nearly identic. If not, there exist many diffferent solutions for the same(!) task and the Neural Network has no idea, which way to choose Dietmar
-
@Mark-XP Here is another example. As you can see, it runs also stable with 1000 Neurons in each of the 2 Hidden Layers. Just now, it is more stable than Neuroph. And I also understand, for what the BIG BIG amount of memory is needed: ALL the Bias and Weights and Outputs have to be stored for each iteration. Only then, the network can learn Dietmar PS: Question is, if after training most of the used memory can be set free because only the last Bias, Weights and Outputs have to be stored. But at this example you can see, how much resources during training are really needed. Gigabyte. package neuralnetwork; import java.util.Arrays; import java.util.concurrent.ThreadLocalRandom; import java.util.Scanner; public class NeuralNetwork { private int numInputNodes; private int numHiddenNodes1; private int numHiddenNodes2; private int numOutputNodes; private double[][] weights1; private double[][] weights2; private double[][] weights3; private double[] bias1; private double[] bias2; private double[] bias3; public NeuralNetwork(int numInputNodes, int numHiddenNodes1, int numHiddenNodes2, int numOutputNodes) { this.numInputNodes = numInputNodes; this.numHiddenNodes1 = numHiddenNodes1; this.numHiddenNodes2 = numHiddenNodes2; this.numOutputNodes = numOutputNodes; this.weights1 = new double[numInputNodes][numHiddenNodes1]; for (int i = 0; i < numInputNodes; i++) { for (int j = 0; j < numHiddenNodes1; j++) { this.weights1[i][j] = ThreadLocalRandom.current().nextDouble(-1, 1); } } this.weights2 = new double[numHiddenNodes1][numHiddenNodes2]; for (int i = 0; i < numHiddenNodes1; i++) { for (int j = 0; j < numHiddenNodes2; j++) { this.weights2[i][j] = ThreadLocalRandom.current().nextDouble(-1, 1); } } this.weights3 = new double[numHiddenNodes2][numOutputNodes]; for (int i = 0; i < numHiddenNodes2; i++) { for (int j = 0; j < numOutputNodes; j++) { this.weights3[i][j] = ThreadLocalRandom.current().nextDouble(-1, 1); } } this.bias1 = new double[numHiddenNodes1]; for (int i = 0; i < numHiddenNodes1; i++) { this.bias1[i] = ThreadLocalRandom.current().nextDouble(-1, 1); } this.bias2 = new double[numHiddenNodes2]; for (int i = 0; i < numHiddenNodes2; i++) { this.bias2[i] = ThreadLocalRandom.current().nextDouble(-1, 1); } this.bias3 = new double[numOutputNodes]; for (int i = 0; i < numOutputNodes; i++) { this.bias3[i] = ThreadLocalRandom.current().nextDouble(-1, 1); } } public double[] feedForward(double[] inputs) { double[] hidden1 = new double[numHiddenNodes1]; double[] hidden2 = new double[numHiddenNodes2]; double[] outputs = new double[numOutputNodes]; // Calculate outputs of hidden layer 1 for (int j = 0; j < numHiddenNodes1; j++) { double sum = 0; for (int i = 0; i < numInputNodes; i++) { sum += inputs[i] * weights1[i][j]; } sum += bias1[j]; hidden1[j] = Math.tanh(sum); } // Calculate outputs of hidden layer 2 for (int j = 0; j < numHiddenNodes2; j++) { double sum = 0; for (int i = 0; i < numHiddenNodes1; i++) { sum += hidden1[i] * weights2[i][j]; } sum += bias2[j]; hidden2[j] = Math.tanh(sum);; } // Calculate outputs for (int j = 0; j < numOutputNodes; j++) { double sum = 0; for (int i = 0; i < numHiddenNodes2; i++) { sum += hidden2[i] * weights3[i][j]; } sum += bias3[j]; outputs[j] = Math.tanh(sum);; } return outputs; } public double[][] getWeights1() { return this.weights1; } public double[][] getWeights2() { return this.weights2; } public double[][] getWeights3() { return this.weights3; } public double[] getBias1() { return this.bias1; } public double[] getBias2() { return this.bias2; } public double[] getBias3() { return this.bias3; } // Backward Propagation public void backPropagate(double[] inputs, double[] expectedOutputs, double learningRate) { // Feed forward to get outputs double[] hidden1 = new double[numHiddenNodes1]; double[] hidden2 = new double[numHiddenNodes2]; double[] outputs = feedForward(inputs); // Calculate error in output layer double[] outputErrors = new double[numOutputNodes]; for (int i = 0; i < numOutputNodes; i++) { outputErrors[i] = expectedOutputs[i] - outputs[i]; } // Calculate error in hidden layer 2 double[] hidden2Errors = new double[numHiddenNodes2]; for (int i = 0; i < numHiddenNodes2; i++) { double error = 0; for (int j = 0; j < numOutputNodes; j++) { error += outputErrors[j] * weights3[i][j]; } hidden2Errors[i] = (1 - Math.pow(Math.tanh(hidden2[i]), 2)) * error; } // Calculate error in hidden layer 1 double[] hidden1Errors = new double[numHiddenNodes1]; for (int i = 0; i < numHiddenNodes1; i++) { double error = 0; for (int j = 0; j < numHiddenNodes2; j++) { error += hidden2Errors[j] * weights2[i][j]; } hidden1Errors[i] = (1 - Math.pow(Math.tanh(hidden1[i]), 2)) * error; } // Update weights and biases in output layer for (int i = 0; i < numHiddenNodes2; i++) { for (int j = 0; j < numOutputNodes; j++) { double delta = outputErrors[j] * tanhDerivative(outputs[j]) * hidden2[i]; weights3[i][j] += learningRate * delta; } } for (int i = 0; i < numOutputNodes; i++) { bias3[i] += learningRate * outputErrors[i] * tanhDerivative(outputs[i]); } // Calculate error in hidden layer 1 for (int i = 0; i < numHiddenNodes1; i++) { double error = 0; for (int j = 0; j < numHiddenNodes2; j++) { error += hidden2Errors[j] * weights2[i][j]; } hidden1Errors[i] = error * tanhDerivative(hidden1[i]); } // Update weights and biases in hidden layer 2 for (int i = 0; i < numHiddenNodes1; i++) { for (int j = 0; j < numHiddenNodes2; j++) { double delta = hidden2Errors[j] * tanhDerivative(hidden2[j]) * hidden1[i]; weights2[i][j] += learningRate * delta; } } for (int i = 0; i < numHiddenNodes2; i++) { bias2[i] += learningRate * hidden2Errors[i] * tanhDerivative(hidden2[i]); } // Update weights and biases in hidden layer 1 for (int i = 0; i < numInputNodes; i++) { for (int j = 0; j < numHiddenNodes1; j++) { double delta = hidden1Errors[j] * tanhDerivative(hidden1[j]) * inputs[i]; weights1[i][j] += learningRate * delta; } } for (int i = 0; i < numHiddenNodes1; i++) { bias1[i] += learningRate * hidden1Errors[i] * tanhDerivative(hidden1[i]); } } // Helper method to calculate the derivative of the hyperbolic tangent function private double tanhDerivative(double x) { double tanh = Math.tanh(x); return 1 - tanh * tanh; } public static void main(String[] args) { NeuralNetwork nn = new NeuralNetwork(3, 1000, 1000, 1); double[] inputs = {7, 3, 9}; double[] expectedOutputs = {0.5}; for (int i = 0; i < 20; i++) { nn.backPropagate(inputs, expectedOutputs, 0.00001); double[] outputs = nn.feedForward(inputs); System.out.println("\nIteration " + (i+1)); System.out.println("Weights1:"); System.out.println(Arrays.deepToString(nn.getWeights1())); System.out.println("Bias1:"); System.out.println(Arrays.toString(nn.getBias1())); System.out.println("Weights2:"); System.out.println(Arrays.deepToString(nn.getWeights2())); System.out.println("Bias2:"); System.out.println(Arrays.toString(nn.getBias2())); System.out.println("Weights3:"); System.out.println(Arrays.deepToString(nn.getWeights3())); System.out.println("Bias3:"); System.out.println(Arrays.toString(nn.getBias3())); System.out.println("Output:"); System.out.println(Arrays.toString(outputs)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } Scanner scanner = new Scanner(System.in); System.out.println("\nEnter any key to continue:"); scanner.nextLine(); System.out.println("Initial weights and biases:"); System.out.println(Arrays.deepToString(nn.getWeights1())); System.out.println(Arrays.toString(nn.getBias1())); System.out.println(Arrays.deepToString(nn.getWeights2())); System.out.println(Arrays.toString(nn.getBias2())); System.out.println(Arrays.deepToString(nn.getWeights3())); System.out.println(Arrays.toString(nn.getBias3())); while (true) { System.out.println("\nEnter 'q' to quit or any other key to continue:"); String input = scanner.nextLine(); if (input.equals("q")) { break; } // Change weight and bias values nn.getWeights1()[1][2] += 0.5; nn.getBias2()[1] -= 0.5; nn.getWeights3()[0][0] *= 2; // Output modified weights and biases System.out.println("\nModified weights and biases:"); System.out.println(Arrays.deepToString(nn.getWeights1())); System.out.println(Arrays.toString(nn.getBias1())); System.out.println(Arrays.deepToString(nn.getWeights2())); System.out.println(Arrays.toString(nn.getBias2())); System.out.println(Arrays.deepToString(nn.getWeights3())); System.out.println(Arrays.toString(nn.getBias3())); // Calculate and output new output with modified weights and biases double[] outputs = nn.feedForward(inputs); System.out.println("New output:"); System.out.println(Arrays.toString(outputs)); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }