Created
April 8, 2023 11:52
-
-
Save ulrischa/048cddddb735ddd7698cc375b4a5ba6e to your computer and use it in GitHub Desktop.
Neuronales Netzwerk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# Eingabedaten | |
X = np.array([[0, 0, 1], | |
[0, 1, 1], | |
[1, 0, 1], | |
[1, 1, 1]]) | |
# Erwartete Ausgabedaten | |
y = np.array([[0], | |
[1], | |
[1], | |
[0]]) | |
# Anzahl der Eingabe- und Ausgabeneuronen | |
input_neurons = X.shape[1] | |
output_neurons = y.shape[1] | |
# Anzahl der Neuronen in der versteckten Schicht | |
hidden_neurons = 3 | |
# Definition der Aktivierungsfunktion (Sigmoid) | |
def sigmoid(x): | |
return 1 / (1 + np.exp(-x)) | |
# Definition der Ableitung der Aktivierungsfunktion (Sigmoid) | |
def sigmoid_derivative(x): | |
return x * (1 - x) | |
# Initialisierung der Gewichtungsmatrizen | |
# Gewichte zwischen Eingabe- und versteckter Schicht | |
weights_input_hidden = np.random.rand(input_neurons, hidden_neurons) | |
# Gewichte zwischen versteckter Schicht und Ausgabe | |
weights_hidden_output = np.random.rand(hidden_neurons, output_neurons) | |
# Anzahl der Epochen für das Training | |
epochs = 5000 | |
# Trainingschleife | |
for epoch in range(epochs): | |
# Vorwärtspropagation (Forward Propagation) | |
hidden_layer_input = np.dot(X, weights_input_hidden) | |
hidden_layer_output = sigmoid(hidden_layer_input) | |
output = sigmoid(np.dot(hidden_layer_output, weights_hidden_output)) | |
# Berechnung des Fehlers (Error) | |
error = y - output | |
# Rückwärtspropagation (Backpropagation) und Gewichtsanpassung | |
d_weights_hidden_output = np.dot(hidden_layer_output.T, error * sigmoid_derivative(output)) | |
d_weights_input_hidden = np.dot(X.T, np.dot(error * sigmoid_derivative(output), weights_hidden_output.T) * sigmoid_derivative(hidden_layer_output)) | |
weights_hidden_output += d_weights_hidden_output | |
weights_input_hidden += d_weights_input_hidden | |
# Ausgabe der finalen Vorhersage | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment