fork download
  1. import numpy as np
  2. from itertools import product
  3.  
  4. # Initialization of thresholds and learning rate
  5. lower_threshold = 0.8
  6. upper_threshold = 1.2
  7. learning_rate = 0.1
  8.  
  9. # Training data for XOR problem
  10. inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
  11.  
  12. # All possible truth tables (16 combinations)
  13. all_possible_targets = list(product([0, 1], repeat=4))
  14.  
  15. # Bias values to test
  16. biases_to_test = [0, 0.9, -0.5]
  17.  
  18. # Training loop for each truth table
  19. for table_index, targets in enumerate(all_possible_targets, start=1):
  20. print(f"\n=== Truth Table {table_index}: Targets = {targets} ===")
  21.  
  22. for bias in biases_to_test:
  23. print(f"\n--- Training with Bias = {bias} ---")
  24.  
  25. # Initialize weights and training variables
  26. max_iterations = 500
  27. epoch = 0
  28. network_trained = False
  29. start_weights = np.random.uniform(-2, 2, 2)
  30. current_weights = start_weights.copy()
  31.  
  32. while epoch < max_iterations:
  33. epoch += 1
  34. all_correct = True
  35.  
  36. for input_vector, target in zip(inputs, targets):
  37. # Weighted sum with bias
  38. weighted_sum = np.dot(input_vector, current_weights) + bias
  39.  
  40. # Activation function
  41. output = 1 if lower_threshold < weighted_sum < upper_threshold else 0
  42.  
  43. # Error calculation
  44. error = target - output
  45.  
  46. if error != 0:
  47. all_correct = False
  48. # Update weights
  49. current_weights += learning_rate * error * np.array(input_vector)
  50.  
  51. # If all outputs are correct, training is successful
  52. if all_correct:
  53. network_trained = True
  54. break
  55.  
  56. # Display results for this bias
  57. if network_trained:
  58. print(f"Training successful after {epoch} iterations.")
  59. print(f"Start Weights: {start_weights}")
  60. print(f"Final Weights: {current_weights}")
  61. print(f"Bias: {bias}")
  62. break
  63. else:
  64. print(f"Training failed after {epoch} iterations with Bias = {bias}.")
  65.  
  66. if not network_trained:
  67. print(f"Truth Table {table_index} could not be trained successfully with the given biases.")
  68.  
Success #stdin #stdout 0.85s 28812KB
stdin
Standard input is empty
stdout
=== Truth Table 1: Targets = (0, 0, 0, 0) ===

--- Training with Bias = 0 ---
Training successful after 1 iterations.
Start Weights: [-0.24788355  0.73818163]
Final Weights: [-0.24788355  0.73818163]
Bias: 0

=== Truth Table 2: Targets = (0, 0, 0, 1) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training failed after 500 iterations with Bias = -0.5.
Truth Table 2 could not be trained successfully with the given biases.

=== Truth Table 3: Targets = (0, 0, 1, 0) ===

--- Training with Bias = 0 ---
Training successful after 37 iterations.
Start Weights: [-1.84065641  0.89394703]
Final Weights: [ 0.85934359 -0.10605297]
Bias: 0

=== Truth Table 4: Targets = (0, 0, 1, 1) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training successful after 9 iterations.
Start Weights: [ 0.1482963  -0.96117832]
Final Weights: [ 1.5482963  -0.16117832]
Bias: -0.5

=== Truth Table 5: Targets = (0, 1, 0, 0) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training successful after 3 iterations.
Start Weights: [-0.2767018   1.16114787]
Final Weights: [-0.2767018   1.36114787]
Bias: -0.5

=== Truth Table 6: Targets = (0, 1, 0, 1) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training successful after 8 iterations.
Start Weights: [-1.04669028  0.58300389]
Final Weights: [-0.34669028  1.68300389]
Bias: -0.5

=== Truth Table 7: Targets = (0, 1, 1, 0) ===

--- Training with Bias = 0 ---
Training successful after 5 iterations.
Start Weights: [0.64436629 0.40923079]
Final Weights: [0.84436629 0.80923079]
Bias: 0

=== Truth Table 8: Targets = (0, 1, 1, 1) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training failed after 500 iterations with Bias = -0.5.
Truth Table 8 could not be trained successfully with the given biases.

=== Truth Table 9: Targets = (1, 0, 0, 0) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training successful after 1 iterations.
Start Weights: [-0.13148464 -0.60446763]
Final Weights: [-0.13148464 -0.60446763]
Bias: 0.9

=== Truth Table 10: Targets = (1, 0, 0, 1) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training failed after 500 iterations with Bias = -0.5.
Truth Table 10 could not be trained successfully with the given biases.

=== Truth Table 11: Targets = (1, 0, 1, 0) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training successful after 12 iterations.
Start Weights: [-1.1572868   1.42838662]
Final Weights: [-0.0572868   1.42838662]
Bias: 0.9

=== Truth Table 12: Targets = (1, 0, 1, 1) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training failed after 500 iterations with Bias = -0.5.
Truth Table 12 could not be trained successfully with the given biases.

=== Truth Table 13: Targets = (1, 1, 0, 0) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training successful after 3 iterations.
Start Weights: [ 1.15496011 -0.22358277]
Final Weights: [ 1.15496011 -0.02358277]
Bias: 0.9

=== Truth Table 14: Targets = (1, 1, 0, 1) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training failed after 500 iterations with Bias = -0.5.
Truth Table 14 could not be trained successfully with the given biases.

=== Truth Table 15: Targets = (1, 1, 1, 0) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training failed after 500 iterations with Bias = -0.5.
Truth Table 15 could not be trained successfully with the given biases.

=== Truth Table 16: Targets = (1, 1, 1, 1) ===

--- Training with Bias = 0 ---
Training failed after 500 iterations with Bias = 0.

--- Training with Bias = 0.9 ---
Training failed after 500 iterations with Bias = 0.9.

--- Training with Bias = -0.5 ---
Training failed after 500 iterations with Bias = -0.5.
Truth Table 16 could not be trained successfully with the given biases.