updated iRProp-, added comments perceptron
parent
919eed574a
commit
115d78e063
|
@ -15,9 +15,11 @@ grad = np.zeros(3)
|
||||||
def sigmoid(summe): # Transferfunktion
|
def sigmoid(summe): # Transferfunktion
|
||||||
return 1.0/(1.0+np.exp(-1.0*summe))
|
return 1.0/(1.0+np.exp(-1.0*summe))
|
||||||
|
|
||||||
|
# Schwellwertfunktion
|
||||||
def perceptron(output):
|
def perceptron(output):
|
||||||
return max(np.sign(output), 0)
|
return max(np.sign(output), 0)
|
||||||
|
|
||||||
|
# Vektorisieren der Schwellwertfunktion
|
||||||
vperceptron = np.vectorize(perceptron)
|
vperceptron = np.vectorize(perceptron)
|
||||||
|
|
||||||
def learn():
|
def learn():
|
||||||
|
@ -25,10 +27,13 @@ def learn():
|
||||||
global train, weight, out, target, learnrate
|
global train, weight, out, target, learnrate
|
||||||
# Neuronenausgabe für alle 4 Trainingsmuster berechnen
|
# Neuronenausgabe für alle 4 Trainingsmuster berechnen
|
||||||
|
|
||||||
|
# Ausgabe des Neurons
|
||||||
out = vperceptron(np.matmul(train, weight))
|
out = vperceptron(np.matmul(train, weight))
|
||||||
|
|
||||||
|
|
||||||
for j in range(4):
|
for j in range(4):
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
|
# Anpassung der Gewichte nach Perzeptronlernregel
|
||||||
if train[j][i] == 1 and out[j] == 0 and target[j] == 1:
|
if train[j][i] == 1 and out[j] == 0 and target[j] == 1:
|
||||||
weight[i] = weight[i] + train[j][i]
|
weight[i] = weight[i] + train[j][i]
|
||||||
elif train[j][i] == 1 and out[j] == 1 and target[j] == 0:
|
elif train[j][i] == 1 and out[j] == 1 and target[j] == 0:
|
||||||
|
|
|
@ -23,11 +23,9 @@ inp_size = 3 # Eingabeneuronen
|
||||||
hid_size = 4 # Hidden-Neuronen
|
hid_size = 4 # Hidden-Neuronen
|
||||||
out_size = 1 # Ausgabeneuron
|
out_size = 1 # Ausgabeneuron
|
||||||
|
|
||||||
delta_L1 = np.ones((inp_size, hid_size)) * 0.125
|
delta_L1 = np.ones((inp_size, hid_size)) * 0.1
|
||||||
delta_L2 = np.ones((hid_size, out_size)) * 0.125
|
delta_L2 = np.ones((hid_size, out_size)) * 0.1
|
||||||
|
|
||||||
delta_min = 0
|
|
||||||
delta_max = 50
|
|
||||||
|
|
||||||
# Gewichte zufällig initialisieren (Mittelwert = 0)
|
# Gewichte zufällig initialisieren (Mittelwert = 0)
|
||||||
w0 = np.random.random((inp_size, hid_size)) - 0.5
|
w0 = np.random.random((inp_size, hid_size)) - 0.5
|
||||||
|
@ -41,14 +39,13 @@ def multiply_learnrate(old, new):
|
||||||
return 0.5
|
return 0.5
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
v_multiply_learnrate = np.vectorize(multiply_learnrate)
|
v_multiply_learnrate = np.vectorize(multiply_learnrate)
|
||||||
|
|
||||||
L2_grad_old = np.zeros((4, 1))
|
L1_grad_old = np.zeros((inp_size, hid_size))
|
||||||
L1_grad_old = np.zeros((3, 4))
|
L2_grad_old = np.zeros((hid_size, out_size))
|
||||||
|
|
||||||
# Netzwerk trainieren
|
# Netzwerk trainieren
|
||||||
for i in range(100):
|
for i in range(1000):
|
||||||
|
|
||||||
# Vorwärtsaktivierung
|
# Vorwärtsaktivierung
|
||||||
L0 = inp
|
L0 = inp
|
||||||
|
@ -65,19 +62,18 @@ for i in range(100):
|
||||||
L1_delta = L1_error * deriv_sigmoid(L1)
|
L1_delta = L1_error * deriv_sigmoid(L1)
|
||||||
|
|
||||||
# Gradienten
|
# Gradienten
|
||||||
L2_grad_new = np.matmul(L1.T, L2_delta)
|
L2_grad_new = np.matmul(L1.T, L2_error)
|
||||||
L1_grad_new = np.matmul(L0.T, L1_delta)
|
L1_grad_new = np.matmul(L0.T, L1_error)
|
||||||
|
|
||||||
# Gewichte aktualisieren
|
# Gewichte aktualisieren
|
||||||
learnrate = 0.1
|
|
||||||
|
|
||||||
delta_L1 = np.clip(
|
delta_L1 = np.clip(
|
||||||
delta_L1 * v_multiply_learnrate(L1_grad_old, L1_grad_new), 0, 50)
|
delta_L1 * v_multiply_learnrate(L1_grad_old, L1_grad_new), 0, 50)
|
||||||
delta_L2 = np.clip(
|
delta_L2 = np.clip(
|
||||||
delta_L2 * v_multiply_learnrate(L2_grad_old, L2_grad_new), 0, 50)
|
delta_L2 * v_multiply_learnrate(L2_grad_old, L2_grad_new), 0, 50)
|
||||||
|
|
||||||
w1 -= learnrate * np.sign(L2_grad_new) * delta_L2
|
w1 -= np.sign(L2_grad_new) * delta_L2
|
||||||
w0 -= learnrate * np.sign(L1_grad_new) * delta_L1
|
w0 -= np.sign(L1_grad_new) * delta_L1
|
||||||
|
|
||||||
# Gradienten aktualisieren
|
# Gradienten aktualisieren
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue