init
This commit is contained in:
@@ -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)
|
||||||
+38
@@ -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, 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,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,0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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,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]]
|
||||||
|
]
|
||||||
@@ -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")
|
||||||
+49
@@ -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
|
||||||
|
|
||||||
@@ -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)
|
||||||
+32
@@ -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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user