From d95ba0a638252cccbaf8b8aba229b4d3c14adf29 Mon Sep 17 00:00:00 2001 From: Jonas Braus Date: Wed, 4 Mar 2026 23:27:19 +0100 Subject: [PATCH] init --- connection.py | 23 +++++++++++++++++++++++ data.train | 38 ++++++++++++++++++++++++++++++++++++++ main.py | 28 ++++++++++++++++++++++++++++ network.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ neuron.py | 39 +++++++++++++++++++++++++++++++++++++++ trainer.py | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 209 insertions(+) create mode 100644 connection.py create mode 100644 data.train create mode 100644 main.py create mode 100644 network.py create mode 100644 neuron.py create mode 100644 trainer.py diff --git a/connection.py b/connection.py new file mode 100644 index 0000000..5f27b5e --- /dev/null +++ b/connection.py @@ -0,0 +1,23 @@ +import random + +class Connection: + def __init__(self, in_, out_): + self.weight = random.uniform(-1.0, 1.0) + self.in_ = in_ + self.out_ = out_ + self.value = 0.0 + + def transfer(self, value:float): + self.value = value + + def adjust(self, delta:float, learning_rate:float): + old_weight = self.weight + + gradient = delta * self.value + self.weight += learning_rate * gradient + + if self.in_ is None: + return + + error_for_prev = delta * old_weight + self.in_.adjust(error_for_prev, learning_rate) \ No newline at end of file diff --git a/data.train b/data.train new file mode 100644 index 0000000..728c8e1 --- /dev/null +++ b/data.train @@ -0,0 +1,38 @@ +[ + [[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0, 1, 0]], + [[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1, 0, 0]], + [[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1, 0, 0]], + [[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,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+ [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0], [0, 0, 1]], + [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0], [0, 0, 1]], + [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0], [0, 0, 1]], + [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0], [0, 0, 1]], + [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0], [0, 0, 1]], + [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0], [0, 0, 1]], + [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0], [0, 0, 1]], + [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [0, 0, 1]] +] \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..8a1b505 --- /dev/null +++ b/main.py @@ -0,0 +1,28 @@ +import string +from network import Network +from trainer import Trainer + +network = Network() +network.build_layers(3, 36, 3, 12) +network.connect_layers() + +trainer = Trainer(network, "./data.train") +trainer.train(20000) + +print("Erkenne die Art eines zeichens (Konsontant, Vokal oder Zahl).") +while True: + inpt = input("Dein Zeichen: ").lower() + + vec = [0 for _ in range(0, 36)] + refrence = string.ascii_lowercase + string.digits + vec[refrence.index(inpt)] = 1 + + output = network.fire(vec) + + print(f"Konsontant: {round(output[0], 2)} | Vokal: {round(output[1], 2)} | Zahl: {round(output[2], 2)}") + if max(output) == output[0]: + print("AI Denkt: Konsontant") + elif max(output) == output[1]: + print("AI Denkt: Vokal") + else: + print("AI Denkt: Zahl") \ No newline at end of file diff --git a/network.py b/network.py new file mode 100644 index 0000000..e96e6ee --- /dev/null +++ b/network.py @@ -0,0 +1,49 @@ +from neuron import Neuron +from connection import Connection +import random +import json + +class Network(): + def __init__(self): + self.layers = [] + + def build_layers(self, count:int, input_size:int, output_size:int, layer_size:int): + + for layer in range(0, count): + self.layers.append([]) + + for _ in range(0, input_size if layer == 0 else output_size if layer == count - 1 else layer_size): + self.layers[layer].append(Neuron()) + + + def _connect_layer(self, last_layer, current_layer): + for last_neuron in last_layer: + for current_neuron in current_layer: + connection = Connection(last_neuron, current_neuron) + last_neuron.connect_out(connection) + current_neuron.connect_in(connection) + + def connect_layers(self): + + for i in range(1, len(self.layers)): + last_layer = self.layers[i-1] + current_layer = self.layers[i] + + self._connect_layer(last_layer, current_layer) + + def fire(self, vec): + + for l in range(0, len(self.layers)): + layer = self.layers[l] + for i in range(0, len(layer)): + neuron = layer[i] + neuron.calc(vec[i] if l == 0 else None) + + output = [] + + for i in range(0, len(self.layers[-1])): + layer = self.layers[-1] + output.append(layer[i].value) + + return output + diff --git a/neuron.py b/neuron.py new file mode 100644 index 0000000..3a80ab1 --- /dev/null +++ b/neuron.py @@ -0,0 +1,39 @@ +import random +import math + +class Neuron: + def __init__(self): + self.bias = random.uniform(-1.0, 1.0) + self.in_ = [] + self.out_ = [] + self.value = 0.0 + + def connect_in(self, connection): + self.in_.append(connection) + + def connect_out(self, connection): + self.out_.append(connection) + + def calc(self, input_val=None): + if input_val is not None: + self.value = input_val + else: + z = self.bias + for connection in self.in_: + z += connection.value * connection.weight + + z = max(-500, min(500, z)) + self.value = 1.0 / (1.0 + math.exp(-z)) + + for connection in self.out_: + connection.transfer(self.value) + + def adjust(self, error:float, learning_rate:float = 0.1): + derivative = self.value * (1.0 - self.value) + + delta = error * derivative + + self.bias += learning_rate * delta + + for connection in self.in_: + connection.adjust(delta, learning_rate) \ No newline at end of file diff --git a/trainer.py b/trainer.py new file mode 100644 index 0000000..692bac7 --- /dev/null +++ b/trainer.py @@ -0,0 +1,32 @@ +from network import Network +import json +import random + +class Trainer: + def __init__(self, network: Network, train_file): + self.network = network + + with open(train_file, "r") as file: + f_data = file.read() + self.data = json.loads(f_data) + + def adjust(self, check_vec, layer_idx): + layer = self.network.layers[layer_idx] + + for i, neuron in enumerate(layer): + error = check_vec[i] - neuron.value + neuron.adjust(error) + + def train(self, iters): + for i in range(0, iters): + print(f"Train {(i / iters) * 100}%") + sample = random.choice(self.data) + in_vec = sample[0] + check_vec = sample[1] + + self.network.fire(in_vec) + + self.adjust(check_vec, len(self.network.layers) - 1) + + + \ No newline at end of file