fork download
  1. import numpy as np
  2. from itertools import product
  3.  
  4. # Parameters
  5. learning_rate = 0.1
  6. max_iterations = 1000
  7.  
  8. # Activation function: Simple thresholding
  9. def activation_function(weighted_sum, lower_threshold=0.8, upper_threshold=1.2):
  10. return 1 if lower_threshold < weighted_sum < upper_threshold else 0
  11.  
  12. # Inputs for the two-variable logic tables
  13. inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  14.  
  15. # Generate all 16 possible truth tables for 2 inputs
  16. all_truth_tables = list(product([0, 1], repeat=4)) # 16 combinations of binary outputs
  17.  
  18. # Train the network for each truth table
  19. for truth_table_idx, targets in enumerate(all_truth_tables):
  20. print(f"\nTraining for Truth Table {truth_table_idx + 1}: {targets}")
  21.  
  22. # Initialize weights for fully connected network
  23. weights_N1_to_N3 = np.random.rand() # Weight from N1 to N3
  24. weights_N2_to_N3 = np.random.rand() # Weight from N2 to N3
  25. weights_N1_to_N4 = np.random.rand() # Weight from N1 to N4
  26. weights_N2_to_N4 = np.random.rand() # Weight from N2 to N4
  27. weights_N3_to_N4 = np.random.rand() # Weight from N3 to N4
  28.  
  29. epoch = 0
  30. network_trained = False
  31.  
  32. while epoch < max_iterations:
  33. epoch += 1
  34. all_correct = True # Flag to track if all outputs are correct
  35.  
  36. for input_vector, target in zip(inputs, targets):
  37. N1, N2 = input_vector # Input neurons
  38.  
  39. # Forward pass
  40. N3_input = N1 * weights_N1_to_N3 + N2 * weights_N2_to_N3
  41. N3 = activation_function(N3_input) # Output of N3
  42.  
  43. N4_input = (N1 * weights_N1_to_N4 +
  44. N2 * weights_N2_to_N4 +
  45. N3 * weights_N3_to_N4)
  46. N4 = activation_function(N4_input) # Output of N4 (final output)
  47.  
  48. # Error calculation
  49. error = target - N4
  50.  
  51. # Backpropagation and weight updates
  52. if error != 0:
  53. all_correct = False
  54.  
  55. # Update weights connecting to N4
  56. weights_N1_to_N4 += learning_rate * error * N1
  57. weights_N2_to_N4 += learning_rate * error * N2
  58. weights_N3_to_N4 += learning_rate * error * N3
  59.  
  60. # Update weights connecting to N3
  61. weights_N1_to_N3 += learning_rate * error * N1
  62. weights_N2_to_N3 += learning_rate * error * N2
  63.  
  64. if all_correct:
  65. network_trained = True
  66. break # Stop training if all outputs are correct
  67.  
  68. # Print results for the truth table
  69. if network_trained:
  70. print(f"The network learned the truth table correctly after {epoch} iterations.")
  71. else:
  72. print(f"The network failed to learn the truth table after {epoch} iterations.")
  73.  
  74. # Test the trained network
  75. print("\nTesting the trained network:")
  76. for input_vector, target in zip(inputs, targets):
  77. N1, N2 = input_vector
  78.  
  79. # Forward pass
  80. N3_input = N1 * weights_N1_to_N3 + N2 * weights_N2_to_N3
  81. N3 = activation_function(N3_input)
  82.  
  83. N4_input = (N1 * weights_N1_to_N4 +
  84. N2 * weights_N2_to_N4 +
  85. N3 * weights_N3_to_N4)
  86. N4 = activation_function(N4_input)
  87.  
  88. print(f"Input: {input_vector}, Target: {target}, Output: {N4}")
  89. print("-----------------------------------------------------------")
  90.  
Success #stdin #stdout 1.51s 28872KB
stdin
Standard input is empty
stdout
Training for Truth Table 1: (0, 0, 0, 0)
The network learned the truth table correctly after 3 iterations.

