import numpy as np # Initialisierung der Schwellenwerte lower_threshold = 0.8 upper_threshold = 1.2 # Lernrate learning_rate = 0.1 # Trainingsdaten (XOR-Problem) inputs = [[0, 0], [0, 1], [1, 0], [1, 1]] targets = [0, 1, 1, 0] # Trainingsloop mit max. 1000 Iterationen max_iterations = 1000 epoch = 0 network_trained = False start_weights = None final_weights = None all_epoch_outputs = [] # Store outputs of all epochs for debugging and transparency while epoch < max_iterations: epoch += 1 all_correct = True # Flag, um zu überprüfen, ob alle Ausgaben korrekt sind if epoch == 1: # Die erste Iteration nach Initialisierung start_weights = current_weights # Speichere die Startgewichte epoch_outputs = [] # To store outputs of this epoch for input_vector, target in zip(inputs, targets): # Berechnung der gewichteten Summe weighted_sum = np.dot(input_vector, current_weights) # Aktivierungsfunktion (einfache Schwellenwertfunktion) output = 1 if lower_threshold < weighted_sum < upper_threshold else 0 # Fehlerberechnung error = target - output # Wenn ein Fehler vorliegt, dann weise die Gewichte an if error != 0: all_correct = False current_weights += learning_rate * error * np.array(input_vector) epoch_outputs.append((input_vector, output, target)) # Save each iteration's output all_epoch_outputs.append(epoch_outputs) # Überprüfe, ob alle Ausgaben korrekt sind if all_correct: network_trained = True final_weights = current_weights # Speichere die finalen Gewichte break # Stoppe, wenn alle Ausgaben korrekt sind # Wenn XOR nach 100 Iterationen nicht gelernt wurde, setze neue zufällige Startgewichte if epoch % 100 == 0: # 100 statt 20 print(f"Nicht funktionierende Startgewichte: {start_weights}") if network_trained: print(f"Das Netzwerk hat XOR korrekt nach {epoch} Iterationen gelernt.") print(f"Die Working Startgewichte waren: {start_weights}") print(f"Die finalen Gewichte sind: {final_weights}") else: print(f"Das Netzwerk hat XOR nach {epoch} Iterationen nicht korrekt gelernt.") # Testen des Netzwerks nach den Lern-Iterationen print("\nFinal Test Output:") for input_vector, target in zip(inputs, targets): weighted_sum = np.dot(input_vector, final_weights) output = 1 if lower_threshold < weighted_sum < upper_threshold else 0 print(f"Input: {input_vector}, Target: {target}, Output: {output}") # Optionally, print out the outputs of each epoch for transparency print("\nEpoch Outputs:") for epoch_index, epoch_outputs in enumerate(all_epoch_outputs): print(f"Epoch {epoch_index + 1}:") for input_vector, output, target in epoch_outputs: print(f" Input: {input_vector}, Output: {output}, Target: {target}")
Standard input is empty
Das Netzwerk hat XOR korrekt nach 10 Iterationen gelernt. Die Working Startgewichte waren: [0.90520255 0.71468416] Die finalen Gewichte sind: [0.87013699 0.88507291] 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: 1, 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: 0, Target: 1 Input: [1, 1], Output: 0, Target: 0 Epoch 3: 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 4: 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 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: 1, Target: 1 Input: [1, 1], Output: 1, Target: 0 Epoch 8: 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: 1, Target: 0 Epoch 9: 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 10: 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