fork download
  1. import numpy as np
  2.  
  3. # Define inputs for all truth tables
  4. inputs = np.array([
  5. [0, 0],
  6. [0, 1],
  7. [1, 0],
  8. [1, 1]
  9. ])
  10.  
  11. # Define all possible truth tables as target outputs
  12. truth_tables = [
  13. [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 1, 1],
  14. [0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 1, 1],
  15. [1, 0, 0, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 0, 1, 1],
  16. [1, 1, 0, 0], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 1]
  17. ]
  18.  
  19. # Define the perceptron learning function
  20. def perceptron_learning(inputs, targets, learning_rate=0.1, max_epochs=1000):
  21. # Initialize weights and bias with random values (can be negative)
  22. weights = np.random.uniform(-1, 1, inputs.shape[1])
  23. bias = np.random.uniform(-1, 1)
  24.  
  25. for epoch in range(max_epochs):
  26. error_count = 0
  27. for i, input_vector in enumerate(inputs):
  28. # Compute the perceptron output
  29. linear_combination = np.dot(input_vector, weights) + bias
  30. output = 1 if linear_combination >= 0 else 0
  31.  
  32. # Calculate the error
  33. error = targets[i] - output
  34.  
  35. # Update weights and bias if there is an error
  36. if error != 0:
  37. weights += learning_rate * error * input_vector
  38. bias += learning_rate * error
  39. error_count += 1
  40.  
  41. # If no errors, learning is complete
  42. if error_count == 0:
  43. break
  44.  
  45. return weights, bias, epoch + 1
  46.  
  47. # Test perceptron for all truth tables
  48. results = []
  49. for idx, table in enumerate(truth_tables):
  50. print(f"\n=== Truth Table {idx + 1}: Targets = {table} ===")
  51. weights, bias, epochs = perceptron_learning(inputs, table)
  52.  
  53. # Test the final perceptron on inputs
  54. outputs = [1 if np.dot(input_vector, weights) + bias >= 0 else 0 for input_vector in inputs]
  55.  
  56. # Display results
  57. print(f"Final Weights: {weights}")
  58. print(f"Final Bias: {bias}")
  59. print(f"Epochs to Learn: {epochs}")
  60. print(f"Final Test Output: {outputs}")
  61.  
  62. # Store the results
  63. results.append({
  64. "truth_table": table,
  65. "weights": weights.tolist(),
  66. "bias": bias,
  67. "epochs": epochs,
  68. "outputs": outputs
  69. })
  70.  
Success #stdin #stdout 0.32s 29136KB
stdin
Standard input is empty
stdout
=== Truth Table 1: Targets = [0, 0, 0, 0] ===
Final Weights: [ 0.15578012 -0.45641446]
Final Bias: -0.20813010918884936
Epochs to Learn: 4
Final Test Output: [0, 0, 0, 0]

=== Truth Table 2: Targets = [0, 0, 0, 1] ===
Final Weights: [0.12232487 0.72891746]
Final Bias: -0.7607097774167211
Epochs to Learn: 7
Final Test Output: [0, 0, 0, 1]

=== Truth Table 3: Targets = [0, 0, 1, 0] ===
Final Weights: [ 0.53609976 -0.10153098]
Final Bias: -0.4713583453023711
Epochs to Learn: 7
Final Test Output: [0, 0, 1, 0]

=== Truth Table 4: Targets = [0, 0, 1, 1] ===
Final Weights: [ 0.24449993 -0.07375654]
Final Bias: -0.06413371591779254
Epochs to Learn: 8
Final Test Output: [0, 0, 1, 1]

=== Truth Table 5: Targets = [0, 1, 0, 0] ===
Final Weights: [-0.92811524  0.13763124]
Final Bias: -0.1366133650870029
Epochs to Learn: 9
Final Test Output: [0, 1, 0, 0]

=== Truth Table 6: Targets = [0, 1, 0, 1] ===
Final Weights: [0.24498444 0.87298307]
Final Bias: -0.24933391810252628
Epochs to Learn: 7
Final Test Output: [0, 1, 0, 1]

=== Truth Table 7: Targets = [0, 1, 1, 0] ===
Final Weights: [-0.21772779 -0.0659535 ]
Final Bias: 0.09100783430492371
Epochs to Learn: 1000
Final Test Output: [1, 1, 0, 0]

=== Truth Table 8: Targets = [0, 1, 1, 1] ===
Final Weights: [0.15618047 0.37526284]
Final Bias: -0.08455528713850663
Epochs to Learn: 8
Final Test Output: [0, 1, 1, 1]

=== Truth Table 9: Targets = [1, 0, 0, 0] ===
Final Weights: [-0.44465284 -0.25205611]
Final Bias: 0.19614984181677728
Epochs to Learn: 5
Final Test Output: [1, 0, 0, 0]

=== Truth Table 10: Targets = [1, 0, 0, 1] ===
Final Weights: [0.16985336 0.04181385]
Final Bias: -0.07544160536019556
Epochs to Learn: 1000
Final Test Output: [0, 0, 1, 1]

=== Truth Table 11: Targets = [1, 0, 1, 0] ===
Final Weights: [-0.41026573 -0.85057327]
Final Bias: 0.798634615301705
Epochs to Learn: 1
Final Test Output: [1, 0, 1, 0]

=== Truth Table 12: Targets = [1, 0, 1, 1] ===
Final Weights: [ 0.16274565 -0.19400859]
Final Bias: 0.09022750168301319
Epochs to Learn: 5
Final Test Output: [1, 0, 1, 1]

=== Truth Table 13: Targets = [1, 1, 0, 0] ===
Final Weights: [-0.27638351 -0.11278153]
Final Bias: 0.2473132514789307
Epochs to Learn: 7
Final Test Output: [1, 1, 0, 0]

=== Truth Table 14: Targets = [1, 1, 0, 1] ===
Final Weights: [-0.07958203  0.15471287]
Final Bias: 0.06660390737126293
Epochs to Learn: 12
Final Test Output: [1, 1, 0, 1]

=== Truth Table 15: Targets = [1, 1, 1, 0] ===
Final Weights: [-0.10461177 -0.66724227]
Final Bias: 0.7135429994654503
Epochs to Learn: 12
Final Test Output: [1, 1, 1, 0]

=== Truth Table 16: Targets = [1, 1, 1, 1] ===
Final Weights: [0.47150314 0.41833221]
Final Bias: 0.06285077195697164
Epochs to Learn: 7
Final Test Output: [1, 1, 1, 1]