fork download
  1. import numpy as np
  2. from itertools import product
  3.  
  4. # Parameters
  5. lower_threshold = 0.8
  6. upper_threshold = 1.2
  7. learning_rate = 0.1
  8. max_iterations = 1000
  9.  
  10. # Inputs for the two-variable logic tables
  11. inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  12.  
  13. # Generate all 16 possible truth tables for 2 inputs
  14. all_truth_tables = list(product([0, 1], repeat=4)) # 16 combinations of [0, 1, 1, 0] etc.
  15.  
  16. # Activation function
  17. def activation_function(weighted_sum):
  18. return 1 if lower_threshold < weighted_sum < upper_threshold else 0
  19.  
  20. # Train the network for each truth table
  21. for truth_table_idx, targets in enumerate(all_truth_tables):
  22. print(f"\nTraining for Truth Table {truth_table_idx + 1}: {targets}")
  23.  
  24. # Initialize weights
  25. weights_input_hidden = np.random.rand(2, 2) # 2 input neurons to 2 hidden neurons
  26. weights_hidden_output = np.random.rand(2) # 2 hidden neurons to 1 output neuron
  27.  
  28. # Track initial weights
  29. start_weights_input_hidden = weights_input_hidden.copy()
  30. start_weights_hidden_output = weights_hidden_output.copy()
  31.  
  32. epoch = 0
  33. network_trained = False
  34.  
  35. while epoch < max_iterations:
  36. epoch += 1
  37. all_correct = True # Flag to track if all outputs are correct
  38.  
  39. for input_vector, target in zip(inputs, targets):
  40. # Forward pass
  41. hidden_input = np.dot(input_vector, weights_input_hidden) # Input -> Hidden
  42. hidden_output = np.array([activation_function(h) for h in hidden_input]) # Hidden activations
  43.  
  44. final_input = np.dot(hidden_output, weights_hidden_output) # Hidden -> Output
  45. output = activation_function(final_input) # Output activation
  46.  
  47. # Error calculation
  48. error = target - output
  49.  
  50. # Backpropagation and weight update if error exists
  51. if error != 0:
  52. all_correct = False
  53.  
  54. # Update weights for Hidden -> Output
  55. weights_hidden_output += learning_rate * error * hidden_output
  56.  
  57. # Update weights for Input -> Hidden
  58. for i in range(2): # Loop over hidden neurons
  59. if hidden_output[i] > 0: # Only update weights if neuron is active
  60. weights_input_hidden[:, i] += learning_rate * error * input_vector
  61.  
  62. if all_correct:
  63. network_trained = True
  64. break # Stop training if all outputs are correct
  65.  
  66. # Print results for the truth table
  67. if network_trained:
  68. print(f"The network learned the truth table correctly after {epoch} iterations.")
  69. else:
  70. print(f"The network failed to learn the truth table after {epoch} iterations.")
  71.  
  72. # Test the trained network
  73. print("\nTesting the trained network:")
  74. for input_vector, target in zip(inputs, targets):
  75. hidden_input = np.dot(input_vector, weights_input_hidden) # Input -> Hidden
  76. hidden_output = np.array([activation_function(h) for h in hidden_input]) # Hidden neuron activations
  77.  
  78. final_input = np.dot(hidden_output, weights_hidden_output) # Hidden -> Output
  79. output = activation_function(final_input) # Activation of output neuron
  80. print(f"Input: {input_vector}, Target: {target}, Output: {output}")
  81.  
  82. # Print weights for reference
  83. print("\nInitial Weights (Input -> Hidden):")
  84. print(start_weights_input_hidden)
  85.  
  86. print("\nInitial Weights (Hidden -> Output):")
  87. print(start_weights_hidden_output)
  88.  
  89. print("\nFinal Weights (Input -> Hidden):")
  90. print(weights_input_hidden)
  91.  
  92. print("\nFinal Weights (Hidden -> Output):")
  93. print(weights_hidden_output)
  94. print("-----------------------------------------------------------")
  95.  
