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 a 4x4 weight matrix (fully connected)
  23. weights = np.random.rand(4, 4)
  24.  
  25. epoch = 0
  26. network_trained = False
  27.  
  28. while epoch < max_iterations:
  29. epoch += 1
  30. all_correct = True # Flag to track if all outputs are correct
  31.  
  32. for input_vector, target in zip(inputs, targets):
  33. # Set inputs (N1 and N2)
  34. N1, N2 = input_vector
  35.  
  36. # Forward pass
  37. # N3 and N4 depend on all neurons, including themselves
  38. neurons = np.array([N1, N2, 0, 0]) # Initial values for all neurons
  39.  
  40. for i in range(2, 4): # Calculate N3 and N4
  41. weighted_sum = np.dot(weights[:, i], neurons)
  42. neurons[i] = activation_function(weighted_sum)
  43.  
  44. # Output is N4
  45. N4 = neurons[3]
  46.  
  47. # Error calculation
  48. error = target - N4
  49.  
  50. # Backpropagation and weight updates
  51. if error != 0:
  52. all_correct = False
  53.  
  54. # Update weights for all connections
  55. for i in range(4):
  56. for j in range(4):
  57. weights[i, j] += learning_rate * error * neurons[i]
  58.  
  59. if all_correct:
  60. network_trained = True
  61. break # Stop training if all outputs are correct
  62.  
  63. # Print results for the truth table
  64. if network_trained:
  65. print(f"The network learned the truth table correctly after {epoch} iterations.")
  66. else:
  67. print(f"The network failed to learn the truth table after {epoch} iterations.")
  68.  
  69. # Test the trained network
  70. print("\nTesting the trained network:")
  71. for input_vector, target in zip(inputs, targets):
  72. # Set inputs (N1 and N2)
  73. N1, N2 = input_vector
  74.  
  75. # Forward pass
  76. neurons = np.array([N1, N2, 0, 0]) # Initial values for all neurons
  77.  
  78. for i in range(2, 4): # Calculate N3 and N4
  79. weighted_sum = np.dot(weights[:, i], neurons)
  80. neurons[i] = activation_function(weighted_sum)
  81.  
  82. N4 = neurons[3] # Final output
  83. print(f"Input: {input_vector}, Target: {target}, Output: {N4}")
  84. print("-----------------------------------------------------------")
  85.  
Success #stdin #stdout 2.63s 28736KB
stdin
Standard input is empty
stdout
Training for Truth Table 1: (0, 0, 0, 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: 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 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: 1, Output: 1
-----------------------------------------------------------

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

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

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