funziona and e or solo se metto bias < -1 in uno dei percettroni input
This commit is contained in:
@@ -3,15 +3,16 @@ from percettrone import Percettrone
|
|||||||
# rete neurale
|
# rete neurale
|
||||||
x = [(0,0),(0,1),(1,0),(1,1)] # Combinazioni
|
x = [(0,0),(0,1),(1,0),(1,1)] # Combinazioni
|
||||||
#output = (0,1,1,0) # XOR Logico
|
#output = (0,1,1,0) # XOR Logico
|
||||||
output = (0,0,0,1) # AND Logico
|
#output = (0,0,0,1) # AND Logico
|
||||||
|
output = (0,1,1,1) # OR Logico
|
||||||
corrette = 0
|
corrette = 0
|
||||||
|
|
||||||
#pin_est_1 = Percettrone(bias=-0.5)
|
pin_est_1 = Percettrone(bias=0)
|
||||||
#pin_est_2 = Percettrone(bias=-1.5)
|
pin_est_2 = Percettrone(bias=-1.5)
|
||||||
#pinout = Percettrone(bias=-1)
|
pinout = Percettrone(bias=0)
|
||||||
pin_est_1 = Percettrone(w1=0.3, w2=4, bias=0.5)
|
""" pin_est_1 = Percettrone(w1=0.3, w2=4, bias=-0.5)
|
||||||
pin_est_2 = Percettrone(w1=0.2, w2=4, bias=-0.5)
|
pin_est_2 = Percettrone(w1=0.2, w2=4, bias=-1.5)
|
||||||
pinout = Percettrone(w1=0.3, w2=2, bias=0)
|
pinout = Percettrone(w1=0.3, w2=2, bias=-1) """
|
||||||
|
|
||||||
def stampa_operazione(p, y, x1, x2, isFinal, errore = 0):
|
def stampa_operazione(p, y, x1, x2, isFinal, errore = 0):
|
||||||
if isFinal == 0:
|
if isFinal == 0:
|
||||||
@@ -44,7 +45,7 @@ def stampa_risultati_multilayer():
|
|||||||
print("Percettrone OUT:")
|
print("Percettrone OUT:")
|
||||||
print(f"\t W1: {pinout.w1}, W2: {pinout.w2}, bias: {pinout.bias}")
|
print(f"\t W1: {pinout.w1}, W2: {pinout.w2}, bias: {pinout.bias}")
|
||||||
|
|
||||||
for i in range(1,10000): #Epoche
|
for i in range(1,100000): #Epoche
|
||||||
|
|
||||||
if corrette == 4:
|
if corrette == 4:
|
||||||
print(f"Epoche necessarie: {i-1}")
|
print(f"Epoche necessarie: {i-1}")
|
||||||
@@ -58,29 +59,17 @@ for i in range(1,10000): #Epoche
|
|||||||
print("\n")
|
print("\n")
|
||||||
y_est_1 = pin_est_1.funzione_gradino(x[j][0], x[j][1])
|
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_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)
|
yout = pinout.funzione_gradino(y_est_1, y_est_2)
|
||||||
errore = pinout.valuta(y_est_1, y_est_2, output[j])
|
|
||||||
|
errore = output[j] - yout
|
||||||
|
|
||||||
stampa_operazione(pin_est_1, y_est_1, x[j][0], x[j][1], 0)
|
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(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)
|
stampa_operazione(pin_est_2, y_est_2, x[j][0], x[j][1], 0)
|
||||||
|
|
||||||
if errore != 0:
|
if errore != 0:
|
||||||
#if y_est_1 != output[j]:
|
|
||||||
pin_est_1.correggi_pesi(x[j][0], x[j][1], errore)
|
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)
|
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)
|
pinout.correggi_pesi(y_est_1, y_est_2, errore)
|
||||||
else:
|
else:
|
||||||
corrette += 1
|
corrette += 1
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
class Percettrone:
|
class Percettrone:
|
||||||
|
|
||||||
def __init__(self, w1 = 0, w2 = 0, bias = 0, lre = 1):
|
def __init__(self, w1 = 1, w2 = 1, bias = 1, lre = 1):
|
||||||
self.w1 = w1
|
self.w1 = w1
|
||||||
self.w2 = w2
|
self.w2 = w2
|
||||||
self.bias = bias
|
self.bias = bias
|
||||||
@@ -11,16 +11,7 @@ class Percettrone:
|
|||||||
return 1
|
return 1
|
||||||
return 0
|
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):
|
def correggi_pesi(self, x1, x2, errore):
|
||||||
self.bias = self.bias + (errore * self.lre)
|
self.bias = self.bias + (errore * self.lre)
|
||||||
#print(f"errore: {errore * self.bias}")
|
|
||||||
self.w1 = self.w1 + (errore * x1 * self.lre)
|
self.w1 = self.w1 + (errore * x1 * self.lre)
|
||||||
self.w2 = self.w2 + (errore * x2 * self.lre)
|
self.w2 = self.w2 + (errore * x2 * self.lre)
|
||||||
|
|
||||||
def get_pesi(self):
|
|
||||||
return (self.w1, self.w2, self.bias)
|
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
from percettrone import Percettrone
|
from percettrone import Percettrone
|
||||||
|
|
||||||
x = [(0,0),(0,1),(1,0),(1,1)] # Combinazioni
|
x = [(0,0),(0,1),(1,0),(1,1)] # Combinazioni
|
||||||
output = (0,0,0,1) # AND Logico
|
#output = (0,0,0,1) # AND Logico
|
||||||
|
output = (0,1,1,1) # OR Logico
|
||||||
|
|
||||||
p = Percettrone()
|
p = Percettrone()
|
||||||
corrette = 0 #Fermo le epoche se termina prima
|
corrette = 0 #Fermo le epoche se termina prima
|
||||||
@@ -30,7 +31,7 @@ for i in range(1,100): #Epoche
|
|||||||
|
|
||||||
for j in range(0,4): #Combinazioni
|
for j in range(0,4): #Combinazioni
|
||||||
y = p.funzione_gradino(x[j][0], x[j][1])
|
y = p.funzione_gradino(x[j][0], x[j][1])
|
||||||
errore = p.valuta(x[j][0], x[j][1], output[j])
|
errore = output[j] - y
|
||||||
stampa_operazione(p, y, x[j][0], x[j][1])
|
stampa_operazione(p, y, x[j][0], x[j][1])
|
||||||
if errore != 0:
|
if errore != 0:
|
||||||
p.correggi_pesi(x[j][0], x[j][1], errore)
|
p.correggi_pesi(x[j][0], x[j][1], errore)
|
||||||
|
|||||||
3
tempCodeRunnerFile.py
Normal file
3
tempCodeRunnerFile.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
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=-1)
|
||||||
Reference in New Issue
Block a user