continue exercise 4
parent
584b60c82e
commit
a72efa44c2
|
|
@ -1,34 +1,137 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "a35b6bf3",
|
||||
"cell_type": "markdown",
|
||||
"id": "44d16018",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np"
|
||||
"## Vorbereitungen den Aufgabe"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "ea7529b2",
|
||||
"execution_count": 25,
|
||||
"id": "a35b6bf3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"iris = np.loadtxt(\"../supplement_files/iris.csv\",delimiter=',')\n"
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import nnwplot"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"id": "a0dc7253",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def ErrorRate(Y,T):\n",
|
||||
" if Y.ndim==1 or Y.shape[0]==1:\n",
|
||||
" errors=Y!=T # gibt boolsche Liste zurück\n",
|
||||
" return errors.sum()/Y.size # summiert True als 1 in errors auf \n",
|
||||
" else: # für mehrere Ausgaben in one-hot Kodierung:\n",
|
||||
" errors=Y.argmax(0)!=T.argmax(0)\n",
|
||||
" return errors.sum()/Y.shape[1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c3166860",
|
||||
"id": "b75ac9e4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class SLN:\n",
|
||||
" def __init__(self, dIn, cOut): # Konstruktor\n",
|
||||
" np.random.seed(42)\n",
|
||||
" self._b=np.zeros((cOut,1))\n",
|
||||
" self._W=np.random.randn(cOut,dIn)/np.sqrt(dIn+1)\n",
|
||||
"\n",
|
||||
" def neuron(self, X):\n",
|
||||
" N = X.shape[1]\n",
|
||||
" net = np.zeros(N)\n",
|
||||
" # self._W.shape (1,2) X.shape (2,4)\n",
|
||||
" net = self._W @ X \n",
|
||||
" # b verwenden ...\n",
|
||||
" net = net - self._b\n",
|
||||
" return net > 0 # threshold ....\n",
|
||||
" \n",
|
||||
" def DeltaTrain(self, X, T, eta, maxIter, maxErrorRate):\n",
|
||||
" N=X.shape[1] # Anzahl Trainingsdaten\n",
|
||||
" plt.ion() # interactive mode on\n",
|
||||
" for it in range(maxIter):\n",
|
||||
" Y = self.neuron(X) # nur ein Aufruf\n",
|
||||
" err = ErrorRate(Y, T)\n",
|
||||
" if (it%20) == 0:\n",
|
||||
" print('#{} err:{}\\n{}\\n{}'.format(it,err,self._W,self._b))\n",
|
||||
" nnwplot.plotTwoFeatures(X, T, self.neuron)\n",
|
||||
" plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht)\n",
|
||||
" if err <= maxErrorRate:\n",
|
||||
" break\n",
|
||||
" Y = Y.astype(float)\n",
|
||||
" # update _W\n",
|
||||
" # X.shape = (2,4), T.shape = (1,4), Y.shape = (1,4)\n",
|
||||
" self._W += eta * (T - Y) @ X.T / N\n",
|
||||
" # update _b \n",
|
||||
" self._b -= eta * (T - Y) @ np.ones((1,N)).T / N\n",
|
||||
" \n",
|
||||
" print('#{} err:{}\\n{}\\n{}'.format(it,err,self._W,self._b))\n",
|
||||
" nnwplot.plotTwoFeatures(X, T, self.neuron)\n",
|
||||
" plt.pause(0.05) # warte auf GUI event loop (je nach python Version notwendig oder nicht)\n",
|
||||
"\n",
|
||||
" input(\"Press Enter to continue...\") # wenn gewünscht ...\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"id": "ea7529b2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"iris = np.loadtxt(\"../supplement_files/iris.csv\",delimiter=',')\n",
|
||||
"\n",
|
||||
"X = iris[:,[0,1]]\n",
|
||||
"T = iris[:,-1][iris[:,-1]!=2] # alle Zeilen und nur die letzte Spalte, wo die letzte Spalte ungleich 2 (also 0 oder 1) ist\n",
|
||||
"X = X[iris[:,-1]!=2]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"id": "c3166860",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(100,)\n",
|
||||
"(100, 2)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "IndexError",
|
||||
"evalue": "index 2 is out of bounds for axis 1 with size 2",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
||||
"\u001b[31mIndexError\u001b[39m Traceback (most recent call last)",
|
||||
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[37]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# 4d)\u001b[39;00m\n\u001b[32m 2\u001b[39m print(T.shape)\n\u001b[32m 3\u001b[39m print(X.shape)\n\u001b[32m 4\u001b[39m sln = SLN(\u001b[32m2\u001b[39m,\u001b[32m1\u001b[39m)\n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m sln.DeltaTrain(X,T,\u001b[32m0.5\u001b[39m,\u001b[32m1000\u001b[39m,\u001b[32m0.2\u001b[39m)\n",
|
||||
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[35]\u001b[39m\u001b[32m, line 24\u001b[39m, in \u001b[36mSLN.DeltaTrain\u001b[39m\u001b[34m(self, X, T, eta, maxIter, maxErrorRate)\u001b[39m\n\u001b[32m 20\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m DeltaTrain(self, X, T, eta, maxIter, maxErrorRate):\n\u001b[32m 21\u001b[39m N=X.shape[\u001b[32m1\u001b[39m] \u001b[38;5;66;03m# Anzahl Trainingsdaten\u001b[39;00m\n\u001b[32m 22\u001b[39m plt.ion() \u001b[38;5;66;03m# interactive mode on\u001b[39;00m\n\u001b[32m 23\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m it \u001b[38;5;28;01min\u001b[39;00m range(maxIter):\n\u001b[32m---> \u001b[39m\u001b[32m24\u001b[39m Y = self.neuron(X) \u001b[38;5;66;03m# nur ein Aufruf\u001b[39;00m\n\u001b[32m 25\u001b[39m err = ErrorRate(Y, T)\n\u001b[32m 26\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m (it%\u001b[32m20\u001b[39m) == \u001b[32m0\u001b[39m:\n\u001b[32m 27\u001b[39m print(\u001b[33m'#{} err:{}\\n{}\\n{}'\u001b[39m.format(it,err,self._W,self._b))\n",
|
||||
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[35]\u001b[39m\u001b[32m, line 15\u001b[39m, in \u001b[36mSLN.neuron\u001b[39m\u001b[34m(self, X)\u001b[39m\n\u001b[32m 11\u001b[39m \u001b[38;5;66;03m# net = self._W @ X\u001b[39;00m\n\u001b[32m 12\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m n \u001b[38;5;28;01min\u001b[39;00m range(N):\n\u001b[32m 13\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m j \u001b[38;5;28;01min\u001b[39;00m range(X.shape[\u001b[32m0\u001b[39m]):\n\u001b[32m 14\u001b[39m \u001b[38;5;66;03m# print(self._W)\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m15\u001b[39m net[n]+= X[j,n] * self._W[\u001b[32m0\u001b[39m, j]\n\u001b[32m 16\u001b[39m \u001b[38;5;66;03m# b verwenden ...\u001b[39;00m\n\u001b[32m 17\u001b[39m net = net - self._b\n\u001b[32m 18\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m net > \u001b[32m0\u001b[39m \u001b[38;5;66;03m# threshold ....\u001b[39;00m\n",
|
||||
"\u001b[31mIndexError\u001b[39m: index 2 is out of bounds for axis 1 with size 2"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 4d)\n",
|
||||
"\n"
|
||||
"print(T.shape)\n",
|
||||
"print(X.shape)\n",
|
||||
"sln = SLN(2,1)\n",
|
||||
"sln.DeltaTrain(X,T,0.5,1000,0.2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -42,7 +145,7 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "nnw (3.14.x)",
|
||||
"display_name": "nnw (3.14.0)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
|
@ -56,7 +159,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.14.8"
|
||||
"version": "3.14.0rc3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
|||
|
|
@ -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 ...
|
||||
Loading…
Reference in New Issue