From 86d5442838ba344bfee0992f51cf01b8b27e5da4 Mon Sep 17 00:00:00 2001 From: mirimatcode Date: Wed, 22 Jan 2025 17:37:26 +0100 Subject: [PATCH] primo push --- hidden_layer.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ percettrone.py | 26 +++++++++++++++ single_layer.py | 38 ++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 hidden_layer.py create mode 100644 percettrone.py create mode 100644 single_layer.py diff --git a/hidden_layer.py b/hidden_layer.py new file mode 100644 index 0000000..18bd7f8 --- /dev/null +++ b/hidden_layer.py @@ -0,0 +1,86 @@ +from percettrone import Percettrone + +# rete neurale +x = [(0,0),(0,1),(1,0),(1,1)] # Combinazioni +#output = (0,1,1,0) # XOR Logico +output = (0,0,0,1) # AND Logico +corrette = 0 + +#pin_est_1 = Percettrone(bias=-0.5) +#pin_est_2 = Percettrone(bias=-1.5) +#pinout = Percettrone(bias=-1) +pin_est_1 = Percettrone(w1=0.3, w2=4, bias=0.5) +pin_est_2 = Percettrone(w1=0.2, w2=4, bias=-0.5) +pinout = Percettrone(w1=0.3, w2=2, bias=0) + +def stampa_operazione(p, y, x1, x2, isFinal, errore = 0): + if isFinal == 0: + print(f"\tW1: {p.w1}") + print(f"X1: {x1} --------> ") + print(f"\t\t( bias: {p.bias} ) -------> Y: {y}") + print(f"X2: {x2} --------> ") + print(f"\tW2: {p.w2}") + elif isFinal == 1: + print(f"\t\t\t\tW1: {p.w1}") + print(f"\t\t\tX1: {x1} --------> ") + print(f"\t\t\t\t\t( bias: {p.bias} ) -------> Y: {y}") + print(f"\t\t\tX2: {x2} --------> ") + print(f"\t\t\t\tW2: {p.w2}") + else: + print(f"\t\t\t\t\t\tW1: {p.w1}") + print(f"\t\t\t\t\tX1: {x1} --------> ") + print(f"\t\t\t\t\t\t\t( bias: {p.bias} ) -------> Y: {y} ----> errore: {errore}") + print(f"\t\t\t\t\tX2: {x2} --------> ") + print(f"\t\t\t\t\t\tW2: {p.w2}") +def stampa_risultati_multilayer(): + print("Percettrone Esterno 1:") + print(f"\t W1: {pin_est_1.w1}, W2: {pin_est_1.w2}, bias: {pin_est_1.bias}") + print("Percettrone Esterno 2:") + print(f"\t W1: {pin_est_2.w1}, W2: {pin_est_2.w2}, bias: {pin_est_2.bias}") + #print("Percettrone Interno 1:") + #print(f"\t W1: {pin_int_1.w1}, W2: {pin_int_1.w2}, bias: {pin_int_1.bias}") + #print("Percettrone Interno 2:") + #print(f"\t W1: {pin_int_2.w1}, W2: {pin_int_2.w2}, bias: {pin_int_2.bias}") + print("Percettrone OUT:") + print(f"\t W1: {pinout.w1}, W2: {pinout.w2}, bias: {pinout.bias}") + +for i in range(1,10000): #Epoche + + if corrette == 4: + print(f"Epoche necessarie: {i-1}") + stampa_risultati_multilayer() + break + + print(f"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tEPOCA {i}") + corrette = 0; + + for j in range(0,4): #Combinazioni + print("\n") + y_est_1 = pin_est_1.funzione_gradino(x[j][0], x[j][1]) + y_est_2 = pin_est_2.funzione_gradino(x[j][0], x[j][1]) + + #y_int_1 = pin_int_1.funzione_gradino(y_est_1, y_est_2) + #y_int_2 = pin_int_2.funzione_gradino(y_est_1, y_est_2) + + yout = pinout.funzione_gradino(y_est_1, y_est_2) + errore = pinout.valuta(y_est_1, y_est_2, output[j]) + + stampa_operazione(pin_est_1, y_est_1, x[j][0], x[j][1], 0) + #stampa_operazione(pin_int_1, y_int_1, y_est_1, y_est_2, 1) + stampa_operazione(pinout, yout, y_est_1, y_est_2, 2, errore) + #stampa_operazione(pin_int_2, y_int_2, y_est_1, y_est_2, 1) + stampa_operazione(pin_est_2, y_est_2, x[j][0], x[j][1], 0) + + if errore != 0: + #if y_est_1 != output[j]: + pin_est_1.correggi_pesi(x[j][0], x[j][1], errore) + #if y_est_2 != output[j]: + pin_est_2.correggi_pesi(x[j][0], x[j][1], errore) + #if y_int_1 != output[j]: + #pin_int_1.correggi_pesi(y_est_1, y_est_2, errore) + #if y_int_2 != output[j]: + #pin_int_2.correggi_pesi(y_est_1, y_est_2, errore) + + pinout.correggi_pesi(y_est_1, y_est_2, errore) + else: + corrette += 1 \ No newline at end of file diff --git a/percettrone.py b/percettrone.py new file mode 100644 index 0000000..3c2e993 --- /dev/null +++ b/percettrone.py @@ -0,0 +1,26 @@ +class Percettrone: + + def __init__(self, w1 = 0, w2 = 0, bias = 0, lre = 1): + self.w1 = w1 + self.w2 = w2 + self.bias = bias + self.lre = lre + + def funzione_gradino(self, x1, x2): + if ((x1 * self.w1) + (x2 * self.w2) + self.bias) >= 0: + return 1 + return 0 + + def valuta(self, x1, x2, risultato_atteso): + y = self.funzione_gradino(x1, x2) + errore = risultato_atteso - y + return errore + + def correggi_pesi(self, x1, x2, errore): + self.bias = self.bias + (errore * self.lre) + #print(f"errore: {errore * self.bias}") + self.w1 = self.w1 + (errore * x1 * self.lre) + self.w2 = self.w2 + (errore * x2 * self.lre) + + def get_pesi(self): + return (self.w1, self.w2, self.bias) \ No newline at end of file diff --git a/single_layer.py b/single_layer.py new file mode 100644 index 0000000..1b062da --- /dev/null +++ b/single_layer.py @@ -0,0 +1,38 @@ +from percettrone import Percettrone + +x = [(0,0),(0,1),(1,0),(1,1)] # Combinazioni +output = (0,0,0,1) # AND Logico + +p = Percettrone() +corrette = 0 #Fermo le epoche se termina prima + +def stampa_operazione(p, y, x1, x2): + print("\n") + print(f"\tW1: {p.w1}") + print(f"X1: {x1} --------> ") + print(f"\t\t( bias: {p.bias} ) -------> Y: {y} ----> errore: {errore}") + print(f"X2: {x2} --------> ") + print(f"\tW2: {p.w2}") + +# Singolo percettrone AND, OR +def stampa_risultati_single_layer(): + print("Percettrone:") + print(f"\t W1: {p.w1}, W2: {p.w2}, bias: {p.bias}") + +for i in range(1,100): #Epoche + if corrette == 4: + print(f"Epoche necessarie: {i-1}") + print(f"\t W1: {p.w1}, W2: {p.w2}, bias: {p.bias}") + break + + print(f"\t\t\t\tEPOCA {i}") + corrette = 0; + + for j in range(0,4): #Combinazioni + y = p.funzione_gradino(x[j][0], x[j][1]) + errore = p.valuta(x[j][0], x[j][1], output[j]) + stampa_operazione(p, y, x[j][0], x[j][1]) + if errore != 0: + p.correggi_pesi(x[j][0], x[j][1], errore) + else: + corrette += 1