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 with dynamic thresholding
  9. def activation_function(weighted_sum, dynamic_threshold):
  10. return 1 if weighted_sum > dynamic_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(3) # Weights from N1, N2, bias → N3
  24. weights_hidden_to_output = np.random.rand(2) # Weights from N3, bias → N4
  25. weights_input_to_output = np.random.rand(3) # Weights from N1, N2, bias → 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. bias = 1 # Bias input
  37.  
  38. # Forward pass
  39. N3_input = np.dot(np.append(input_vector, bias), weights_input_to_hidden) # Weighted sum for N3
  40. N3 = activation_function(N3_input, dynamic_threshold=0.5) # Output of N3 (hidden neuron)
  41.  
  42. N4_input = (N3 * weights_hidden_to_output[0] + # From hidden neuron
  43. bias * weights_hidden_to_output[1] + # Bias contribution
  44. np.dot(np.append(input_vector, bias), weights_input_to_output)) # Direct connections
  45. N4 = activation_function(N4_input, dynamic_threshold=0.5) # Output of N4 (final output)
  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 N3 → N4
  55. weights_hidden_to_output[0] += learning_rate * error * N3
  56. weights_hidden_to_output[1] += learning_rate * error * bias
  57.  
  58. # Update weights for N1, N2, bias → N4
  59. weights_input_to_output += learning_rate * error * np.append(input_vector, bias)
  60.  
  61. # Update weights for N1, N2, bias → N3
  62. weights_input_to_hidden += learning_rate * error * np.append(input_vector, bias)
  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. bias = 1
  79.  
  80. # Forward pass
  81. N3_input = np.dot(np.append(input_vector, bias), weights_input_to_hidden)
  82. N3 = activation_function(N3_input, dynamic_threshold=0.5)
  83.  
  84. N4_input = (N3 * weights_hidden_to_output[0] +
  85. bias * weights_hidden_to_output[1] +
  86. np.dot(np.append(input_vector, bias), weights_input_to_output))
  87. N4 = activation_function(N4_input, dynamic_threshold=0.5)
  88.  
  89. print(f"Input: {input_vector}, Target: {target}, Output: {N4}")
  90. print("-----------------------------------------------------------")
  91.  
Success #stdin #stdout 0.45s 28812KB
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 5 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 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: 1, Output: 1
-----------------------------------------------------------

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

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

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

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

Testing the trained network:
Input: [0 0], Target: 1, Output: 1
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 learned the truth table correctly after 16 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 1
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 learned the truth table correctly after 7 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 1
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 learned the truth table correctly after 5 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 1
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 learned the truth table correctly after 7 iterations.

Testing the trained network:
Input: [0 0], Target: 1, Output: 1
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 learned the truth table correctly after 5 iterations.

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

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

Testing the trained network:
Input: [0 0], Target: 1, Output: 1
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 learned the truth table correctly after 1 iterations.

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