start exercise 4

main
Nikola Munder 2026-11-02 17:43:55 +01:00
parent 1b2bdad76a
commit 584b60c82e
5 changed files with 3213 additions and 2926 deletions

View File

@ -2869,6 +2869,10 @@
"display_name": "nnw (3.14.x)", "display_name": "nnw (3.14.x)",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
},
"language_info": {
"name": "python",
"version": "3.14.-1"
} }
}, },
"nbformat": 4, "nbformat": 4,

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,64 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "a35b6bf3",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ea7529b2",
"metadata": {},
"outputs": [],
"source": [
"iris = np.loadtxt(\"../supplement_files/iris.csv\",delimiter=',')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3166860",
"metadata": {},
"outputs": [],
"source": [
"# 4d)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f9a70f7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "nnw (3.14.x)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.14.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -0,0 +1,65 @@
# -*- 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 # gibt boolsche Liste zurück
return errors.sum()/Y.size # summiert True als 1 in errors auf
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)
net = self._W @ X
# b verwenden ...
net = 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 = 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
Y = Y.astype(float)
# update _W
# X.shape = (2,4), T.shape = (1,4), Y.shape = (1,4)
self._W += eta * (T - Y) @ X.T / N
# update _b
self._b -= eta * (T - Y) @ np.ones((1,N)).T / N
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)
input("Press Enter to continue...") # wenn gewünscht ...
# %%
# 3f)
X_AND = np.array([[0,0], [1,0], [0,1], [1,1]]).T
T_AND = np.array([[0],[0],[0],[1]]).T
sln = SLN(2, 1)
sln.DeltaTrain(X_AND, T_AND,0.5,10,0)
# %%

View File

@ -7,8 +7,8 @@ import matplotlib.pyplot as plt
# %% ErrorRate # %% ErrorRate
def ErrorRate(Y,T): def ErrorRate(Y,T):
if Y.ndim==1 or Y.shape[0]==1: if Y.ndim==1 or Y.shape[0]==1:
errors=Y!=T errors=Y!=T # gibt boolsche Liste zurück
return errors.sum()/Y.size return errors.sum()/Y.size # summiert True als 1 in errors auf
else: # für mehrere Ausgaben in one-hot Kodierung: else: # für mehrere Ausgaben in one-hot Kodierung:
errors=Y.argmax(0)!=T.argmax(0) errors=Y.argmax(0)!=T.argmax(0)
return errors.sum()/Y.shape[1] return errors.sum()/Y.shape[1]
@ -24,37 +24,45 @@ class SLN:
N = X.shape[1] N = X.shape[1]
net = np.zeros(N) net = np.zeros(N)
for n in range(0,N): for n in range(N):
for j in range(0,X.shape[0]): for j in range(X.shape[0]):
# print(self._W) # print(self._W)
net[n]+= X[j,n] * self._W[j] net[n]+= X[j,n] * self._W[0, j]
# b verwenden ... # b verwenden ...
net += self._b net = net - self._b
return net > 0 # threshold .... return net > 0 # threshold ....
# def DeltaTrain(self, X, T, eta, maxIter, maxErrorRate): def DeltaTrain(self, X, T, eta, maxIter, maxErrorRate):
# N=X.shape[1] # Anzahl Trainingsdaten N=X.shape[1] # Anzahl Trainingsdaten
# plt.ion() # interactive mode on plt.ion() # interactive mode on
# for it in range(maxIter): for it in range(maxIter):
# Y = ... # nur ein Aufruf Y = self.neuron(X) # nur ein Aufruf
# err = ErrorRate(..., ...) err = ErrorRate(Y, T)
# if (it%20) == 0: if (it%20) == 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(...) 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 Y = Y.astype(float)
# ... # update _W
# # update _b # X.shape = (2,4), T.shape = (1,4), Y.shape = (1,4)
# ... self._W += eta * (T - Y) @ X.T / N
# print('#{} err:{}\n{}\n{}'.format(it,err,self._W,self._b)) # update _b
# nnwplot.plotTwoFeatures(...) self._b -= eta * (T - Y) @ np.ones((1,N)).T / N
# plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht)
#
# input("Press Enter to continue...") # wenn gewünscht ...
# %%
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)
input("Press Enter to continue...") # wenn gewünscht ...
# %%
# 3f)
X_AND = np.array([[0,0], [1,0], [0,1], [1,1]]).T
T_AND = np.array([[0],[0],[0],[1]]).T
sln = SLN(2, 1)
sln.DeltaTrain(X_AND, T_AND,0.5,10,0)
# %% # %%