übung 3
parent
c978f95588
commit
21bdd9376a
|
|
@ -0,0 +1,57 @@
|
||||||
|
import numpy as np
|
||||||
|
import nnwplot
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
|
def ErrorRate(Y, T):
|
||||||
|
if Y.ndim == 1 or Y.shape[0] == 1:
|
||||||
|
errors = Y != T
|
||||||
|
return errors.sum() / Y.size
|
||||||
|
else: # für mehrere Ausgaben in one-hot Kodierung:
|
||||||
|
errors = Y.argmax(0) != T.argmax(0)
|
||||||
|
return errors.sum() / Y.shape[1]
|
||||||
|
|
||||||
|
|
||||||
|
class SLN:
|
||||||
|
def __init__(self, dIn, cOut):
|
||||||
|
self.dIn = dIn
|
||||||
|
self.cOut = cOut
|
||||||
|
np.random.seed(42)
|
||||||
|
self._b = np.zeros((cOut, 1))
|
||||||
|
self._W = np.random.randn(dIn, cOut) / np.sqrt(dIn + 1)
|
||||||
|
|
||||||
|
def neuron(self, X):
|
||||||
|
N = X.shape[1]
|
||||||
|
net = np.zeros(N)
|
||||||
|
|
||||||
|
for n in range(0, N):
|
||||||
|
value = 0
|
||||||
|
for i in range(self.dIn):
|
||||||
|
value += X[i, n] * self._W[i][0]
|
||||||
|
net[n] += value
|
||||||
|
return net > self._b[0]
|
||||||
|
|
||||||
|
def delta_train(self, X, T, eta, maxIter, maxErrorRate):
|
||||||
|
N = X.shape[1] # Anzahl Trainingsdaten
|
||||||
|
plt.ion() # interactive mode on
|
||||||
|
for it in range(maxIter):
|
||||||
|
Y = self.neuron(X) # nur ein Aufruf
|
||||||
|
err = ErrorRate(Y, T)
|
||||||
|
if (it % 20) == 0:
|
||||||
|
print('#{} err:{}\n{}\n{}'.format(it, err, self._W, self._b))
|
||||||
|
nnwplot.plotTwoFeatures(X, T, self.neuron)
|
||||||
|
plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht)
|
||||||
|
if err <= maxErrorRate:
|
||||||
|
break
|
||||||
|
# update _W
|
||||||
|
for n in range(N):
|
||||||
|
error = T[n] - Y[n]
|
||||||
|
for j in range(self.dIn):
|
||||||
|
weighted_error = error * X[j, n]
|
||||||
|
to_learn = weighted_error * eta
|
||||||
|
self._W[j, 0] += to_learn
|
||||||
|
# update _b
|
||||||
|
...
|
||||||
|
print('#{} err:{}\n{}\n{}'.format(it, err, self._W, self._b))
|
||||||
|
nnwplot.plotTwoFeatures(X, T, self.neuron)
|
||||||
|
plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht)
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
# %%
|
|
||||||
import numpy as np
|
|
||||||
import nnwplot
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
# %% ErrorRate
|
|
||||||
def ErrorRate(Y,T):
|
|
||||||
if Y.ndim==1 or Y.shape[0]==1:
|
|
||||||
errors=Y!=T
|
|
||||||
return errors.sum()/Y.size
|
|
||||||
else: # für mehrere Ausgaben in one-hot Kodierung:
|
|
||||||
errors=Y.argmax(0)!=T.argmax(0)
|
|
||||||
return errors.sum()/Y.shape[1]
|
|
||||||
|
|
||||||
# %%
|
|
||||||
class SLN:
|
|
||||||
def __init__(...): # Konstruktor
|
|
||||||
np.random.seed(42)
|
|
||||||
self._b=np.zeros((AAA,1))
|
|
||||||
self._W=np.random.randn(BBB,CCC)/np.sqrt(DDD)
|
|
||||||
|
|
||||||
# def DeltaTrain(self, X, T, eta, maxIter, maxErrorRate):
|
|
||||||
# N=X.shape[1] # Anzahl Trainingsdaten
|
|
||||||
# plt.ion() # interactive mode on
|
|
||||||
# for it in range(maxIter):
|
|
||||||
# Y = ... # nur ein Aufruf
|
|
||||||
# err = ErrorRate(..., ...)
|
|
||||||
# if (it%20) == 0:
|
|
||||||
# print('#{} err:{}\n{}\n{}'.format(it,err,self._W,self._b))
|
|
||||||
# nnwplot.plotTwoFeatures(...)
|
|
||||||
# plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht)
|
|
||||||
# if err <= maxErrorRate:
|
|
||||||
# break
|
|
||||||
# # update _W
|
|
||||||
# ...
|
|
||||||
# # update _b
|
|
||||||
# ...
|
|
||||||
# print('#{} err:{}\n{}\n{}'.format(it,err,self._W,self._b))
|
|
||||||
# nnwplot.plotTwoFeatures(...)
|
|
||||||
# plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht)
|
|
||||||
#
|
|
||||||
# input("Press Enter to continue...") # wenn gewünscht ...
|
|
||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue