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
  23. weights_input_to_hidden = np.random.rand(2) # Weights from N1, N2 → N3
  24. weights_hidden_to_output = np.random.rand(1) # Weight from N3 → N4
  25. weights_input_to_output = np.random.rand(2) # Weights from N1, N2 → N4
  26.  
  27. epoch = 0
  28. network_trained = False
  29.  
  30. while epoch < max_iterations:
  31. epoch += 1
  32. all_correct = True # Flag to track if all outputs are correct
  33.  
  34. for input_vector, target in zip(inputs, targets):
  35. N1, N2 = input_vector # Input neurons
  36.  
  37. # Forward pass
  38. N3_input = np.dot(input_vector, weights_input_to_hidden) # Weighted sum for N3
  39. N3 = activation_function(N3_input) # Output of N3 (hidden neuron)
  40.  
  41. N4_input = (N3 * weights_hidden_to_output[0] + # From hidden neuron
  42. N1 * weights_input_to_output[0] + # Direct from N1
  43. N2 * weights_input_to_output[1]) # Direct from N2
  44. N4 = activation_function(N4_input) # Output of N4 (final output)
  45.  
  46. # Error calculation
  47. error = target - N4
  48.  
  49. # Backpropagation and weight updates
  50. if error != 0:
  51. all_correct = False
  52.  
  53. # Update weights for N3 → N4
  54. weights_hidden_to_output[0] += learning_rate * error * N3
  55.  
  56. # Update weights for N1, N2 → N4
  57. weights_input_to_output[0] += learning_rate * error * N1
  58. weights_input_to_output[1] += learning_rate * error * N2
  59.  
  60. # Update weights for N1, N2 → N3
  61. weights_input_to_hidden += learning_rate * error * np.array([N1, N2])
  62.  
  63. if all_correct:
  64. network_trained = True
  65. break # Stop training if all outputs are correct
  66.  
  67. # Print results for the truth table
  68. if network_trained:
  69. print(f"The network learned the truth table correctly after {epoch} iterations.")
  70. else:
  71. print(f"The network failed to learn the truth table after {epoch} iterations.")
  72.  
  73. # Test the trained network
  74. print("\nTesting the trained network:")
  75. for input_vector, target in zip(inputs, targets):
  76. N1, N2 = input_vector
  77.  
  78. # Forward pass
  79. N3_input = np.dot(input_vector, weights_input_to_hidden)
  80. N3 = activation_function(N3_input)
  81.  
  82. N4_input = (N3 * weights_hidden_to_output[0] +
  83. N1 * weights_input_to_output[0] +
  84. N2 * weights_input_to_output[1])
  85. N4 = activation_function(N4_input)
  86.  
  87. print(f"Input: {input_vector}, Target: {target}, Output: {N4}")
  88. print("-----------------------------------------------------------")
  89.  
Success #stdin #stdout 1.22s 28816KB
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
-----------------------------------------------------------

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

Training for Truth Table 3: (0, 0, 1, 0)
The network learned the truth table correctly after 5 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 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: 1, Output: 1
Input: [1 1], Target: 1, Output: 1
-----------------------------------------------------------

Training for Truth Table 5: (0, 1, 0, 0)
The network learned the truth table correctly after 4 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 5 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: 1
Input: [1 0], Target: 1, Output: 0
Input: [1 1], Target: 1, Output: 0
-----------------------------------------------------------

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