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. # Start with weights in the range [0, 2] and bias 0.0
  43. network_trained = False
  44. print(f"Attempt with weights between 0 and 2, bias 0.0:")
  45. final_weights, epoch, network_trained = train_network(inputs, targets, max_iterations, bias=0.0, weight_range=(0, 2))
  46.  
  47. if network_trained:
  48. print(f"Network trained successfully in {epoch} epochs. Final weights: {final_weights}")
  49. else:
  50. print(f"Failed to train after {epoch} epochs. Moving to next training with bias adjustments.")
  51.  
  52. # If not trained, try training with bias adjustments
  53. if not network_trained:
  54. print("Trying different biases between 0.1 and 1.3:")
  55. for bias in np.arange(0.1, max_bias + bias_increment, bias_increment):
  56. print(f"Attempt with bias {bias}:")
  57. final_weights, epoch, network_trained = train_network(inputs, targets, max_iterations, bias, weight_range=(0, 2))
  58. if network_trained:
  59. print(f"Network trained successfully in {epoch} epochs. Final weights: {final_weights}")
  60. break # Stop if trained
  61.  
  62. # If still not trained, try with negative weights and bias range -0.9 to 0.9
  63. if not network_trained:
  64. print("Increasing bias until max, then try decreasing with weights between -2 and 2.")
  65. for bias in np.arange(max_bias, min_bias - bias_increment, -bias_increment):
  66. print(f"Attempt with bias {bias}:")
  67. final_weights, epoch, network_trained = train_network(inputs, targets, max_iterations, bias, weight_range=(-2, 2))
  68. if network_trained:
  69. print(f"Network trained successfully in {epoch} epochs. Final weights: {final_weights}")
  70. break # Stop if trained
  71.  
  72. # If network still not trained, print the status
  73. if not network_trained:
  74. print(f"Network failed to train for truth table {table_index}.")
  75. else:
  76. print(f"Successfully trained network for table {table_index}. Final weights: {final_weights}")
  77.  
Success #stdin #stdout 3.8s 28932KB
stdin
Standard input is empty
stdout
=== Wahrheitstabelle 1: Targets = (0, 0, 0, 0) ===
Attempt with weights between 0 and 2, bias 0.0:
Network trained successfully in 2 epochs. Final weights: [0.00102747 0.69827707]
Successfully trained network for table 1. Final weights: [0.00102747 0.69827707]

=== Wahrheitstabelle 2: Targets = (0, 0, 0, 1) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 successfully in 5 epochs. Final weights: [0.07792734 0.19195991]
Successfully trained network for table 2. Final weights: [0.07792734 0.19195991]

=== Wahrheitstabelle 3: Targets = (0, 0, 1, 0) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Network trained successfully in 1 epochs. Final weights: [0.68539848 0.49589082]
Successfully trained network for table 3. Final weights: [0.68539848 0.49589082]

=== Wahrheitstabelle 4: Targets = (0, 0, 1, 1) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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:
Network trained successfully in 1 epochs. Final weights: [0.27903531 0.07475321]
Successfully trained network for table 4. Final weights: [0.27903531 0.07475321]

=== Wahrheitstabelle 5: Targets = (0, 1, 0, 0) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
Attempt with bias 0.1:
Network trained successfully in 7 epochs. Final weights: [1.38504247 0.74821291]
Successfully trained network for table 5. Final weights: [1.38504247 0.74821291]

=== Wahrheitstabelle 6: Targets = (0, 1, 0, 1) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
Attempt with bias 0.1:
Attempt with bias 0.2:
Attempt with bias 0.30000000000000004:
Attempt with bias 0.4:
Network trained successfully in 4 epochs. Final weights: [0.33839918 0.43290712]
Successfully trained network for table 6. Final weights: [0.33839918 0.43290712]

=== Wahrheitstabelle 7: Targets = (0, 1, 1, 0) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
Attempt with bias 0.1:
Network trained successfully in 1 epochs. Final weights: [0.84071951 0.99978141]
Successfully trained network for table 7. Final weights: [0.84071951 0.99978141]

=== Wahrheitstabelle 8: Targets = (0, 1, 1, 1) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 with weights between -2 and 2.
Attempt with bias 0.9:
Attempt with bias 0.8:
Network trained successfully in 7 epochs. Final weights: [0.01284123 0.05640178]
Successfully trained network for table 8. Final weights: [0.01284123 0.05640178]

=== Wahrheitstabelle 9: Targets = (1, 0, 0, 0) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 successfully in 4 epochs. Final weights: [-0.15843941  0.80898375]
Successfully trained network for table 9. Final weights: [-0.15843941  0.80898375]

=== Wahrheitstabelle 10: Targets = (1, 0, 0, 1) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 with weights between -2 and 2.
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 10.

=== Wahrheitstabelle 11: Targets = (1, 0, 1, 0) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 with weights between -2 and 2.
Attempt with bias 0.9:
Network trained successfully in 18 epochs. Final weights: [-0.06143757 -0.14736495]
Successfully trained network for table 11. Final weights: [-0.06143757 -0.14736495]

=== Wahrheitstabelle 12: Targets = (1, 0, 1, 1) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 with weights between -2 and 2.
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 12.

=== Wahrheitstabelle 13: Targets = (1, 1, 0, 0) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 with weights between -2 and 2.
Attempt with bias 0.9:
Network trained successfully in 2 epochs. Final weights: [ 1.53833614 -0.04526522]
Successfully trained network for table 13. Final weights: [ 1.53833614 -0.04526522]

=== Wahrheitstabelle 14: Targets = (1, 1, 0, 1) ===
Attempt with weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 with weights between -2 and 2.
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 weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 with weights between -2 and 2.
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 weights between 0 and 2, bias 0.0:
Failed to train after 500 epochs. Moving to next training with bias adjustments.
Trying different biases between 0.1 and 1.3:
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 with weights between -2 and 2.
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.