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. # Sigmoid activation function
  9. def sigmoid(x):
  10. return 1 / (1 + np.exp(-x))
  11.  
  12. def activation_function(weighted_sum):
  13. return sigmoid(weighted_sum)
  14.  
  15. # Inputs for the two-variable logic tables
  16. inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  17.  
  18. # Generate all 16 possible truth tables for 2 inputs
  19. all_truth_tables = list(product([0, 1], repeat=4)) # 16 combinations of binary outputs
  20.  
  21. # Train the network for each truth table
  22. for truth_table_idx, targets in enumerate(all_truth_tables):
  23. print(f"\nTraining for Truth Table {truth_table_idx + 1}: {targets}")
  24.  
  25. # Initialize weights with smaller values
  26. weights_input_hidden = np.random.uniform(-0.5, 0.5, (2, 4)) # 2 input neurons to 4 hidden neurons
  27. weights_hidden_output = np.random.uniform(-0.5, 0.5, 4) # 4 hidden neurons to 1 output neuron
  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. # Forward pass
  38. hidden_input = np.dot(input_vector, weights_input_hidden) # Input -> Hidden
  39. hidden_output = activation_function(hidden_input) # Hidden neuron activations
  40.  
  41. final_input = np.dot(hidden_output, weights_hidden_output) # Hidden -> Output
  42. output = activation_function(final_input) # Sigmoid activation
  43. output = 1 if output > 0.5 else 0 # Threshold for binary output
  44.  
  45. # Error calculation
  46. error = target - output
  47.  
  48. # Backpropagation and weight updates
  49. if error != 0:
  50. all_correct = False
  51.  
  52. # Update weights for Hidden -> Output
  53. weights_hidden_output += learning_rate * error * hidden_output
  54.  
  55. # Update weights for Input -> Hidden
  56. delta_hidden = weights_hidden_output * error * hidden_output * (1 - hidden_output)
  57. for i in range(4): # Loop over hidden neurons
  58. weights_input_hidden[:, i] += learning_rate * delta_hidden[i] * input_vector
  59.  
  60. if all_correct:
  61. network_trained = True
  62. break # Stop training if all outputs are correct
  63.  
  64. # Print results for the truth table
  65. if network_trained:
  66. print(f"The network learned the truth table correctly after {epoch} iterations.")
  67. else:
  68. print(f"The network failed to learn the truth table after {epoch} iterations.")
  69.  
  70. # Test the trained network
  71. print("\nTesting the trained network:")
  72. for input_vector, target in zip(inputs, targets):
  73. hidden_input = np.dot(input_vector, weights_input_hidden) # Input -> Hidden
  74. hidden_output = activation_function(hidden_input) # Hidden activations
  75.  
  76. final_input = np.dot(hidden_output, weights_hidden_output) # Hidden -> Output
  77. output = activation_function(final_input) # Output activation
  78. output = 1 if output > 0.5 else 0 # Threshold for binary output
  79. print(f"Input: {input_vector}, Target: {target}, Output: {output}")
  80. print("-----------------------------------------------------------")
  81.  
Success #stdin #stdout 0.65s 28884KB
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 learned the truth table correctly after 25 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 52 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 20 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 32 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 learned the truth table correctly after 769 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 38 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 28 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 843 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 4 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 17 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 26 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 20 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 12 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 2 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
-----------------------------------------------------------