fork download
  1. import numpy as np
  2.  
  3. # Initial thresholds for activation function
  4. lower_threshold = 0.8
  5. upper_threshold = 1.2
  6.  
  7. # Learning rate
  8. learning_rate = 0.1
  9.  
  10. # XOR training data (4 combinations of 2 binary inputs)
  11. inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
  12. targets = [0, 1, 1, 0]
  13.  
  14. # Maximum number of iterations
  15. max_iterations = 1000
  16. epoch = 0
  17. network_trained = False
  18.  
  19. # Initialize weights for 3 neurons (2 inputs + 1 hidden + 1 output)
  20. weights_input_to_hidden = np.random.rand(3) * 0.5 # Initialize with a smaller value to avoid large initial weights
  21. weights_hidden_to_output = np.random.rand(2) * 0.5 # Initialize with a smaller value to avoid large initial weights
  22.  
  23. # Training loop
  24. while epoch < max_iterations:
  25. epoch += 1
  26. all_correct = True # Flag to check if all outputs are correct
  27. current_weights_input_to_hidden = weights_input_to_hidden.copy()
  28. current_weights_hidden_to_output = weights_hidden_to_output.copy()
  29.  
  30. # Loop through each training example
  31. for input_vector, target in zip(inputs, targets):
  32. N1, N2 = input_vector
  33. bias = 1 # Bias input
  34.  
  35. # Calculate the weighted sum for the hidden layer (Neuron 1)
  36. N3_input = np.dot(np.array([N1, N2, bias]), current_weights_input_to_hidden)
  37. # Apply the threshold activation function for hidden neuron
  38. N3 = 1 if lower_threshold < N3_input < upper_threshold else 0
  39.  
  40. # Calculate the weighted sum for the output layer (Neuron 2)
  41. N4_input = N3 * current_weights_hidden_to_output[0] + bias * current_weights_hidden_to_output[1]
  42. # Apply the threshold activation function for output neuron
  43. N4 = 1 if lower_threshold < N4_input < upper_threshold else 0
  44.  
  45. # Calculate the error (difference between target and output)
  46. error = target - N4
  47.  
  48. # Update weights if there's an error
  49. if error != 0:
  50. all_correct = False
  51. # Update the weights for the hidden-to-output connection
  52. current_weights_hidden_to_output[0] += learning_rate * error * N3
  53. current_weights_hidden_to_output[1] += learning_rate * error * bias
  54.  
  55. # Update the weights for the input-to-hidden connection
  56. current_weights_input_to_hidden += learning_rate * error * N3 * np.array([N1, N2, bias])
  57.  
  58. # Check if all outputs are correct for this epoch
  59. if all_correct:
  60. network_trained = True
  61. break # Stop if the network has learned the XOR function
  62.  
  63. # If after 100 iterations it's not working, reset the weights
  64. if epoch % 100 == 0:
  65. print(f"Nicht funktionierende Startgewichte nach {epoch} Iterationen.")
  66. weights_input_to_hidden = np.random.rand(3) * 0.5 # Reset input-to-hidden weights
  67. weights_hidden_to_output = np.random.rand(2) * 0.5 # Reset hidden-to-output weights
  68.  
  69. # Results
  70. if network_trained:
  71. print(f"Das Netzwerk hat XOR korrekt nach {epoch} Iterationen gelernt.")
  72. else:
  73. print(f"Das Netzwerk hat XOR nach {epoch} Iterationen nicht korrekt gelernt.")
  74.  
  75. # Testing the trained network
  76. print("\nFinal Test Output:")
  77. for input_vector, target in zip(inputs, targets):
  78. N1, N2 = input_vector
  79. bias = 1
  80.  
  81. # Calculate the weighted sum for the hidden layer (Neuron 1)
  82. N3_input = np.dot(np.array([N1, N2, bias]), current_weights_input_to_hidden)
  83. N3 = 1 if lower_threshold < N3_input < upper_threshold else 0
  84.  
  85. # Calculate the weighted sum for the output layer (Neuron 2)
  86. N4_input = N3 * current_weights_hidden_to_output[0] + bias * current_weights_hidden_to_output[1]
  87. N4 = 1 if lower_threshold < N4_input < upper_threshold else 0
  88.  
  89. print(f"Input: {input_vector}, Target: {target}, Output: {N4}")
  90.  
Success #stdin #stdout 0.2s 28800KB
stdin
Standard input is empty
stdout
Nicht funktionierende Startgewichte nach 100 Iterationen.
Nicht funktionierende Startgewichte nach 200 Iterationen.
Nicht funktionierende Startgewichte nach 300 Iterationen.
Nicht funktionierende Startgewichte nach 400 Iterationen.
Nicht funktionierende Startgewichte nach 500 Iterationen.
Nicht funktionierende Startgewichte nach 600 Iterationen.
Nicht funktionierende Startgewichte nach 700 Iterationen.
Nicht funktionierende Startgewichte nach 800 Iterationen.
Nicht funktionierende Startgewichte nach 900 Iterationen.
Nicht funktionierende Startgewichte nach 1000 Iterationen.
Das Netzwerk hat XOR nach 1000 Iterationen nicht korrekt gelernt.

Final Test Output:
Input: [0, 0], Target: 0, Output: 0
Input: [0, 1], Target: 1, Output: 0
Input: [1, 0], Target: 1, Output: 0
Input: [1, 1], Target: 0, Output: 0