Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class HopfieldNetwork {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the number of input patterns: ");
- int n = sc.nextInt();
- int s[][] = new int[n][];
- System.out.println("Enter the length of the inputs: ");
- int length = sc.nextInt();
- for (int i = 0; i < s.length; i++) {
- s[i] = new int[length];
- System.out.println("Enter the pattern for s" + (i + 1) + ":");
- for (int j = 0; j < s[i].length; j++) {
- s[i][j] = sc.nextInt();
- }
- }
- int[][] weights = new int[length][length];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < length; j++) {
- for (int k = 0; k < length; k++) {
- weights[j][k] += (s[i][j] * s[i][k]);
- }
- }
- }
- System.out.println("Making the diagonal elements as 0: ");
- for (int i = 0; i < weights.length; i++) {
- weights[i][i] = 0;
- }
- System.out.println("The weight matrix is: ");
- for (int i = 0; i < weights.length; i++) {
- System.out.println(Arrays.toString(weights[i]));
- }
- for (int i = 0; i < n; i++) {
- int inter[][] = new int[n][length];
- System.out.println("Now we will calculate WP" + i + "");
- for (int j = 0; j < length; j++) {
- for (int k = 0; k < length; k++) {
- inter[i][k] += weights[k][j] * s[i][j];
- }
- }
- for (int p = 0; p < length; p++) {
- inter[i][p] = inter[i][p] / (Math.abs(inter[i][p]));
- }
- System.out.println("Output for pattern " + (i + 1) + " is: " + Arrays.toString(inter[i]));
- }
- }
- }
- /*
- bash-4.1$ javac HopfieldNetwork.java
- bash-4.1$ java HopfieldNetwork
- Enter the number of input patterns:
- 3
- Enter the length of the inputs:
- 6
- Enter the pattern for s1:
- 1 1 1 -1 -1 -1
- Enter the pattern for s2:
- -1 -1 -1 1 1 1
- Enter the pattern for s3:
- 1 1 -1 -1 1 1
- Making the diagonal elements as 0:
- The weight matrix is:
- [0, 3, 1, -3, -1, -1]
- [3, 0, 1, -3, -1, -1]
- [1, 1, 0, -1, -3, -3]
- [-3, -3, -1, 0, 1, 1]
- [-1, -1, -3, 1, 0, 3]
- [-1, -1, -3, 1, 3, 0]
- Now we will calculate WP0
- Output for pattern 1 is: [1, 1, 1, -1, -1, -1]
- Now we will calculate WP1
- Output for pattern 2 is: [-1, -1, -1, 1, 1, 1]
- Now we will calculate WP2
- Output for pattern 3 is: [1, 1, -1, -1, 1, 1]
- bash-4.1$ java HopfieldNetwork
- Enter the number of input patterns:
- 4
- Enter the length of the inputs:
- 4
- Enter the pattern for s1:
- 1 -1 1 -1
- Enter the pattern for s2:
- -1 1 -1 1
- Enter the pattern for s3:
- 1 1 -1 -1
- Enter the pattern for s4:
- -1 -1 1 1
- Making the diagonal elements as 0:
- The weight matrix is:
- [0, 0, 0, -4]
- [0, 0, -4, 0]
- [0, -4, 0, 0]
- [-4, 0, 0, 0]
- Now we will calculate WP0
- Output for pattern 1 is: [1, -1, 1, -1]
- Now we will calculate WP1
- Output for pattern 2 is: [-1, 1, -1, 1]
- Now we will calculate WP2
- Output for pattern 3 is: [1, 1, -1, -1]
- Now we will calculate WP3
- Output for pattern 4 is: [-1, -1, 1, 1]
- */
Add Comment
Please, Sign In to add comment