main
Florian Babel 2026-10-26 10:14:33 +01:00
parent 2740fc8bf9
commit d6b2b5bfd3
3 changed files with 91 additions and 146 deletions

View File

@ -37,23 +37,22 @@ class SLN:
for it in range(maxIter): for it in range(maxIter):
Y = self.neuron(X) # nur ein Aufruf Y = self.neuron(X) # nur ein Aufruf
err = error_rate(Y, T) err = error_rate(Y, T)
if (it % 10) == 0: # if (it % 10) == 0:
print('#{} err:{}\n{}\n{}'.format(it, err, self._W, self._b)) print('#{} err:{}\n{}\n{}'.format(it, err, self._W, self._b))
nnwplot.plotTwoFeatures(X, T, self.neuron) nnwplot.plotTwoFeatures(X, T, self.neuron)
plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht) plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht)
if err <= maxErrorRate: if err <= maxErrorRate:
break break
# update _W
for n in range(N): for n in range(N):
for j in range(self.dIn): delta = T[n] - Y[n] # Fehlersignal pro Beispiel (mit Vorzeichen!)
weighted_error = err * X[j, n]
to_learn = weighted_error * eta # update _W
#print("{:.2f} = {} * {} * {}".format(to_learn, err, X[j, n], eta)) for j in range(self.dIn):
self._W[j, 0] += to_learn self._W[j, 0] += eta * delta * X[j, n]
# update _b
self._b[0][0] += err * eta # update _b
self._b[0][0] += eta * delta
...
print('#{} err:{}\n{}\n{}'.format(it, err, self._W, self._b)) print('#{} err:{}\n{}\n{}'.format(it, err, self._W, self._b))
nnwplot.plotTwoFeatures(X, T, self.neuron) nnwplot.plotTwoFeatures(X, T, self.neuron)
plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht) plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht)

View File

@ -4,26 +4,25 @@ from matplotlib import pyplot as plt
from matplotlib import colors from matplotlib import colors
def plotTwoFeatures(X,T,pred_func): def plotTwoFeatures(X, T, pred_func):
if X.ndim!=2: if X.ndim != 2:
raise ValueError('X be a matrix (2 dimensional array).') raise ValueError('X be a matrix (2 dimensional array).')
tp=False tp = False
if X.shape[0]!=2 and X.shape[1]==2: if X.shape[0] != 2 and X.shape[1] == 2:
X=X.T X = X.T
tp=True tp = True
if X.shape[0]!=2: if X.shape[0] != 2:
raise ValueError('X must contain exactly 2 features.') raise ValueError('X must contain exactly 2 features.')
# determine canvas borders # determine canvas borders
mins = np.amin(X,1); mins = np.amin(X, 1)
mins = mins - 0.1*np.abs(mins); mins = mins - np.where(mins == 0, 0.1, 0.1 * np.abs(mins))
maxs = np.amax(X,1); maxs = np.amax(X, 1)
maxs = maxs + 0.1*maxs; maxs = maxs + 0.1 * maxs
## generate dense grid ## generate dense grid
xs,ys = np.meshgrid(np.linspace(mins[0],maxs[0],300), xs, ys = np.meshgrid(np.linspace(mins[0], maxs[0], 300),
np.linspace(mins[1], maxs[1], 300)); np.linspace(mins[1], maxs[1], 300))
# evaluate model on the dense grid # evaluate model on the dense grid
try: try:
@ -32,18 +31,18 @@ def plotTwoFeatures(X,T,pred_func):
Z = pred_func(np.c_[xs.flatten(), ys.flatten()]); Z = pred_func(np.c_[xs.flatten(), ys.flatten()]);
if tp: if tp:
Z=Z.T Z = Z.T
T=T.T T = T.T
if Z.ndim>1 and Z.shape[0]>1: # onehot? -> convert if Z.ndim > 1 and Z.shape[0] > 1: # onehot? -> convert
Z=Z.argmax(0) Z = Z.argmax(0)
Z = Z.reshape(xs.shape) Z = Z.reshape(xs.shape)
if T.ndim>1 and T.shape[0]>1: # onehot? -> convert if T.ndim > 1 and T.shape[0] > 1: # onehot? -> convert
T=T.argmax(0) T = T.argmax(0)
# Plot the contour and training examples # Plot the contour and training examples
plt.contourf(xs, ys, Z, cmap=plt.cm.Spectral) plt.contourf(xs, ys, Z, cmap=plt.cm.Spectral)
plt.scatter(X[0,:], X[1,:], c=T, s=50, plt.scatter(X[0, :], X[1, :], c=T, s=50,
cmap=colors.ListedColormap(['orange', 'blue', 'green'])) cmap=colors.ListedColormap(['blue', 'green', 'red']))
plt.show() plt.show()

File diff suppressed because one or more lines are too long