Testing the trained network:
Input: [0 0], Target: 0, Output: 0
Input: [0 1], Target: 0, Output: 0
Input: [1 0], Target: 0, Output: 0
Input: [1 1], Target: 0, Output: 0
-----------------------------------------------------------

Training for Truth Table 2: (0, 0, 0, 1)
The network learned the truth table correctly after 2 iterations.

Testing the trained network:
Input: [0 0], Target: 0, Output: 0
Input: [0 1], Target: 0, Output: 0
Input: [1 0], Target: 0, Output: 0
Input: [1 1], Target: 1, Output: 1
-----------------------------------------------------------

Training for Truth Table 3: (0, 0, 1, 0)
The network learned the truth table correctly after 8 iterations.

Testing the trained network:
Input: [0 0], Target: 0, Output: 0
Input: [0 1], Target: 0, Output: 0
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 0, Output: 0
-----------------------------------------------------------

Training for Truth Table 4: (0, 0, 1, 1)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 0, Output: 0
Input: [0 1], Target: 0, Output: 1
Input: [1 0], Target: 1, Output: 0
Input: [1 1], Target: 1, Output: 0
-----------------------------------------------------------

Training for Truth Table 5: (0, 1, 0, 0)
The network learned the truth table correctly after 10 iterations.

Testing the trained network:
Input: [0 0], Target: 0, Output: 0
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 0, Output: 0
Input: [1 1], Target: 0, Output: 0
-----------------------------------------------------------

Training for Truth Table 6: (0, 1, 0, 1)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 0, Output: 0
Input: [0 1], Target: 1, Output: 0
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 1, Output: 0
-----------------------------------------------------------

Training for Truth Table 7: (0, 1, 1, 0)
The network learned the truth table correctly after 2 iterations.

Testing the trained network:
Input: [0 0], Target: 0, Output: 0
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 0, Output: 0
-----------------------------------------------------------

Training for Truth Table 8: (0, 1, 1, 1)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 0, Output: 0
Input: [0 1], Target: 1, Output: 0
Input: [1 0], Target: 1, Output: 0
Input: [1 1], Target: 1, Output: 0
-----------------------------------------------------------

Training for Truth Table 9: (1, 0, 0, 0)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 0
Input: [0 1], Target: 0, Output: 0
Input: [1 0], Target: 0, Output: 0
Input: [1 1], Target: 0, Output: 0
-----------------------------------------------------------

Training for Truth Table 10: (1, 0, 0, 1)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 0
Input: [0 1], Target: 0, Output: 0
Input: [1 0], Target: 0, Output: 0
Input: [1 1], Target: 1, Output: 1
-----------------------------------------------------------

Training for Truth Table 11: (1, 0, 1, 0)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 0
Input: [0 1], Target: 0, Output: 0
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 0, Output: 0
-----------------------------------------------------------

Training for Truth Table 12: (1, 0, 1, 1)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 0
Input: [0 1], Target: 0, Output: 0
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 1, Output: 1
-----------------------------------------------------------

Training for Truth Table 13: (1, 1, 0, 0)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 0
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 0, Output: 0
Input: [1 1], Target: 0, Output: 0
-----------------------------------------------------------

Training for Truth Table 14: (1, 1, 0, 1)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 0
Input: [0 1], Target: 1, Output: 0
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 1, Output: 0
-----------------------------------------------------------

Training for Truth Table 15: (1, 1, 1, 0)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 0
Input: [0 1], Target: 1, Output: 0
Input: [1 0], Target: 1, Output: 0
Input: [1 1], Target: 0, Output: 0
-----------------------------------------------------------

Training for Truth Table 16: (1, 1, 1, 1)
The network failed to learn the truth table after 1000 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 0
Input: [0 1], Target: 1, Output: 0
Input: [1 0], Target: 1, Output: 0
Input: [1 1], Target: 1, Output: 0
-----------------------------------------------------------