sandeshMC

HopfieldNetwork

Mar 22nd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class HopfieldNetwork {
  4.  
  5.     /**
  6.      * @param args the command line arguments
  7.      */
  8.     public static void main(String[] args) {
  9.         // TODO code application logic here
  10.         Scanner sc = new Scanner(System.in);
  11.         System.out.println("Enter the number of input patterns: ");
  12.         int n = sc.nextInt();
  13.         int s[][] = new int[n][];
  14.         System.out.println("Enter the length of the inputs: ");
  15.         int length = sc.nextInt();
  16.         for (int i = 0; i < s.length; i++) {
  17.             s[i] = new int[length];
  18.             System.out.println("Enter the pattern for s" + (i + 1) + ":");
  19.             for (int j = 0; j < s[i].length; j++) {
  20.                 s[i][j] = sc.nextInt();
  21.             }
  22.         }
  23.         int[][] weights = new int[length][length];
  24.         for (int i = 0; i < n; i++) {
  25.             for (int j = 0; j < length; j++) {
  26.                 for (int k = 0; k < length; k++) {
  27.                     weights[j][k] += (s[i][j] * s[i][k]);
  28.                 }
  29.             }
  30.         }
  31.         System.out.println("Making the diagonal elements as 0: ");
  32.         for (int i = 0; i < weights.length; i++) {
  33.             weights[i][i] = 0;
  34.         }
  35.         System.out.println("The weight matrix is: ");
  36.         for (int i = 0; i < weights.length; i++) {
  37.             System.out.println(Arrays.toString(weights[i]));
  38.         }
  39.         for (int i = 0; i < n; i++) {
  40.             int inter[][] = new int[n][length];
  41.             System.out.println("Now we will calculate WP" + i + "");
  42.             for (int j = 0; j < length; j++) {
  43.  
  44.                 for (int k = 0; k < length; k++) {
  45.                     inter[i][k] += weights[k][j] * s[i][j];
  46.                 }
  47.             }
  48.             for (int p = 0; p < length; p++) {
  49.                 inter[i][p] = inter[i][p] / (Math.abs(inter[i][p]));
  50.             }
  51.             System.out.println("Output for pattern " + (i + 1) + " is: " + Arrays.toString(inter[i]));
  52.         }
  53.     }
  54. }
  55.  
  56.  
  57. /*
  58. bash-4.1$ javac HopfieldNetwork.java
  59. bash-4.1$ java HopfieldNetwork
  60. Enter the number of input patterns:
  61. 3
  62. Enter the length of the inputs:
  63. 6
  64. Enter the pattern for s1:
  65. 1 1 1 -1 -1 -1
  66. Enter the pattern for s2:
  67. -1 -1 -1 1 1 1
  68. Enter the pattern for s3:
  69. 1 1 -1 -1 1 1
  70. Making the diagonal elements as 0:
  71. The weight matrix is:
  72. [0, 3, 1, -3, -1, -1]
  73. [3, 0, 1, -3, -1, -1]
  74. [1, 1, 0, -1, -3, -3]
  75. [-3, -3, -1, 0, 1, 1]
  76. [-1, -1, -3, 1, 0, 3]
  77. [-1, -1, -3, 1, 3, 0]
  78. Now we will calculate WP0
  79. Output for pattern 1 is: [1, 1, 1, -1, -1, -1]
  80. Now we will calculate WP1
  81. Output for pattern 2 is: [-1, -1, -1, 1, 1, 1]
  82. Now we will calculate WP2
  83. Output for pattern 3 is: [1, 1, -1, -1, 1, 1]
  84.  
  85.  
  86.  
  87.  
  88. bash-4.1$ java HopfieldNetwork
  89. Enter the number of input patterns:
  90. 4
  91. Enter the length of the inputs:
  92. 4
  93. Enter the pattern for s1:
  94. 1 -1 1 -1
  95. Enter the pattern for s2:
  96. -1 1 -1 1
  97. Enter the pattern for s3:
  98. 1 1 -1 -1
  99. Enter the pattern for s4:
  100. -1 -1 1 1
  101. Making the diagonal elements as 0:
  102. The weight matrix is:
  103. [0, 0, 0, -4]
  104. [0, 0, -4, 0]
  105. [0, -4, 0, 0]
  106. [-4, 0, 0, 0]
  107. Now we will calculate WP0
  108. Output for pattern 1 is: [1, -1, 1, -1]
  109. Now we will calculate WP1
  110. Output for pattern 2 is: [-1, 1, -1, 1]
  111. Now we will calculate WP2
  112. Output for pattern 3 is: [1, 1, -1, -1]
  113. Now we will calculate WP3
  114. Output for pattern 4 is: [-1, -1, 1, 1]
  115.  
  116.  
  117.  
  118. */
Add Comment
Please, Sign In to add comment