diff --git a/.gitignore b/.gitignore index 4462e77..74e7841 100644 --- a/.gitignore +++ b/.gitignore @@ -173,4 +173,5 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ +.vscode diff --git a/README.md b/README.md index ff1421b..03b9486 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@ Code-Snippets für die Vorlesung "Grundlagen Neuronale Netze" im SS24. ## Aufgaben -| Nr | Aufgabenstellung | Kommentar | -|----|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------| -| 1 | Verändern Sie die Lernregel so, dass es die Perzeptron Lernregel ist. | | -| 2 | Verändern Sie den Backpropagation Algorithmus so, dass er den Gradientenabstieg vollständig mit iRProp− macht. | | -| 3 | Implementieren Sie eine Restricted Boltzmann Machine als einfachste Form eines Autoencoders. Am Eingang sollen die MNIST Ziffern angelegt werden. Durch Aktivierung der Hidden Schicht und Rückaktivierung sollten die MNIST Ziffern wieder rekonstruiert werden können. Nun reduzieren Sie die Anzahl der Hidden Neuronen auf 100. Können die Ziffern trotzdem so rekonstruiert werden, dass man sie noch erkennen kann? | | -| 4 | | | -| 5 | | | -| 6 | | | +| Nr | Aufgabenstellung | Kommentar | +|----|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------| +| 1 | Verändern Sie die Lernregel so, dass es die Perzeptron Lernregel ist. | | +| 2 | Verändern Sie den Backpropagation Algorithmus so, dass er den Gradientenabstieg vollständig mit iRProp− macht. | | +| 3 | Implementieren Sie eine Restricted Boltzmann Machine als einfachste Form eines Autoencoders. Am Eingang sollen die MNIST Ziffern angelegt werden. Durch Aktivierung der Hidden Schicht und Rückaktivierung sollten die MNIST Ziffern wieder rekonstruiert werden können. Nun reduzieren Sie die Anzahl der Hidden Neuronen auf 100. Können die Ziffern trotzdem so rekonstruiert werden, dass man sie noch erkennen kann? | | +| 4 | Implementieren Sie den zweidimensionalen Pooling Layer eines Convolutional Networks. Laden Sie dazu ein Schwarz-Weiß Bild und erzeugen Sie ein kleineres Bild indem Sie die Pooling Maske mit Stride 2 (heißt in Schrittweite 2) über das Bild schieben. | [Bild Quelle](https://commons.wikimedia.org/wiki/File:Young_tabby_cat_keeping_watch.jpg) | +| 5 | | | +| 6 | | | diff --git a/notizen/test.ipynb b/notizen/test.ipynb index 577f6f2..5b27ff2 100644 --- a/notizen/test.ipynb +++ b/notizen/test.ipynb @@ -4,9 +4,25 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "print(\"hello\")" + "import numpy as np\n", + "\n", + "test = [[1,2],\n", + " [3,4]]\n", + "\n", + "np.max(test)" ] } ], @@ -26,7 +42,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.10.1" } }, "nbformat": 4, diff --git a/uebungen/aufgabe4/cat.png b/uebungen/aufgabe4/cat.png new file mode 100644 index 0000000..2a32fb7 Binary files /dev/null and b/uebungen/aufgabe4/cat.png differ diff --git a/uebungen/aufgabe4/uebung4.py b/uebungen/aufgabe4/uebung4.py new file mode 100644 index 0000000..6758009 --- /dev/null +++ b/uebungen/aufgabe4/uebung4.py @@ -0,0 +1,69 @@ +import numpy as np +from numpy.lib.stride_tricks import as_strided +from PIL import Image +import matplotlib.pyplot as plt + + +def load_data(): + return np.asarray(Image.open("cat.png").convert("L"), dtype=np.uint8) / 255 + +image = load_data() + +def dimension(length, padding, stride, kernel): + return int((length + 2 * padding - kernel + 1)/stride) + +def pad(image, size, strategy): + (ih, iw) = image.shape + (ph, pw) = size + + (rh, rw) = (ih + 2*ph, iw + 2*pw) + + if strategy == 'zero': + base = np.zeros((rh, rw)) + base[ph:ph+ih, pw:pw+iw] = image.copy() + return base + elif strategy == 'mirror': + raise NotImplementedError("Missing") + +def convolute(weights): + return lambda view : np.dot(view.flatten(), weights.flatten()) + +def pool(image, stride=1, kernel_size=(2,2), transform=np.max, padding_size=(0,0), padding_strategy='zero'): + # calculate output size + (height, width) = [dimension(image.shape[i], padding_size[i], stride, kernel_size[i]) for i in range(2)] + + padded = pad(image, padding_size, padding_strategy) + + next = np.zeros((height, width)) + + #'zero'|'mirror' + for i in range(height): + for j in range(width): + # Translate upper left corner into original image space + (y, x) = (i*stride, j*stride) + + # Retrieve image window + view = padded[y:y+kernel_size[0], x:x+kernel_size[1]] + + #print(view) + # Set pooled image value + next[i][j] = transform(view) + + return next + +weights = np.array([ + [-10,-5,0,5,10], + [-10,-5,0,5,10], + [-10,-5,0,5,10], + [-10,-5,0,5,10], + [-10,-5,0,5,10] +]) + +new1 = pool(image, kernel_size=(5,5), padding_size=(2,2), transform=convolute(weights)) +new2 = pool(new1, stride=3, kernel_size=(30,30), transform=np.max) + +fig = plt.figure() +fig.canvas.manager.set_window_title("A Cat") + +plt.imshow(new2, cmap="gray") +plt.show() \ No newline at end of file