fork download
  1. import numpy as np
  2.  
  3. # Initialisierung der Schwellenwerte
  4. lower_threshold = 0.8
  5. upper_threshold = 1.2
  6.  
  7. # Lernrate
  8. learning_rate = 0.1
  9.  
  10. # Trainingsdaten (XOR-Problem)
  11. inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
  12. targets = [0, 1, 1, 0]
  13.  
  14. # Trainingsloop mit max. 1000 Iterationen
  15. max_iterations = 1000
  16. epoch = 0
  17. network_trained = False
  18. start_weights = None
  19. final_weights = None
  20. all_epoch_outputs = [] # Store outputs of all epochs for debugging and transparency
  21.  
  22. while epoch < max_iterations:
  23. epoch += 1
  24. all_correct = True # Flag, um zu überprüfen, ob alle Ausgaben korrekt sind
  25. current_weights = np.random.rand(2) # Zufällige Startgewichte
  26.  
  27. if epoch == 1: # Die erste Iteration nach Initialisierung
  28. start_weights = current_weights # Speichere die Startgewichte
  29.  
  30. epoch_outputs = [] # To store outputs of this epoch
  31.  
  32. for input_vector, target in zip(inputs, targets):
  33. # Berechnung der gewichteten Summe
  34. weighted_sum = np.dot(input_vector, current_weights)
  35.  
  36. # Aktivierungsfunktion (einfache Schwellenwertfunktion)
  37. output = 1 if lower_threshold < weighted_sum < upper_threshold else 0
  38.  
  39. # Fehlerberechnung
  40. error = target - output
  41.  
  42. # Wenn ein Fehler vorliegt, dann weise die Gewichte an
  43. if error != 0:
  44. all_correct = False
  45. current_weights += learning_rate * error * np.array(input_vector)
  46.  
  47. epoch_outputs.append((input_vector, output, target)) # Save each iteration's output
  48.  
  49. all_epoch_outputs.append(epoch_outputs)
  50.  
  51. # Überprüfe, ob alle Ausgaben korrekt sind
  52. if all_correct:
  53. network_trained = True
  54. final_weights = current_weights # Speichere die finalen Gewichte
  55. break # Stoppe, wenn alle Ausgaben korrekt sind
  56.  
  57. # Wenn XOR nach 100 Iterationen nicht gelernt wurde, setze neue zufällige Startgewichte
  58. if epoch % 100 == 0: # 100 statt 20
  59. print(f"Nicht funktionierende Startgewichte: {start_weights}")
  60. start_weights = np.random.rand(2) # Setze neue Startgewichte
  61.  
  62. if network_trained:
  63. print(f"Das Netzwerk hat XOR korrekt nach {epoch} Iterationen gelernt.")
  64. print(f"Die Working Startgewichte waren: {start_weights}")
  65. print(f"Die finalen Gewichte sind: {final_weights}")
  66. else:
  67. print(f"Das Netzwerk hat XOR nach {epoch} Iterationen nicht korrekt gelernt.")
  68.  
  69. # Testen des Netzwerks nach den Lern-Iterationen
  70. print("\nFinal Test Output:")
  71. for input_vector, target in zip(inputs, targets):
  72. weighted_sum = np.dot(input_vector, final_weights)
  73. output = 1 if lower_threshold < weighted_sum < upper_threshold else 0
  74. print(f"Input: {input_vector}, Target: {target}, Output: {output}")
  75.  
  76. # Optionally, print out the outputs of each epoch for transparency
  77. print("\nEpoch Outputs:")
  78. for epoch_index, epoch_outputs in enumerate(all_epoch_outputs):
  79. print(f"Epoch {epoch_index + 1}:")
  80. for input_vector, output, target in epoch_outputs:
  81. print(f" Input: {input_vector}, Output: {output}, Target: {target}")
  82.  
Success #stdin #stdout 0.13s 28784KB
stdin
Standard input is empty
stdout
Das Netzwerk hat XOR korrekt nach 16 Iterationen gelernt.
Die Working Startgewichte waren: [0.57908339 0.69566789]
Die finalen Gewichte sind: [0.82632467 0.98801676]

Final Test Output:
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

Epoch Outputs:
Epoch 1:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 2:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 1, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 3:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 1, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 4:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 1, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 5:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 1, Target: 0
Epoch 6:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 7:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 8:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 9:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 10:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 1, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 11:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 1, Target: 0
Epoch 12:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 13:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 14:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 15:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 0, Target: 1
  Input: [1, 0], Output: 0, Target: 1
  Input: [1, 1], Output: 0, Target: 0
Epoch 16:
  Input: [0, 0], Output: 0, Target: 0
  Input: [0, 1], Output: 1, Target: 1
  Input: [1, 0], Output: 1, Target: 1
  Input: [1, 1], Output: 0, Target: 0