fork download
  1. import numpy as np
  2. from itertools import product
  3.  
  4. # Parameters
  5. lower_threshold = 0.8
  6. upper_threshold = 1.2
  7. learning_rate = 0.1
  8. max_iterations = 500
  9. bias_increment = 0.1
  10. min_bias, max_bias = -0.9, 0.9
  11.  
  12. # XOR input data
  13. inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  14.  
  15. # Generate all possible target tables
  16. all_possible_targets = list(product([0, 1], repeat=4))
  17.  
  18. def train_network(inputs, targets, max_iterations, bias, weight_range=(-2, 2)):
  19. """Train network with a specific bias and weights range"""
  20. weights = np.random.uniform(*weight_range, size=2)
  21. epoch = 0
  22.  
  23. while epoch < max_iterations:
  24. epoch += 1
  25. all_correct = True
  26. # Vectorized error calculation and weight updates
  27. for input_vector, target in zip(inputs, targets):
  28. weighted_sum = np.dot(input_vector, weights) + bias
  29. output = 1 if lower_threshold < weighted_sum < upper_threshold else 0
  30. error = target - output
  31. if error != 0:
  32. all_correct = False
  33. weights += learning_rate * error * input_vector
  34. if all_correct:
  35. return weights, epoch, True
  36. return weights, epoch, False
  37.  
  38. # Loop through all target tables and train
  39. for table_index, targets in enumerate(all_possible_targets, start=1):
  40. print(f"\n=== Wahrheitstabelle {table_index}: Targets = {targets} ===")
  41.  
  42. # Training without bias adjustment
  43. network_trained = False
  44. for bias in np.arange(0.1, max_bias + bias_increment, bias_increment): # Start with bias incrementing upwards
  45. print(f"Attempt with bias {bias}:")
  46. final_weights, epoch, network_trained = train_network(inputs, targets, max_iterations, bias)
  47. if network_trained:
  48. print(f"Network trained in {epoch} epochs. Start weights: {final_weights}")
  49. break
  50.  
  51. # If not trained, try with decreasing bias
  52. if not network_trained:
  53. print("Increasing bias until max, then try decreasing.")
  54. for bias in np.arange(max_bias, min_bias - bias_increment, -bias_increment):
  55. print(f"Attempt with bias {bias}:")
  56. final_weights, epoch, network_trained = train_network(inputs, targets, max_iterations, bias)
  57. if network_trained:
  58. print(f"Network trained in {epoch} epochs. Start weights: {final_weights}")
  59. break
  60.  
  61. # If network still not trained, print the status
  62. if not network_trained:
  63. print(f"Network failed to train for truth table {table_index}.")
  64. else:
  65. print(f"Successfully trained network for table {table_index}. Final weights: {final_weights}")
  66.  
Success #stdin #stdout 4.14s 29088KB
stdin
Standard input is empty
stdout
=== Wahrheitstabelle 1: Targets = (0, 0, 0, 0) ===
Attempt with bias 0.1:
Network trained in 1 epochs. Start weights: [-0.48084165  1.77589139]
Successfully trained network for table 1. Final weights: [-0.48084165  1.77589139]

=== Wahrheitstabelle 2: Targets = (0, 0, 0, 1) ===
Attempt with bias 0.1:
Network trained in 18 epochs. Start weights: [0.63945552 0.08987405]
Successfully trained network for table 2. Final weights: [0.63945552 0.08987405]

=== Wahrheitstabelle 3: Targets = (0, 0, 1, 0) ===
Attempt with bias 0.1:
Network trained in 38 epochs. Start weights: [ 0.72299975 -0.09435871]
Successfully trained network for table 3. Final weights: [ 0.72299975 -0.09435871]

=== Wahrheitstabelle 4: Targets = (0, 0, 1, 1) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Network trained in 7 epochs. Start weights: [ 0.06639862 -0.06087761]
Successfully trained network for table 4. Final weights: [ 0.06639862 -0.06087761]

=== Wahrheitstabelle 5: Targets = (0, 1, 0, 0) ===
Attempt with bias 0.1:
Network trained in 18 epochs. Start weights: [-0.46614009  0.73256652]
Successfully trained network for table 5. Final weights: [-0.46614009  0.73256652]

=== Wahrheitstabelle 6: Targets = (0, 1, 0, 1) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Increasing bias until max, then try decreasing.
Attempt with bias 0.9:
Attempt with bias 0.8:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.6000000000000001:
Attempt with bias 0.5000000000000001:
Attempt with bias 0.40000000000000013:
Network trained in 6 epochs. Start weights: [0.14229491 0.46191418]
Successfully trained network for table 6. Final weights: [0.14229491 0.46191418]

