initial.
This commit is contained in:
55
experiments/neuron.py
Normal file
55
experiments/neuron.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import random
|
||||
import math
|
||||
|
||||
|
||||
class SimpleNeuron:
|
||||
def __init__(self):
|
||||
# Initialisiere Gewichte und Bias mit Zufallswerten zwischen -0.5 und 0.5
|
||||
self.weights = [random.uniform(-0.5, 0.5) for _ in range(2)]
|
||||
self.bias = random.uniform(-0.5, 0.5)
|
||||
|
||||
def dot(self, input_vector):
|
||||
"""
|
||||
Berechnet das Skalarprodukt zwischen dem Eingabevektor und den Gewichten,
|
||||
fügt den Bias hinzu und gibt das Ergebnis zurück.
|
||||
"""
|
||||
acc = sum(i * w for i, w in zip(input_vector, self.weights))
|
||||
return acc + self.bias
|
||||
|
||||
def process_input(self, input_vector):
|
||||
"""
|
||||
Verarbeitet den Eingabevektor, berechnet den Dot-Produkt,
|
||||
wendet die Aktivierungsfunktion (tanh) an und gibt das Ergebnis zurück.
|
||||
"""
|
||||
print("**** Processing ****")
|
||||
print(f"Input: {input_vector}")
|
||||
print(f"Using Weights: {self.weights} and Bias: {self.bias}")
|
||||
|
||||
dot_product = self.dot(input_vector)
|
||||
output = math.tanh(dot_product)
|
||||
|
||||
print(f"Output: {output}")
|
||||
return output
|
||||
|
||||
|
||||
def sense(in_neuron, signal):
|
||||
"""
|
||||
Diese Funktion überprüft, ob das Signal eine Liste der Länge 2 ist,
|
||||
und sendet es an das Neuron zur Verarbeitung.
|
||||
"""
|
||||
if isinstance(signal, list) and len(signal) == 2:
|
||||
output = in_neuron.process_input(signal)
|
||||
print(f"Final Output: {output}")
|
||||
else:
|
||||
print("The Signal must be a list of length 2")
|
||||
|
||||
# Beispiel zur Verwendung:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Erstelle ein Neuron
|
||||
neuron = SimpleNeuron()
|
||||
|
||||
# Sende ein Signal (Eingabevektor) an das Neuron
|
||||
test_signal = [0.5, -0.3] # Beispiel-Eingabe
|
||||
sense(neuron, test_signal)
|
||||
Reference in New Issue
Block a user