fork download
  1. import numpy as np
  2.  
  3. # Parameters
  4. learning_rate = 0.1
  5. max_iterations = 1000
  6. lower_threshold = 0.8 # Lower threshold for activation
  7. upper_threshold = 1.2 # Upper threshold for activation
  8.  
  9. # Threshold activation function with two thresholds
  10. def activation_function(weighted_sum):
  11. if weighted_sum >= upper_threshold:
  12. return 1
  13. else:
  14. return 0
  15.  
  16. # Generate all 16 possible truth tables for 2 inputs
  17. inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  18. all_targets = [
  19. [0, 0, 0, 0], # 0
  20. [0, 0, 0, 1], # 1
  21. [0, 0, 1, 0], # 2
  22. [0, 0, 1, 1], # 3
  23. [0, 1, 0, 0], # 4
  24. [0, 1, 0, 1], # 5
  25. [0, 1, 1, 0], # 6
  26. [0, 1, 1, 1], # 7
  27. [1, 0, 0, 0], # 8
  28. [1, 0, 0, 1], # 9
  29. [1, 0, 1, 0], # 10
  30. [1, 0, 1, 1], # 11
  31. [1, 1, 0, 0], # 12
  32. [1, 1, 0, 1], # 13
  33. [1, 1, 1, 0], # 14
  34. [1, 1, 1, 1], # 15
  35. ]
  36.  
  37. # Initialize weights for the neurons in a smaller range
  38. weights_input_to_hidden = np.random.rand(3) * 0.1 # 2 inputs + 1 bias → 1 hidden neuron
  39. weights_hidden_to_output = np.random.rand(2) * 0.1 # 1 hidden neuron + 1 bias → 1 output neuron
  40.  
  41. # Training loop
  42. network_trained = False
  43. for target in all_targets: # Train for all possible tables
  44. epoch = 0
  45. while epoch < max_iterations:
  46. epoch += 1
  47. all_correct = True # Flag to track if all outputs are correct
  48.  
  49. for input_vector, expected_target in zip(inputs, target):
  50. N1, N2 = input_vector
  51. bias = 1 # Bias input
  52.  
  53. # Forward pass (hidden layer)
  54. N3_input = np.dot(np.append(input_vector, bias), weights_input_to_hidden) # Hidden layer sum
  55. N3 = activation_function(N3_input) # Output of the hidden neuron
  56.  
  57. # Output layer
  58. N4_input = (N3 * weights_hidden_to_output[0] + bias * weights_hidden_to_output[1]) # Weighted sum to output
  59. N4 = activation_function(N4_input) # Output neuron decision
  60.  
  61. # Error calculation
  62. error = expected_target - N4
  63.  
  64. # If there is an error, update the weights
  65. if error != 0:
  66. all_correct = False
  67.  
  68. # Update weights for hidden to output neuron
  69. weights_hidden_to_output[0] += learning_rate * error * N3
  70. weights_hidden_to_output[1] += learning_rate * error * bias
  71.  
  72. # Update weights for input to hidden neuron
  73. weights_input_to_hidden += learning_rate * error * N3 * np.append(input_vector, bias)
  74.  
  75. if all_correct:
  76. network_trained = True
  77. break
  78.  
  79. # Results
  80. if network_trained:
  81. print(f"The network learned the truth tables correctly after {epoch} iterations.")
  82. else:
  83. print(f"The network failed to learn the truth tables after {epoch} iterations.")
  84.  
  85. # Testing the trained network
  86. print("\nTesting the trained network:")
  87. for target_index, target in enumerate(all_targets):
  88. print(f"Testing Table {target_index}:")
  89. for input_vector, expected_target in zip(inputs, target):
  90. N1, N2 = input_vector
  91. bias = 1
  92.  
  93. # Forward pass (hidden layer)
  94. N3_input = np.dot(np.append(input_vector, bias), weights_input_to_hidden)
  95. N3 = activation_function(N3_input)
  96.  
  97. # Output layer
  98. N4_input = (N3 * weights_hidden_to_output[0] + bias * weights_hidden_to_output[1])
  99. N4 = activation_function(N4_input)
  100.  
  101. print(f"Input: {input_vector}, Target: {expected_target}, Output: {N4}")
  102.  
Success #stdin #stdout 1.99s 28816KB
stdin
Standard input is empty
stdout
The network learned the truth tables correctly after 2 iterations.

Testing the trained network:
Testing Table 0:
Input: [0 0], Target: 0, Output: 1
Input: [0 1], Target: 0, Output: 1
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 0, Output: 1
Testing Table 1:
Input: [0 0], Target: 0, Output: 1
Input: [0 1], Target: 0, Output: 1
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 1, Output: 1
Testing Table 2:
Input: [0 0], Target: 0, Output: 1
Input: [0 1], Target: 0, Output: 1
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 0, Output: 1
Testing Table 3:
Input: [0 0], Target: 0, Output: 1
Input: [0 1], Target: 0, Output: 1
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 1, Output: 1
Testing Table 4:
Input: [0 0], Target: 0, Output: 1
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 0, Output: 1
Testing Table 5:
Input: [0 0], Target: 0, Output: 1
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 1, Output: 1
Testing Table 6:
Input: [0 0], Target: 0, Output: 1
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 0, Output: 1
Testing Table 7:
Input: [0 0], Target: 0, Output: 1
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 1, Output: 1
Testing Table 8:
Input: [0 0], Target: 1, Output: 1
Input: [0 1], Target: 0, Output: 1
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 0, Output: 1
Testing Table 9:
Input: [0 0], Target: 1, Output: 1
Input: [0 1], Target: 0, Output: 1
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 1, Output: 1
Testing Table 10:
Input: [0 0], Target: 1, Output: 1
Input: [0 1], Target: 0, Output: 1
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 0, Output: 1
Testing Table 11:
Input: [0 0], Target: 1, Output: 1
Input: [0 1], Target: 0, Output: 1
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 1, Output: 1
Testing Table 12:
Input: [0 0], Target: 1, Output: 1
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 0, Output: 1
Testing Table 13:
Input: [0 0], Target: 1, Output: 1
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 0, Output: 1
Input: [1 1], Target: 1, Output: 1
Testing Table 14:
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: 1
Testing Table 15:
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