continue exercise 3, not done yet

main
Nikola Sebastian Munder 2026-10-25 19:39:17 +01:00
parent a637362219
commit 512c21bcd1
3 changed files with 258 additions and 14 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,60 @@
# -*- 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__(self, dIn, cOut): # Konstruktor
np.random.seed(42)
self._b=np.zeros((cOut,1))
self._W=np.random.randn(cOut,dIn)/np.sqrt(dIn+1)
def neuron(self, X):
N = X.shape[1]
net = np.zeros(N)
for n in range(0,N):
for j in range(0,X.shape[0]):
# print(self._W)
net[n]+= X[j,n] * self._W[j]
# b verwenden ...
net += self._b
return net > 0 # threshold ....
# 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 ...
# %%
# %%

View File

@ -6,25 +6,29 @@ import numpy as np
import nnwplot
def neuron(X):
N = X.shape[...]
N = X.shape[1]
net = np.zeros(N)
W = [....]
b = # Bias ... ergibt sich aus Aktivierungsschwelle
W = [-0.3,1]
b = 2 # Bias ... ergibt sich aus Aktivierungsschwelle
for n in range(0,N):
for j in range ...:
net[..]+=
b verwenden ...
return net ... # threshold ....
for j in range(0,X.shape[0]):
net[n]+= X[n,j] * W[j]
# b verwenden ...
return net > b # threshold ....
#%%
iris = np.loadtxt("iris.csv",delimiter=',')
X=iris[...]
T=iris[...]
# X=iris[:,:2].T # nur die ersten 2 Merkmale anschauen und in Spaltenvektoren umwandeln
T=iris[-1]
print(X)
print(T)
# print(X)
# print(T)
print(X.shape[0])
neuron(X)
#%%
# AG1b
nnwplot.plotTwoFeatures(X[...], T, neuron)
# %%