fork download
  1. import numpy as np
  2.  
  3. # Parameters
  4. lower_threshold = 0.8
  5. upper_threshold = 1.2
  6. learning_rate = 0.1
  7. max_iterations = 1000
  8.  
  9. # Training data (XOR problem)
  10. inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
  11. targets = np.array([0, 1, 1, 0])
  12.  
  13. # Initialize weights for connections: Input -> Hidden, Hidden -> Output
  14. weights_input_hidden = np.random.rand(2, 2) # 2 input neurons, 2 hidden neurons
  15. weights_hidden_output = np.random.rand(2) # 2 hidden neurons to 1 output neuron
  16.  
  17. # Function to apply the threshold activation
  18. def activation_function(weighted_sum):
  19. return 1 if lower_threshold < weighted_sum < upper_threshold else 0
  20.  
  21. # Training loop
  22. epoch = 0
  23. network_trained = False
  24. start_weights_input_hidden = weights_input_hidden.copy()
  25. start_weights_hidden_output = weights_hidden_output.copy()
  26.  
  27. while epoch < max_iterations:
  28. epoch += 1
  29. all_correct = True # Track if all outputs are correct in an epoch
  30.  
  31. for input_vector, target in zip(inputs, targets):
  32. # Forward pass
  33. hidden_input = np.dot(input_vector, weights_input_hidden) # Input -> Hidden
  34. hidden_output = np.array([activation_function(h) for h in hidden_input]) # Activation of hidden neurons
  35.  
  36. final_input = np.dot(hidden_output, weights_hidden_output) # Hidden -> Output
  37. output = activation_function(final_input) # Activation of output neuron
  38.  
  39. # Calculate error
  40. error = target - output
  41.  
  42. # Backpropagation and weight update if error exists
  43. if error != 0:
  44. all_correct = False
  45.  
  46. # Update weights for Hidden -> Output
  47. weights_hidden_output += learning_rate * error * hidden_output
  48.  
  49. # Update weights for Input -> Hidden
  50. for i in range(2): # Loop over hidden neurons
  51. if hidden_output[i] > 0: # Only update weights if neuron is active
  52. weights_input_hidden[:, i] += learning_rate * error * input_vector
  53.  
  54. if all_correct:
  55. network_trained = True
  56. break # Stop training if all outputs are correct
  57.  
  58. # Results
  59. if network_trained:
  60. print(f"The network learned XOR correctly after {epoch} iterations.")
  61. else:
  62. print(f"The network did not learn XOR correctly after {epoch} iterations.")
  63.  
  64. print("\nTesting the trained network:")
  65. for input_vector, target in zip(inputs, targets):
  66. hidden_input = np.dot(input_vector, weights_input_hidden) # Input -> Hidden
  67. hidden_output = np.array([activation_function(h) for h in hidden_input]) # Hidden neuron activations
  68.  
  69. final_input = np.dot(hidden_output, weights_hidden_output) # Hidden -> Output
  70. output = activation_function(final_input) # Activation of output neuron
  71. print(f"Input: {input_vector}, Target: {target}, Output: {output}")
  72.  
  73. print("\nInitial Weights (Input -> Hidden):")
  74. print(start_weights_input_hidden)
  75.  
  76. print("\nInitial Weights (Hidden -> Output):")
  77. print(start_weights_hidden_output)
  78.  
  79. print("\nFinal Weights (Input -> Hidden):")
  80. print(weights_input_hidden)
  81.  
  82. print("\nFinal Weights (Hidden -> Output):")
  83. print(weights_hidden_output)
  84.  
Success #stdin #stdout 0.16s 28776KB
stdin
Standard input is empty
stdout
The network learned XOR correctly after 6 iterations.

Testing the trained network:
Input: [0 0], Target: 0, Output: 0
Input: [0 1], Target: 1, Output: 1
Input: [1 0], Target: 1, Output: 1
Input: [1 1], Target: 0, Output: 0

Initial Weights (Input -> Hidden):
[[0.4133748  0.92702173]
 [0.82638346 0.08439388]]

Initial Weights (Hidden -> Output):
[0.8610371  0.72881776]

Final Weights (Input -> Hidden):
[[ 0.4133748   1.02702173]
 [ 0.82638346 -0.31560612]]

Final Weights (Hidden -> Output):
[0.8610371  0.82881776]