=== Wahrheitstabelle 7: Targets = (0, 1, 1, 0) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Increasing bias until max, then try decreasing.
Attempt with bias 0.9:
Attempt with bias 0.8:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.6000000000000001:
Attempt with bias 0.5000000000000001:
Attempt with bias 0.40000000000000013:
Attempt with bias 0.30000000000000016:
Attempt with bias 0.20000000000000018:
Attempt with bias 0.1000000000000002:
Attempt with bias 2.220446049250313e-16:
Attempt with bias -0.09999999999999976:
Network trained in 1 epochs. Start weights: [0.94092252 0.93601617]
Successfully trained network for table 7. Final weights: [0.94092252 0.93601617]

=== Wahrheitstabelle 8: Targets = (0, 1, 1, 1) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Network trained in 5 epochs. Start weights: [0.22346137 0.20338291]
Successfully trained network for table 8. Final weights: [0.22346137 0.20338291]

=== Wahrheitstabelle 9: Targets = (1, 0, 0, 0) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Network trained in 1 epochs. Start weights: [ 1.15571994 -1.32524024]
Successfully trained network for table 9. Final weights: [ 1.15571994 -1.32524024]

=== Wahrheitstabelle 10: Targets = (1, 0, 0, 1) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Increasing bias until max, then try decreasing.
Attempt with bias 0.9:
Network trained in 7 epochs. Start weights: [ 1.12121661 -1.06702134]
Successfully trained network for table 10. Final weights: [ 1.12121661 -1.06702134]

=== Wahrheitstabelle 11: Targets = (1, 0, 1, 0) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Network trained in 15 epochs. Start weights: [-0.02122794 -0.71311665]
Successfully trained network for table 11. Final weights: [-0.02122794 -0.71311665]

=== Wahrheitstabelle 12: Targets = (1, 0, 1, 1) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Increasing bias until max, then try decreasing.
Attempt with bias 0.9:
Network trained in 11 epochs. Start weights: [ 0.0663865  -0.14271712]
Successfully trained network for table 12. Final weights: [ 0.0663865  -0.14271712]

=== Wahrheitstabelle 13: Targets = (1, 1, 0, 0) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Network trained in 17 epochs. Start weights: [-1.41134042 -0.07156359]
Successfully trained network for table 13. Final weights: [-1.41134042 -0.07156359]

=== Wahrheitstabelle 14: Targets = (1, 1, 0, 1) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Increasing bias until max, then try decreasing.
Attempt with bias 0.9:
Attempt with bias 0.8:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.6000000000000001:
Attempt with bias 0.5000000000000001:
Attempt with bias 0.40000000000000013:
Attempt with bias 0.30000000000000016:
Attempt with bias 0.20000000000000018:
Attempt with bias 0.1000000000000002:
Attempt with bias 2.220446049250313e-16:
Attempt with bias -0.09999999999999976:
Attempt with bias -0.19999999999999962:
Attempt with bias -0.2999999999999997:
Attempt with bias -0.3999999999999998:
Attempt with bias -0.49999999999999967:
Attempt with bias -0.5999999999999995:
Attempt with bias -0.6999999999999996:
Attempt with bias -0.7999999999999997:
Attempt with bias -0.8999999999999996:
Network failed to train for truth table 14.

=== Wahrheitstabelle 15: Targets = (1, 1, 1, 0) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Increasing bias until max, then try decreasing.
Attempt with bias 0.9:
Attempt with bias 0.8:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.6000000000000001:
Attempt with bias 0.5000000000000001:
Attempt with bias 0.40000000000000013:
Attempt with bias 0.30000000000000016:
Attempt with bias 0.20000000000000018:
Attempt with bias 0.1000000000000002:
Attempt with bias 2.220446049250313e-16:
Attempt with bias -0.09999999999999976:
Attempt with bias -0.19999999999999962:
Attempt with bias -0.2999999999999997:
Attempt with bias -0.3999999999999998:
Attempt with bias -0.49999999999999967:
Attempt with bias -0.5999999999999995:
Attempt with bias -0.6999999999999996:
Attempt with bias -0.7999999999999997:
Attempt with bias -0.8999999999999996:
Network failed to train for truth table 15.

=== Wahrheitstabelle 16: Targets = (1, 1, 1, 1) ===
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Attempt with bias 0.5:
Attempt with bias 0.6:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.8:
Attempt with bias 0.9:
Increasing bias until max, then try decreasing.
Attempt with bias 0.9:
Attempt with bias 0.8:
Attempt with bias 0.7000000000000001:
Attempt with bias 0.6000000000000001:
Attempt with bias 0.5000000000000001:
Attempt with bias 0.40000000000000013:
Attempt with bias 0.30000000000000016:
Attempt with bias 0.20000000000000018:
Attempt with bias 0.1000000000000002:
Attempt with bias 2.220446049250313e-16:
Attempt with bias -0.09999999999999976:
Attempt with bias -0.19999999999999962:
Attempt with bias -0.2999999999999997:
Attempt with bias -0.3999999999999998:
Attempt with bias -0.49999999999999967:
Attempt with bias -0.5999999999999995:
Attempt with bias -0.6999999999999996:
Attempt with bias -0.7999999999999997:
Attempt with bias -0.8999999999999996:
Network failed to train for truth table 16.