Success #stdin #stdout 1.6s 29016KB
stdin
Standard input is empty
stdout
Training for Truth Table 1: (0, 0, 0, 0)
The network learned the truth table correctly after 1 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

Initial Weights (Input -> Hidden):
[[0.53337239 0.19766255]
 [0.18377121 0.0049292 ]]

Initial Weights (Hidden -> Output):
[0.34651997 0.79652266]

Final Weights (Input -> Hidden):
[[0.53337239 0.19766255]
 [0.18377121 0.0049292 ]]

Final Weights (Hidden -> Output):
[0.34651997 0.79652266]
-----------------------------------------------------------

Training for Truth Table 2: (0, 0, 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: 0, Output: 0
Input: [1 0], Target: 0, Output: 0
Input: [1 1], Target: 1, Output: 0

Initial Weights (Input -> Hidden):
[[0.9394361  0.00259522]
 [0.5438311  0.95889809]]

Initial Weights (Hidden -> Output):
[0.79110817 0.60984493]

Final Weights (Input -> Hidden):
[[0.9394361  0.20259522]
 [0.5438311  1.05889809]]

Final Weights (Hidden -> Output):
[0.79110817 0.70984493]
-----------------------------------------------------------

Training for Truth Table 3: (0, 0, 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: 0, Output: 0
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 0, Output: 0

Initial Weights (Input -> Hidden):
[[0.84660471 0.91258842]
 [0.25411337 0.35572475]]

Initial Weights (Hidden -> Output):
[0.47333493 0.27844345]

Final Weights (Input -> Hidden):
[[0.94660471 1.01258842]
 [0.25411337 0.35572475]]

Final Weights (Hidden -> Output):
[0.57333493 0.37844345]
-----------------------------------------------------------

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: 0
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 1, Output: 0

Initial Weights (Input -> Hidden):
[[0.94951032 0.04381053]
 [0.78466125 0.65932941]]

Initial Weights (Hidden -> Output):
[0.97632696 0.16880446]

Final Weights (Input -> Hidden):
[[0.94951032 0.04381053]
 [0.78466125 0.65932941]]

Final Weights (Hidden -> Output):
[0.97632696 0.16880446]
-----------------------------------------------------------

Training for Truth Table 5: (0, 1, 0, 0)
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: 0
Input: [1 1], Target: 0, Output: 0

Initial Weights (Input -> Hidden):
[[0.72870973 0.57744179]
 [0.18792842 0.25623007]]

Initial Weights (Hidden -> Output):
[0.17060548 0.95259139]

Final Weights (Input -> Hidden):
[[0.62870973 0.47744179]
 [0.08792842 0.15623007]]

Final Weights (Hidden -> Output):
[0.07060548 0.85259139]
-----------------------------------------------------------

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: 0
Input: [1 1], Target: 1, Output: 1

Initial Weights (Input -> Hidden):
[[0.02712117 0.46479367]
 [0.48772063 0.63494348]]

Initial Weights (Hidden -> Output):
[0.74173405 0.9224191 ]

Final Weights (Input -> Hidden):
[[0.02712117 0.46479367]
 [0.48772063 0.63494348]]

Final Weights (Hidden -> Output):
[0.74173405 0.9224191 ]
-----------------------------------------------------------

Training for Truth Table 7: (0, 1, 1, 0)
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: 0, Output: 0

Initial Weights (Input -> Hidden):
[[0.14380847 0.54878651]
 [0.47093973 0.59235292]]

Initial Weights (Hidden -> Output):
[0.15596722 0.84988594]

Final Weights (Input -> Hidden):
[[0.14380847 0.44878651]
 [0.47093973 0.49235292]]

Final Weights (Hidden -> Output):
[0.15596722 0.74988594]
-----------------------------------------------------------

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

Initial Weights (Input -> Hidden):
[[0.32685979 0.0444049 ]
 [0.98755163 0.84283821]]

Initial Weights (Hidden -> Output):
[0.62318282 0.9962772 ]

Final Weights (Input -> Hidden):
[[0.32685979 0.1444049 ]
 [1.28755163 1.24283821]]

Final Weights (Hidden -> Output):
[0.92318282 1.3962772 ]
-----------------------------------------------------------

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

Initial Weights (Input -> Hidden):
[[0.30587349 0.61635109]
 [0.72196745 0.3441004 ]]

Initial Weights (Hidden -> Output):
[0.81931943 0.37056072]

Final Weights (Input -> Hidden):
[[0.20587349 0.51635109]
 [0.62196745 0.2441004 ]]

Final Weights (Hidden -> Output):
[0.71931943 0.27056072]
-----------------------------------------------------------

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: 0

Initial Weights (Input -> Hidden):
[[0.9792277  0.29655232]
 [0.78141684 0.87444476]]

Initial Weights (Hidden -> Output):
[0.1636738 0.414314 ]

Final Weights (Input -> Hidden):
[[0.9792277  0.39655232]
 [0.78141684 0.97444476]]

Final Weights (Hidden -> Output):
[0.1636738 0.514314 ]
-----------------------------------------------------------

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: 0
Input: [1 1], Target: 0, Output: 0

Initial Weights (Input -> Hidden):
[[0.57556625 0.19780532]
 [0.61585713 0.57510579]]

Initial Weights (Hidden -> Output):
[0.87851638 0.05614224]

Final Weights (Input -> Hidden):
[[0.47556625 0.19780532]
 [0.51585713 0.57510579]]

Final Weights (Hidden -> Output):
[0.77851638 0.05614224]
-----------------------------------------------------------

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

Initial Weights (Input -> Hidden):
[[0.54237599 0.71597308]
 [0.33645771 0.42573742]]

Initial Weights (Hidden -> Output):
[0.84321805 0.78660935]

Final Weights (Input -> Hidden):
[[0.64237599 0.81597308]
 [0.43645771 0.52573742]]

Final Weights (Hidden -> Output):
[0.94321805 0.88660935]
-----------------------------------------------------------

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: 0
Input: [1 0], Target: 0, Output: 0
Input: [1 1], Target: 0, Output: 0

Initial Weights (Input -> Hidden):
[[0.88628634 0.32485996]
 [0.06377701 0.54976519]]

Initial Weights (Hidden -> Output):
[0.36397368 0.50184017]

Final Weights (Input -> Hidden):
[[ 0.78628634  0.22485996]
 [-0.03622299  0.44976519]]

Final Weights (Hidden -> Output):
[0.26397368 0.40184017]
-----------------------------------------------------------

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: 0
Input: [1 1], Target: 1, Output: 0

Initial Weights (Input -> Hidden):
[[0.4428625  0.15583271]
 [0.25532406 0.67217989]]

Initial Weights (Hidden -> Output):
[0.19247774 0.03735062]

Final Weights (Input -> Hidden):
[[0.4428625  0.35583271]
 [0.25532406 1.27217989]]

Final Weights (Hidden -> Output):
[0.19247774 0.63735062]
-----------------------------------------------------------

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: 1
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 0, Output: 0

Initial Weights (Input -> Hidden):
[[0.10547552 0.97784461]
 [0.21363979 0.86768973]]

Initial Weights (Hidden -> Output):
[0.01397043 0.91203758]

Final Weights (Input -> Hidden):
[[0.10547552 0.97784461]
 [0.21363979 0.86768973]]

Final Weights (Hidden -> Output):
[0.01397043 0.91203758]
-----------------------------------------------------------

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: 1
Input: [1 0], Target: 1, Output: 0
Input: [1 1], Target: 1, Output: 0

Initial Weights (Input -> Hidden):
[[0.18041178 0.32327852]
 [0.32236654 0.88078795]]

Initial Weights (Hidden -> Output):
[0.70491173 0.72469569]

Final Weights (Input -> Hidden):
[[0.18041178 0.32327852]
 [0.32236654 0.98078795]]

Final Weights (Hidden -> Output):
[0.70491173 0.82469569]
-----------------------------------------------------------