73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
import matplotlib.pyplot as plt
|
|
from mpl_toolkits.mplot3d import Axes3D
|
|
import numpy as np
|
|
# BIAS,x,y
|
|
train = np.array( [[1,0,0],
|
|
[1,1,0],
|
|
[1,0,1],
|
|
[1,1,1]])
|
|
target = np.array([0,0,0,1]) # AND Operation
|
|
out = np.array([0,0,0,0])
|
|
weight = np.random.rand(3)*(0.5)
|
|
learnrate = 1.0
|
|
grad = np.zeros(3)
|
|
|
|
def sigmoid(summe): # Transferfunktion
|
|
return 1.0/(1.0+np.exp(-1.0*summe))
|
|
|
|
# Schwellwertfunktion
|
|
def perceptron(output):
|
|
return max(np.sign(output), 0)
|
|
|
|
# Vektorisieren der Schwellwertfunktion
|
|
vperceptron = np.vectorize(perceptron)
|
|
|
|
def learn():
|
|
#TODO implement here
|
|
global train, weight, out, target, learnrate
|
|
# Neuronenausgabe für alle 4 Trainingsmuster berechnen
|
|
|
|
# Ausgabe des Neurons
|
|
out = vperceptron(np.matmul(train, weight))
|
|
|
|
|
|
for j in range(4):
|
|
for i in range(3):
|
|
# Anpassung der Gewichte nach Perzeptronlernregel
|
|
if train[j][i] == 1 and out[j] == 0 and target[j] == 1:
|
|
weight[i] = weight[i] + train[j][i]
|
|
elif train[j][i] == 1 and out[j] == 1 and target[j] == 0:
|
|
weight[i] = weight[i] - train[j][i]
|
|
|
|
|
|
def outp(N=100): # Daten für die Ausgabefunktion generieren
|
|
global weight
|
|
x = np.linspace(0, 1, N)
|
|
y = np.linspace(0, 1, N)
|
|
xx, yy = np.meshgrid(x, y)
|
|
oo = vperceptron(weight[0] + weight[1]*xx + weight[2]*yy)
|
|
return xx, yy, oo
|
|
|
|
def on_close(event): # Fenster schließen
|
|
exit(0)
|
|
|
|
plt.ion()
|
|
fig = plt.figure()
|
|
fig.canvas.mpl_connect('close_event', on_close)
|
|
while True: # Endlosschleife
|
|
#for i in range(1000):
|
|
learn() # lerne einen Schritt
|
|
plt.clf() # Bildschirm löschen
|
|
X, Y, Z = outp() # generiere Plotdaten
|
|
ax = fig.add_subplot(111, projection='3d')
|
|
# 3D plot von den Daten
|
|
ax.plot_surface(X, Y, Z, edgecolor='royalblue',
|
|
lw=0.5, rstride=8, cstride=8, alpha=0.3)
|
|
ax.set_title('Neuron lernt AND-Funktion')
|
|
ax.set_xlabel('In[1]')
|
|
ax.set_ylabel('In[2]')
|
|
ax.set_zlabel('Ausgabe\ndes Neurons')
|
|
ax.set_zlim(0, 1)
|
|
plt.draw()
|
|
plt.pause(0.3)
|