{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Uczenie maszynowe – zastosowania\n", "# 10. Sieci neuronowe – propagacja wsteczna" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import numpy as np\n", "import math" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 10.1. Metoda propagacji wstecznej – wprowadzenie" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Architektura sieci neuronowych\n", "\n", "* Budowa warstwowa, najczęściej sieci jednokierunkowe i gęste.\n", "* Liczbę i rozmiar warstw dobiera się do każdego problemu.\n", "* Rozmiary sieci określane poprzez liczbę neuronów lub parametrów." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### _Feedforward_\n", "\n", "Mając daną $n$-warstwową sieć neuronową oraz jej parametry $\\Theta^{(1)}, \\ldots, \\Theta^{(L)} $ oraz $\\beta^{(1)}, \\ldots, \\beta^{(L)} $, obliczamy:\n", "\n", "$$a^{(l)} = g^{(l)}\\left( a^{(l-1)} \\Theta^{(l)} + \\beta^{(l)} \\right). $$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "* Funkcje $g^{(l)}$ to **funkcje aktywacji**.
\n", "Dla $i = 0$ przyjmujemy $a^{(0)} = x$ (wektor wierszowy cech) oraz $g^{(0)}(x) = x$ (identyczność)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Parametry $\\Theta$ to wagi na połączeniach miedzy neuronami dwóch warstw.
\n", "Rozmiar macierzy $\\Theta^{(l)}$, czyli macierzy wag na połączeniach warstw $a^{(l-1)}$ i $a^{(l)}$, to $\\dim(a^{(l-1)}) \\times \\dim(a^{(l)})$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Parametry $\\beta$ zastępują tutaj dodawanie kolumny z jedynkami do macierzy cech.
Macierz $\\beta^{(l)}$ ma rozmiar równy liczbie neuronów w odpowiedniej warstwie, czyli $1 \\times \\dim(a^{(l)})$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "* **Klasyfikacja**: dla ostatniej warstwy $L$ (o rozmiarze równym liczbie klas) przyjmuje się $g^{(L)}(x) = \\mathop{\\mathrm{softmax}}(x)$.\n", "* **Regresja**: pojedynczy neuron wyjściowy; funkcją aktywacji może wtedy być np. funkcja identycznościowa." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Pozostałe funkcje aktywacji najcześciej mają postać sigmoidy, np. sigmoidalna, tangens hiperboliczny.
Ale niekoniecznie, np. ReLU, leaky ReLU, maxout." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Jak uczyć sieci neuronowe?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* W poznanych do tej pory algorytmach (regresja liniowa, regresja logistyczna) do uczenia używaliśmy funkcji kosztu, jej gradientu oraz algorytmu gradientu prostego (GD/SGD)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Dla sieci neuronowych potrzebowalibyśmy również znaleźć gradient funkcji kosztu." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Co sprowadza się do bardziej ogólnego problemu:
jak obliczyć gradient $\\nabla f(x)$ dla danej funkcji $f$ i wektora wejściowego $x$?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Pochodna funkcji\n", "\n", "* **Pochodna** mierzy, jak szybko zmienia się wartość funkcji względem zmiany jej argumentów:\n", "\n", "$$ \\frac{d f(x)}{d x} = \\lim_{h \\to 0} \\frac{ f(x + h) - f(x) }{ h } $$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Pochodna cząstkowa i gradient\n", "\n", "* **Pochodna cząstkowa** mierzy, jak szybko zmienia się wartość funkcji względem zmiany jej *pojedynczego argumentu*." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* **Gradient** to wektor pochodnych cząstkowych:\n", "\n", "$$ \\nabla f = \\left( \\frac{\\partial f}{\\partial x_1}, \\ldots, \\frac{\\partial f}{\\partial x_n} \\right) $$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Gradient – przykłady\n", "\n", "$$ f(x_1, x_2) = x_1 + x_2 \\qquad \\to \\qquad \\frac{\\partial f}{\\partial x_1} = 1, \\quad \\frac{\\partial f}{\\partial x_2} = 1, \\quad \\nabla f = (1, 1) $$ " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "$$ f(x_1, x_2) = x_1 \\cdot x_2 \\qquad \\to \\qquad \\frac{\\partial f}{\\partial x_1} = x_2, \\quad \\frac{\\partial f}{\\partial x_2} = x_1, \\quad \\nabla f = (x_2, x_1) $$ " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "$$ f(x_1, x_2) = \\max(x_1 + x_2) \\hskip{12em} \\\\\n", "\\to \\qquad \\frac{\\partial f}{\\partial x_1} = \\mathbb{1}_{x \\geq y}, \\quad \\frac{\\partial f}{\\partial x_2} = \\mathbb{1}_{y \\geq x}, \\quad \\nabla f = (\\mathbb{1}_{x \\geq y}, \\mathbb{1}_{y \\geq x}) $$ " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Własności pochodnych cząstkowych\n", "\n", "Jezeli $f(x, y, z) = (x + y) \\, z$ oraz $x + y = q$, to:\n", "$$f = q z,\n", "\\quad \\frac{\\partial f}{\\partial q} = z,\n", "\\quad \\frac{\\partial f}{\\partial z} = q,\n", "\\quad \\frac{\\partial q}{\\partial x} = 1,\n", "\\quad \\frac{\\partial q}{\\partial y} = 1 $$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Reguła łańcuchowa\n", "\n", "$$ \\frac{\\partial f}{\\partial x} = \\frac{\\partial f}{\\partial q} \\, \\frac{\\partial q}{\\partial x},\n", "\\quad \\frac{\\partial f}{\\partial y} = \\frac{\\partial f}{\\partial q} \\, \\frac{\\partial q}{\\partial y} $$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Propagacja wsteczna – prosty przykład" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "# Dla ustalonego wejścia\n", "x = -2; y = 5; z = -4" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3, -12)\n" ] } ], "source": [ "# Krok w przód\n", "q = x + y\n", "f = q * z\n", "print(q, f)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-4, -4, 3]\n" ] } ], "source": [ "# UWAGA: teraz za pomocą zmiennych `dfx`, `dfy`, `dfz` i `dfq`\n", "# oznaczę pochodne cząstkowe ∂f/∂x, ∂f/∂y, ∂f/∂z i ∂f/∂q odpowiednio\n", "\n", "# Propagacja wsteczna dla f = q * z\n", "dfz = q\n", "dfq = z\n", "# Propagacja wsteczna dla q = x + y\n", "dfx = 1 * dfq # z reguły łańcuchowej\n", "dfy = 1 * dfq # z reguły łańcuchowej\n", "print([dfx, dfy, dfz])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "* Właśnie tak wygląda obliczanie pochodnych metodą propagacji wstecznej!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Spróbujmy czegoś bardziej skomplikowanego:
metodą propagacji wstecznej obliczmy pochodną funkcji sigmoidalnej." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Propagacja wsteczna – funkcja sigmoidalna" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Funkcja sigmoidalna:\n", "\n", "$$f(\\theta,x) = \\frac{1}{1+e^{-(\\theta_0 x_0 + \\theta_1 x_1 + \\theta_2)}}$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "$$\n", "\\begin{array}{lcl}\n", "f(x) = \\frac{1}{x} \\quad & \\rightarrow & \\quad \\frac{df}{dx} = -\\frac{1}{x^2} \\\\\n", "f_c(x) = c + x \\quad & \\rightarrow & \\quad \\frac{df}{dx} = 1 \\\\\n", "f(x) = e^x \\quad & \\rightarrow & \\quad \\frac{df}{dx} = e^x \\\\\n", "f_a(x) = ax \\quad & \\rightarrow & \\quad \\frac{df}{dx} = a \\\\\n", "\\end{array}\n", "$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.3932238664829637, -0.5898357997244456]\n", "[-0.19661193324148185, -0.3932238664829637, 0.19661193324148185]\n" ] } ], "source": [ "# Losowe wagi i dane\n", "w = [2,-3,-3]\n", "x = [-1, -2]\n", "\n", "# Krok w przód\n", "dot = w[0]*x[0] + w[1]*x[1] + w[2]\n", "f = 1.0 / (1 + math.exp(-dot)) # funkcja sigmoidalna\n", "\n", "# Krok w tył\n", "ddot = (1 - f) * f # pochodna funkcji sigmoidalnej\n", "dx = [w[0] * ddot, w[1] * ddot]\n", "dw = [x[0] * ddot, x[1] * ddot, 1.0 * ddot]\n", "\n", "print(dx)\n", "print(dw)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Obliczanie gradientów – podsumowanie\n", "\n", "* Gradient $f$ dla $x$ mówi jak zmieni się całe wyrażenie przy zmianie wartości $x$.\n", "* Gradienty łączymy korzystając z **reguły łańcuchowej**.\n", "* W kroku wstecz gradienty informują, które części grafu powinny być zwiększone lub zmniejszone (i z jaką siłą), aby zwiększyć wartość na wyjściu.\n", "* W kontekście implementacji chcemy dzielić funkcję $f$ na części, dla których można łatwo obliczyć gradienty." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 10.2. Uczenie wielowarstwowych sieci neuronowych metodą propagacji wstecznej" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Mając algorytm SGD oraz gradienty wszystkich wag, moglibyśmy trenować każdą sieć." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "* Niech:\n", "$$\\Theta = (\\Theta^{(1)},\\Theta^{(2)},\\Theta^{(3)},\\beta^{(1)},\\beta^{(2)},\\beta^{(3)})$$\n", "\n", "* Funkcja sieci neuronowej z grafiki:\n", "\n", "$$\\small h_\\Theta(x) = \\tanh(\\tanh(\\tanh(x\\Theta^{(1)}+\\beta^{(1)})\\Theta^{(2)} + \\beta^{(2)})\\Theta^{(3)} + \\beta^{(3)})$$\n", "* Funkcja kosztu dla regresji:\n", "$$J(\\Theta) = \\dfrac{1}{2m} \\sum_{i=1}^{m} (h_\\Theta(x^{(i)})- y^{(i)})^2 $$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "* Jak obliczymy gradienty?\n", "\n", "$$\\nabla_{\\Theta^{(l)}} J(\\Theta) = ? \\quad \\nabla_{\\beta^{(l)}} J(\\Theta) = ?$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### W kierunku propagacji wstecznej\n", "\n", "* Pewna (niewielka) zmiana wagi $\\Delta z^l_j$ dla $j$-ego neuronu w warstwie $l$ pociąga za sobą (niewielką) zmianę kosztu: \n", "\n", "$$\\frac{\\partial J(\\Theta)}{\\partial z^{l}_j} \\Delta z^{l}_j$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Jeżeli $\\frac{\\partial J(\\Theta)}{\\partial z^{l}_j}$ jest duża, $\\Delta z^l_j$ ze znakiem przeciwnym zredukuje koszt.\n", "* Jeżeli $\\frac{\\partial J(\\Theta)}{\\partial z^l_j}$ jest bliska zeru, koszt nie będzie mocno poprawiony." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "* Definiujemy błąd $\\delta^l_j$ neuronu $j$ w warstwie $l$: \n", "\n", "$$\\delta^l_j := \\dfrac{\\partial J(\\Theta)}{\\partial z^l_j}$$ \n", "$$\\delta^l := \\nabla_{z^l} J(\\Theta) \\quad \\textrm{ (zapis wektorowy)} $$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Podstawowe równania propagacji wstecznej\n", "\n", "$$\n", "\\begin{array}{rcll}\n", "\\delta^L & = & \\nabla_{a^L}J(\\Theta) \\odot { \\left( g^{L} \\right) }^{\\prime} \\left( z^L \\right) & (BP1) \\\\[2mm]\n", "\\delta^{l} & = & \\left( \\left( \\Theta^{l+1} \\right) \\! ^\\top \\, \\delta^{l+1} \\right) \\odot {{ \\left( g^{l} \\right) }^{\\prime}} \\left( z^{l} \\right) & (BP2)\\\\[2mm]\n", "\\nabla_{\\beta^l} J(\\Theta) & = & \\delta^l & (BP3)\\\\[2mm]\n", "\\nabla_{\\Theta^l} J(\\Theta) & = & a^{l-1} \\odot \\delta^l & (BP4)\\\\\n", "\\end{array}\n", "$$\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### (BP1)\n", "$$ \\delta^L_j \\; = \\; \\frac{ \\partial J }{ \\partial a^L_j } \\, g' \\!\\! \\left( z^L_j \\right) $$\n", "$$ \\delta^L \\; = \\; \\nabla_{a^L}J(\\Theta) \\odot { \\left( g^{L} \\right) }^{\\prime} \\left( z^L \\right) $$\n", "Błąd w ostatniej warstwie jest iloczynem szybkości zmiany kosztu względem $j$-tego wyjścia i szybkości zmiany funkcji aktywacji w punkcie $z^L_j$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### (BP2)\n", "$$ \\delta^{l} \\; = \\; \\left( \\left( \\Theta^{l+1} \\right) \\! ^\\top \\, \\delta^{l+1} \\right) \\odot {{ \\left( g^{l} \\right) }^{\\prime}} \\left( z^{l} \\right) $$\n", "Aby obliczyć błąd w $l$-tej warstwie, należy przemnożyć błąd z następnej ($(l+1)$-szej) warstwy przez transponowany wektor wag, a uzyskaną macierz pomnożyć po współrzędnych przez szybkość zmiany funkcji aktywacji w punkcie $z^l$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### (BP3)\n", "$$ \\nabla_{\\beta^l} J(\\Theta) \\; = \\; \\delta^l $$\n", "Błąd w $l$-tej warstwie jest równy wartości gradientu funkcji kosztu." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### (BP4)\n", "$$ \\nabla_{\\Theta^l} J(\\Theta) \\; = \\; a^{l-1} \\odot \\delta^l $$\n", "Gradient funkcji kosztu względem wag $l$-tej warstwy można obliczyć jako iloczyn po współrzędnych $a^{l-1}$ przez $\\delta^l$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Algorytm propagacji wstecznej" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Dla jednego przykładu $(x,y)$:\n", "\n", "1. **Wejście**: Ustaw aktywacje w warstwie cech $a^{(0)}=x$ \n", "2. **Feedforward:** dla $l=1,\\dots,L$ oblicz \n", "$$z^{(l)} = a^{(l-1)} \\Theta^{(l)} + \\beta^{(l)} \\textrm{ oraz } a^{(l)}=g^{(l)} \\!\\! \\left( z^{(l)} \\right) $$\n", "3. **Błąd wyjścia $\\delta^{(L)}$:** oblicz wektor $$\\delta^{(L)}= \\nabla_{a^{(L)}}J(\\Theta) \\odot {g^{\\prime}}^{(L)} \\!\\! \\left( z^{(L)} \\right) $$\n", "4. **Propagacja wsteczna błędu:** dla $l = L-1,L-2,\\dots,1$ oblicz $$\\delta^{(l)} = \\delta^{(l+1)}(\\Theta^{(l+1)})^T \\odot {g^{\\prime}}^{(l)} \\!\\! \\left( z^{(l)} \\right) $$\n", "5. **Gradienty:** \n", " * $\\dfrac{\\partial}{\\partial \\Theta_{ij}^{(l)}} J(\\Theta) = a_i^{(l-1)}\\delta_j^{(l)} \\textrm{ oraz } \\dfrac{\\partial}{\\partial \\beta_{j}^{(l)}} J(\\Theta) = \\delta_j^{(l)}$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "W naszym przykładzie:\n", "\n", "$$\\small J(\\Theta) = \\frac{1}{2} \\left( a^{(L)} - y \\right) ^2 $$\n", "$$\\small \\dfrac{\\partial}{\\partial a^{(L)}} J(\\Theta) = a^{(L)} - y$$\n", "\n", "$$\\small \\tanh^{\\prime}(x) = 1 - \\tanh^2(x)$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Algorytm SGD z propagacją wsteczną\n", "\n", "Pojedyncza iteracja:\n", "* Dla parametrów $\\Theta = (\\Theta^{(1)},\\ldots,\\Theta^{(L)})$ utwórz pomocnicze macierze zerowe $\\Delta = (\\Delta^{(1)},\\ldots,\\Delta^{(L)})$ o takich samych wymiarach (dla uproszczenia opuszczono wagi $\\beta$).\n", "* Dla $m$ przykładów we wsadzie (_batch_), $i = 1,\\ldots,m$:\n", " * Wykonaj algortym propagacji wstecznej dla przykładu $(x^{(i)}, y^{(i)})$ i przechowaj gradienty $\\nabla_{\\Theta}J^{(i)}(\\Theta)$ dla tego przykładu;\n", " * $\\Delta := \\Delta + \\dfrac{1}{m}\\nabla_{\\Theta}J^{(i)}(\\Theta)$\n", "* Wykonaj aktualizację wag: $\\Theta := \\Theta - \\alpha \\Delta$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Propagacja wsteczna – podsumowanie\n", "\n", "* Algorytm pierwszy raz wprowadzony w latach 70. XX w.\n", "* W 1986 David Rumelhart, Geoffrey Hinton i Ronald Williams pokazali, że jest znacznie szybszy od wcześniejszych metod.\n", "* Obecnie najpopularniejszy algorytm uczenia sieci neuronowych." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## 10.3. Przykłady implementacji wielowarstwowych sieci neuronowych" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "### Uwaga!\n", "\n", "Poniższe przykłady wykorzystują interfejs [Keras](https://keras.io), który jest częścią biblioteki [TensorFlow](https://www.tensorflow.org).\n", "\n", "Aby uruchomić TensorFlow w środowisku Jupyter, należy wykonać następujące czynności:\n", "\n", "#### Przed pierwszym uruchomieniem (wystarczy wykonać tylko raz)\n", "\n", "Instalacja biblioteki TensorFlow w środowisku Anaconda:\n", "\n", "1. Uruchom *Anaconda Navigator*\n", "1. Wybierz kafelek *CMD.exe Prompt*\n", "1. Kliknij przycisk *Launch*\n", "1. Pojawi się konsola. Wpisz następujące polecenia, każde zatwierdzając wciśnięciem klawisza Enter:\n", "```\n", "conda create -n tf tensorflow\n", "conda activate tf\n", "conda install pandas matplotlib\n", "jupyter notebook\n", "```\n", "\n", "#### Przed każdym uruchomieniem\n", "\n", "Jeżeli chcemy korzystać z biblioteki TensorFlow, to środowisko Jupyter Notebook należy uruchomić w następujący sposób:\n", "\n", "1. Uruchom *Anaconda Navigator*\n", "1. Wybierz kafelek *CMD.exe Prompt*\n", "1. Kliknij przycisk *Launch*\n", "1. Pojawi się konsola. Wpisz następujące polecenia, każde zatwierdzając wciśnięciem klawisza Enter:\n", "```\n", "conda activate tf\n", "jupyter notebook\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Przykład: MNIST\n", "\n", "_Modified National Institute of Standards and Technology database_" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "* Zbiór cyfr zapisanych pismem odręcznym\n", "* 60 000 przykładów uczących, 10 000 przykładów testowych\n", "* Rozdzielczość każdego przykładu: 28 × 28 = 784 piksele" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "# źródło: https://github.com/keras-team/keras/examples/minst_mlp.py\n", "\n", "from tensorflow import keras\n", "from tensorflow.keras.datasets import mnist\n", "from tensorflow.keras.layers import Dense, Dropout\n", "\n", "# załaduj dane i podziel je na zbiory uczący i testowy\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "\n", "def draw_examples(examples, captions=None):\n", " plt.figure(figsize=(16, 4))\n", " m = len(examples)\n", " for i, example in enumerate(examples):\n", " plt.subplot(100 + m * 10 + i + 1)\n", " plt.imshow(example, cmap=plt.get_cmap('gray'))\n", " plt.show()\n", " if captions is not None:\n", " print(6 * ' ' + (10 * ' ').join(str(captions[i]) for i in range(m)))" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAA54AAACOCAYAAABZsdfhAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAcc0lEQVR4nO3deZBU5bnH8ecVAVFERJAQEYYIQYhsAgpeC0gAV2SRgBL2GKHEBVJCgUoMxiCIStUAEkUujCAlWGHVSJCwSFRCgQTvBVkGjMDgBAYF2Qxc9Nw/6O15menpnjnn9Onu76eKmvOb093nnebh9Lycfvo1juMIAAAAAABeuSTVAwAAAAAAZDYmngAAAAAATzHxBAAAAAB4ioknAAAAAMBTTDwBAAAAAJ5i4gkAAAAA8FS5Jp7GmLuMMbuNMXuNMePcGhTSD7WAMGoBItQBoqgFiFAHiKIWspcp6zqexpgKIrJHRLqKSIGIbBaRfo7jfO7e8JAOqAWEUQsQoQ4QRS1AhDpAFLWQ3S4tx31vEZG9juN8ISJijFkoIj1EpMTCMcaUbZaLVDrqOE6tUm5DLWQBx3FMAjdLqhaog7TEOQFh1AJEJKHXB+ogO3BOQFixtVCet9peJyIHY3JB6HvILPsTuA21gDBqIfNxTkAYtYBEUQfZgXMCwoqthfJc8Szuf7cu+h8JY8wwERlWjuMg+KgFhJVaC9RBVuCcgDBqASLUAaKohSxWnolngYhcH5PrishX9o0cx5klIrNEuFSewagFhJVaC9RBVuCcgDBqASLUAaKohSxWnrfabhaRRsaYBsaYSiLyoIiscGdYSDPUAsKoBYhQB4iiFiBCHSCKWshiZb7i6TjOeWPMYyKySkQqiMgcx3F2uDYypA1qAWHUAkSoA0RRCxChDhBFLWS3Mi+nUqaDcak8HX3qOE4btx+UWkg/CX6qbVKog7TEOQFh1AJ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AAAAA8BQTTwAAAACAp5h4AgAAAAA8xcQTAAAAAOApJp4AAAAAAE8x8QQAAAAAeIqJJwAAAADAU/8PSXdIbxrRR2wAAAAASUVORK5CYII=\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ " 5 0 4 1 9 2 1\n" ] } ], "source": [ "draw_examples(x_train[:7], captions=y_train)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "60000 przykładów uczących\n", "10000 przykładów testowych\n" ] } ], "source": [ "num_classes = 10\n", "\n", "x_train = x_train.reshape(60000, 784) # 784 = 28 * 28\n", "x_test = x_test.reshape(10000, 784)\n", "x_train = x_train.astype('float32')\n", "x_test = x_test.astype('float32')\n", "x_train /= 255\n", "x_test /= 255\n", "print('{} przykładów uczących'.format(x_train.shape[0]))\n", "print('{} przykładów testowych'.format(x_test.shape[0]))\n", "\n", "# przekonwertuj wektory klas na binarne macierze klas\n", "y_train = keras.utils.to_categorical(y_train, num_classes)\n", "y_test = keras.utils.to_categorical(y_test, num_classes)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "scrolled": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_21\"\n", "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "dense_59 (Dense) (None, 512) 401920 \n", "_________________________________________________________________\n", "dropout_2 (Dropout) (None, 512) 0 \n", "_________________________________________________________________\n", "dense_60 (Dense) (None, 512) 262656 \n", "_________________________________________________________________\n", "dropout_3 (Dropout) (None, 512) 0 \n", "_________________________________________________________________\n", "dense_61 (Dense) (None, 10) 5130 \n", "=================================================================\n", "Total params: 669,706\n", "Trainable params: 669,706\n", "Non-trainable params: 0\n", "_________________________________________________________________\n" ] } ], "source": [ "model = keras.Sequential()\n", "model.add(Dense(512, activation='relu', input_shape=(784,)))\n", "model.add(Dropout(0.2))\n", "model.add(Dense(512, activation='relu'))\n", "model.add(Dropout(0.2))\n", "model.add(Dense(num_classes, activation='softmax'))\n", "model.summary()" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(60000, 784) (60000, 10)\n" ] } ], "source": [ "print(x_train.shape, y_train.shape)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/5\n", "469/469 [==============================] - 11s 23ms/step - loss: 0.2463 - accuracy: 0.9238 - val_loss: 0.1009 - val_accuracy: 0.9690\n", "Epoch 2/5\n", "469/469 [==============================] - 10s 22ms/step - loss: 0.1042 - accuracy: 0.9681 - val_loss: 0.0910 - val_accuracy: 0.9739\n", "Epoch 3/5\n", "469/469 [==============================] - 11s 23ms/step - loss: 0.0774 - accuracy: 0.9762 - val_loss: 0.0843 - val_accuracy: 0.9755\n", "Epoch 4/5\n", "469/469 [==============================] - 11s 24ms/step - loss: 0.0606 - accuracy: 0.9815 - val_loss: 0.0691 - val_accuracy: 0.9818\n", "Epoch 5/5\n", "469/469 [==============================] - 10s 22ms/step - loss: 0.0504 - accuracy: 0.9848 - val_loss: 0.0886 - val_accuracy: 0.9772\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.RMSprop(), metrics=['accuracy'])\n", "\n", "model.fit(x_train, y_train, batch_size=128, epochs=5, verbose=1,\n", " validation_data=(x_test, y_test))" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test loss: 0.08859136700630188\n", "Test accuracy: 0.9771999716758728\n" ] } ], "source": [ "score = model.evaluate(x_test, y_test, verbose=0)\n", "\n", "print('Test loss: {}'.format(score[0]))\n", "print('Test accuracy: {}'.format(score[1]))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Warstwa _dropout_ to metoda regularyzacji, służy zapobieganiu nadmiernemu dopasowaniu sieci. Polega na tym, że część węzłów sieci jest usuwana w sposób losowy." ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_22\"\n", "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "dense_62 (Dense) (None, 512) 401920 \n", "_________________________________________________________________\n", "dense_63 (Dense) (None, 512) 262656 \n", "_________________________________________________________________\n", "dense_64 (Dense) (None, 10) 5130 \n", "=================================================================\n", "Total params: 669,706\n", "Trainable params: 669,706\n", "Non-trainable params: 0\n", "_________________________________________________________________\n", "Epoch 1/5\n", "469/469 [==============================] - 10s 20ms/step - loss: 0.2203 - accuracy: 0.9317 - val_loss: 0.0936 - val_accuracy: 0.9697\n", "Epoch 2/5\n", "469/469 [==============================] - 10s 21ms/step - loss: 0.0816 - accuracy: 0.9746 - val_loss: 0.0747 - val_accuracy: 0.9779\n", "Epoch 3/5\n", "469/469 [==============================] - 10s 20ms/step - loss: 0.0544 - accuracy: 0.9827 - val_loss: 0.0674 - val_accuracy: 0.9798\n", "Epoch 4/5\n", "469/469 [==============================] - 10s 22ms/step - loss: 0.0384 - accuracy: 0.9879 - val_loss: 0.0746 - val_accuracy: 0.9806\n", "Epoch 5/5\n", "469/469 [==============================] - 10s 22ms/step - loss: 0.0298 - accuracy: 0.9901 - val_loss: 0.0736 - val_accuracy: 0.9801\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Bez warstw Dropout\n", "\n", "num_classes = 10\n", "\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "\n", "x_train = x_train.reshape(60000, 784) # 784 = 28 * 28\n", "x_test = x_test.reshape(10000, 784)\n", "x_train = x_train.astype('float32')\n", "x_test = x_test.astype('float32')\n", "x_train /= 255\n", "x_test /= 255\n", "\n", "y_train = keras.utils.to_categorical(y_train, num_classes)\n", "y_test = keras.utils.to_categorical(y_test, num_classes)\n", "\n", "model_no_dropout = keras.Sequential()\n", "model_no_dropout.add(Dense(512, activation='relu', input_shape=(784,)))\n", "model_no_dropout.add(Dense(512, activation='relu'))\n", "model_no_dropout.add(Dense(num_classes, activation='softmax'))\n", "model_no_dropout.summary()\n", "\n", "model_no_dropout.compile(loss='categorical_crossentropy',\n", " optimizer=keras.optimizers.RMSprop(),\n", " metrics=['accuracy'])\n", "\n", "model_no_dropout.fit(x_train, y_train,\n", " batch_size=128,\n", " epochs=5,\n", " verbose=1,\n", " validation_data=(x_test, y_test))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test loss (no dropout): 0.07358124107122421\n", "Test accuracy (no dropout): 0.9800999760627747\n" ] } ], "source": [ "# Bez warstw Dropout\n", "\n", "score = model_no_dropout.evaluate(x_test, y_test, verbose=0)\n", "\n", "print('Test loss (no dropout): {}'.format(score[0]))\n", "print('Test accuracy (no dropout): {}'.format(score[1]))" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_23\"\n", "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "dense_65 (Dense) (None, 2500) 1962500 \n", "_________________________________________________________________\n", "dense_66 (Dense) (None, 2000) 5002000 \n", "_________________________________________________________________\n", "dense_67 (Dense) (None, 1500) 3001500 \n", "_________________________________________________________________\n", "dense_68 (Dense) (None, 1000) 1501000 \n", "_________________________________________________________________\n", "dense_69 (Dense) (None, 500) 500500 \n", "_________________________________________________________________\n", "dense_70 (Dense) (None, 10) 5010 \n", "=================================================================\n", "Total params: 11,972,510\n", "Trainable params: 11,972,510\n", "Non-trainable params: 0\n", "_________________________________________________________________\n", "Epoch 1/10\n", "469/469 [==============================] - 129s 275ms/step - loss: 0.9587 - accuracy: 0.7005 - val_loss: 0.5066 - val_accuracy: 0.8566\n", "Epoch 2/10\n", "469/469 [==============================] - 130s 276ms/step - loss: 0.2666 - accuracy: 0.9234 - val_loss: 0.3376 - val_accuracy: 0.9024\n", "Epoch 3/10\n", "469/469 [==============================] - 130s 277ms/step - loss: 0.1811 - accuracy: 0.9477 - val_loss: 0.1678 - val_accuracy: 0.9520\n", "Epoch 4/10\n", "469/469 [==============================] - 134s 287ms/step - loss: 0.1402 - accuracy: 0.9588 - val_loss: 0.1553 - val_accuracy: 0.9576\n", "Epoch 5/10\n", "469/469 [==============================] - 130s 278ms/step - loss: 0.1153 - accuracy: 0.9662 - val_loss: 0.1399 - val_accuracy: 0.9599\n", "Epoch 6/10\n", "469/469 [==============================] - 130s 277ms/step - loss: 0.0956 - accuracy: 0.9711 - val_loss: 0.1389 - val_accuracy: 0.9612\n", "Epoch 7/10\n", "469/469 [==============================] - 131s 280ms/step - loss: 0.0803 - accuracy: 0.9761 - val_loss: 0.1008 - val_accuracy: 0.9724\n", "Epoch 8/10\n", "469/469 [==============================] - 134s 286ms/step - loss: 0.0685 - accuracy: 0.9797 - val_loss: 0.1137 - val_accuracy: 0.9679\n", "Epoch 9/10\n", "469/469 [==============================] - 130s 278ms/step - loss: 0.0602 - accuracy: 0.9819 - val_loss: 0.1064 - val_accuracy: 0.9700\n", "Epoch 10/10\n", "469/469 [==============================] - 129s 274ms/step - loss: 0.0520 - accuracy: 0.9843 - val_loss: 0.1095 - val_accuracy: 0.9698\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Więcej warstw, inna funkcja aktywacji\n", "\n", "num_classes = 10\n", "\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "\n", "x_train = x_train.reshape(60000, 784) # 784 = 28 * 28\n", "x_test = x_test.reshape(10000, 784)\n", "x_train = x_train.astype('float32')\n", "x_test = x_test.astype('float32')\n", "x_train /= 255\n", "x_test /= 255\n", "\n", "y_train = keras.utils.to_categorical(y_train, num_classes)\n", "y_test = keras.utils.to_categorical(y_test, num_classes)\n", "\n", "model3 = Sequential()\n", "model3.add(Dense(2500, activation='tanh', input_shape=(784,)))\n", "model3.add(Dense(2000, activation='tanh'))\n", "model3.add(Dense(1500, activation='tanh'))\n", "model3.add(Dense(1000, activation='tanh'))\n", "model3.add(Dense(500, activation='tanh'))\n", "model3.add(Dense(num_classes, activation='softmax'))\n", "model3.summary()\n", "\n", "model3.compile(loss='categorical_crossentropy',\n", " optimizer=keras.optimizers.RMSprop(),\n", " metrics=['accuracy'])\n", "\n", "model3.fit(x_train, y_train,\n", " batch_size=128,\n", " epochs=10,\n", " verbose=1,\n", " validation_data=(x_test, y_test))" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test loss: 0.10945799201726913\n", "Test accuracy: 0.9697999954223633\n" ] } ], "source": [ "# Więcej warstw, inna funkcja aktywacji\n", "\n", "score = model3.evaluate(x_test, y_test, verbose=0)\n", "\n", "print('Test loss: {}'.format(score[0]))\n", "print('Test accuracy: {}'.format(score[1]))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Przykład: 4-pikselowy aparat fotograficzny\n", "\n", "https://www.youtube.com/watch?v=ILsA4nyG7I0" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "def generate_example(description):\n", " variant = random.choice([1, -1])\n", " if description == 's': # solid\n", " return (np.array([[ 1.0, 1.0], [ 1.0, 1.0]]) if variant == 1 else\n", " np.array([[-1.0, -1.0], [-1.0, -1.0]]))\n", " elif description == 'v': # vertical\n", " return (np.array([[ 1.0, -1.0], [ 1.0, -1.0]]) if variant == 1 else\n", " np.array([[-1.0, 1.0], [-1.0, 1.0]]))\n", " elif description == 'd': # diagonal\n", " return (np.array([[ 1.0, -1.0], [-1.0, 1.0]]) if variant == 1 else\n", " np.array([[-1.0, 1.0], [ 1.0, -1.0]]))\n", " elif description == 'h': # horizontal\n", " return (np.array([[ 1.0, 1.0], [-1.0, -1.0]]) if variant == 1 else\n", " np.array([[-1.0, -1.0], [ 1.0, 1.0]]))\n", " else:\n", " return np.array([[random.uniform(-1, 1), random.uniform(-1, 1)],\n", " [random.uniform(-1, 1), random.uniform(-1, 1)]])" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "import random\n", "\n", "num_classes = 4\n", "\n", "trainset_size = 4000\n", "testset_size = 1000\n", "\n", "y4_train = np.array([random.choice(['s', 'v', 'd', 'h']) for i in range(trainset_size)])\n", "x4_train = np.array([generate_example(desc) for desc in y4_train])\n", "\n", "y4_test = np.array([random.choice(['s', 'v', 'd', 'h']) for i in range(testset_size)])\n", "x4_test = np.array([generate_example(desc) for desc in y4_test])" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAA6oAAACQCAYAAAABdZZIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAPGUlEQVR4nO3dX4ild33H8c+3uwYhVKTN+ie7WZOLZSUVWswkVXqTXliSYJNeaIm9UKSwKOSyFwsF7WUveiUVQy5C4o22eGGXNq1NhRALymZXVJK2SRdRs0nARkvaVTFm+fVizrMzpLOzc/Y858xvnvN6wZA5c56c5zkn7/09893zzKRa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ntSxTfr2m/NyWYcqv15Sf2zJM9fXq5XkZVAEAAOjKKv/3NAAAAHBNaz+oVtU9VfV8VV2oqtP7fTxjqapHq+rHVfXsfh/LQaEFBlNsQQfzm2IHiRauhxYYTLEFHcxvih0k/bWw1oNqVR1K8vkk9ya5PcnHqur2/T2q0TyW5J79PoiDQgsMJtzCY9HBnk24g0QLc9ECgwm38Fh0sGcT7iDprIW1HlST3JXkQmvt+62115N8OckD+3xMo2itPZ3kp/t9HAeIFhhMsgUdzG2SHSRauA5aYDDJFnQwt0l2kPTXwroPqkeTvLjt9sXZ11g/WmCgBRIdsEULDLRAooOVWfdBtXb4ml+DvJ60wEALJDpgixYYaIFEByuz7oPqxSS3bLt9LMnL+3Qs7C8tMNACiQ7YogUGWiDRwcqs+6D6TJITVXVbVd2Q5MEkZ/b5mNgfWmCgBRIdsEULDLRAooOVWetBtbX2RpKHknwtyb8n+dvW2nP7e1TjqKovJflmkpNVdbGq/nS/j6lnWmAw1RZ0MJ+pdpBoYV5aYDDVFnQwn6l2kPTXQrXmkmoAAAD6sdbvqAIAANAfgyoAAABdMagCAADQFYMqAAAAXTGoAgAA0BWDKgAAAF0xqAIAANAVgyoAAABd+T89XtOTyy31ugAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ " s d h s d v v\n" ] } ], "source": [ "draw_examples(x4_train[:7], captions=y4_train)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "x4_train = x4_train.reshape(trainset_size, 4)\n", "x4_test = x4_test.reshape(testset_size, 4)\n", "x4_train = x4_train.astype('float32')\n", "x4_test = x4_test.astype('float32')\n", "\n", "y4_train = np.array([{'s': 0, 'v': 1, 'd': 2, 'h': 3}[desc] for desc in y4_train])\n", "y4_test = np.array([{'s': 0, 'v': 1, 'd': 2, 'h': 3}[desc] for desc in y4_test])\n", "\n", "y4_train = keras.utils.to_categorical(y4_train, num_classes)\n", "y4_test = keras.utils.to_categorical(y4_test, num_classes)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_24\"\n", "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "dense_71 (Dense) (None, 4) 20 \n", "_________________________________________________________________\n", "dense_72 (Dense) (None, 4) 20 \n", "_________________________________________________________________\n", "dense_73 (Dense) (None, 8) 40 \n", "_________________________________________________________________\n", "dense_74 (Dense) (None, 4) 36 \n", "=================================================================\n", "Total params: 116\n", "Trainable params: 116\n", "Non-trainable params: 0\n", "_________________________________________________________________\n" ] } ], "source": [ "model4 = keras.Sequential()\n", "model4.add(Dense(4, activation='tanh', input_shape=(4,)))\n", "model4.add(Dense(4, activation='tanh'))\n", "model4.add(Dense(8, activation='relu'))\n", "model4.add(Dense(num_classes, activation='softmax'))\n", "model4.summary()" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "model4.layers[0].set_weights(\n", " [np.array([[ 1.0, 0.0, 1.0, 0.0],\n", " [ 0.0, 1.0, 0.0, 1.0],\n", " [ 1.0, 0.0, -1.0, 0.0],\n", " [ 0.0, 1.0, 0.0, -1.0]],\n", " dtype=np.float32), np.array([0., 0., 0., 0.], dtype=np.float32)])\n", "model4.layers[1].set_weights(\n", " [np.array([[ 1.0, -1.0, 0.0, 0.0],\n", " [ 1.0, 1.0, 0.0, 0.0],\n", " [ 0.0, 0.0, 1.0, -1.0],\n", " [ 0.0, 0.0, -1.0, -1.0]],\n", " dtype=np.float32), np.array([0., 0., 0., 0.], dtype=np.float32)])\n", "model4.layers[2].set_weights(\n", " [np.array([[ 1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n", " [ 0.0, 0.0, 1.0, -1.0, 0.0, 0.0, 0.0, 0.0],\n", " [ 0.0, 0.0, 0.0, 0.0, 1.0, -1.0, 0.0, 0.0],\n", " [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, -1.0]],\n", " dtype=np.float32), np.array([0., 0., 0., 0., 0., 0., 0., 0.], dtype=np.float32)])" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "model4.layers[3].set_weights(\n", " [np.array([[ 1.0, 0.0, 0.0, 0.0],\n", " [ 1.0, 0.0, 0.0, 0.0],\n", " [ 0.0, 1.0, 0.0, 0.0],\n", " [ 0.0, 1.0, 0.0, 0.0],\n", " [ 0.0, 0.0, 1.0, 0.0],\n", " [ 0.0, 0.0, 1.0, 0.0],\n", " [ 0.0, 0.0, 0.0, 1.0],\n", " [ 0.0, 0.0, 0.0, 1.0]],\n", " dtype=np.float32), np.array([0., 0., 0., 0.], dtype=np.float32)])\n", "\n", "model4.compile(loss='categorical_crossentropy',\n", " optimizer=keras.optimizers.Adagrad(),\n", " metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[array([[ 1., 0., 1., 0.],\n", " [ 0., 1., 0., 1.],\n", " [ 1., 0., -1., 0.],\n", " [ 0., 1., 0., -1.]], dtype=float32), array([0., 0., 0., 0.], dtype=float32)]\n", "[array([[ 1., -1., 0., 0.],\n", " [ 1., 1., 0., 0.],\n", " [ 0., 0., 1., -1.],\n", " [ 0., 0., -1., -1.]], dtype=float32), array([0., 0., 0., 0.], dtype=float32)]\n", "[array([[ 1., -1., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 1., -1., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 1., -1., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 1., -1.]], dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]\n", "[array([[1., 0., 0., 0.],\n", " [1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", " [0., 0., 1., 0.],\n", " [0., 0., 0., 1.],\n", " [0., 0., 0., 1.]], dtype=float32), array([0., 0., 0., 0.], dtype=float32)]\n" ] } ], "source": [ "for layer in model4.layers:\n", " print(layer.get_weights())" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.17831734, 0.17831734, 0.17831734, 0.465048 ]], dtype=float32)" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model4.predict([np.array([[1.0, 1.0], [-1.0, -1.0]]).reshape(1, 4)])" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test loss: 0.7656148672103882\n", "Test accuracy: 1.0\n" ] } ], "source": [ "score = model4.evaluate(x4_test, y4_test, verbose=0)\n", "\n", "print('Test loss: {}'.format(score[0]))\n", "print('Test accuracy: {}'.format(score[1]))" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"sequential_25\"\n", "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "dense_75 (Dense) (None, 4) 20 \n", "_________________________________________________________________\n", "dense_76 (Dense) (None, 4) 20 \n", "_________________________________________________________________\n", "dense_77 (Dense) (None, 8) 40 \n", "_________________________________________________________________\n", "dense_78 (Dense) (None, 4) 36 \n", "=================================================================\n", "Total params: 116\n", "Trainable params: 116\n", "Non-trainable params: 0\n", "_________________________________________________________________\n" ] } ], "source": [ "model5 = Sequential()\n", "model5.add(Dense(4, activation='tanh', input_shape=(4,)))\n", "model5.add(Dense(4, activation='tanh'))\n", "model5.add(Dense(8, activation='relu'))\n", "model5.add(Dense(num_classes, activation='softmax'))\n", "model5.compile(loss='categorical_crossentropy',\n", " optimizer=keras.optimizers.RMSprop(),\n", " metrics=['accuracy'])\n", "model5.summary()" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "scrolled": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/8\n", "125/125 [==============================] - 0s 3ms/step - loss: 1.3126 - accuracy: 0.3840 - val_loss: 1.1926 - val_accuracy: 0.6110\n", "Epoch 2/8\n", "125/125 [==============================] - 0s 2ms/step - loss: 1.0978 - accuracy: 0.5980 - val_loss: 1.0085 - val_accuracy: 0.6150\n", "Epoch 3/8\n", "125/125 [==============================] - 0s 2ms/step - loss: 0.9243 - accuracy: 0.7035 - val_loss: 0.8416 - val_accuracy: 0.7380\n", "Epoch 4/8\n", "125/125 [==============================] - 0s 2ms/step - loss: 0.7522 - accuracy: 0.8740 - val_loss: 0.6738 - val_accuracy: 1.0000\n", "Epoch 5/8\n", "125/125 [==============================] - 0s 2ms/step - loss: 0.5811 - accuracy: 1.0000 - val_loss: 0.5030 - val_accuracy: 1.0000\n", "Epoch 6/8\n", "125/125 [==============================] - 0s 2ms/step - loss: 0.4134 - accuracy: 1.0000 - val_loss: 0.3428 - val_accuracy: 1.0000\n", "Epoch 7/8\n", "125/125 [==============================] - 0s 2ms/step - loss: 0.2713 - accuracy: 1.0000 - val_loss: 0.2161 - val_accuracy: 1.0000\n", "Epoch 8/8\n", "125/125 [==============================] - 0s 1ms/step - loss: 0.1621 - accuracy: 1.0000 - val_loss: 0.1225 - val_accuracy: 1.0000\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model5.fit(x4_train, y4_train, epochs=8, validation_data=(x4_test, y4_test))" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[3.2040708e-02, 1.0065207e-03, 4.9596769e-04, 9.6645677e-01]],\n", " dtype=float32)" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model5.predict([np.array([[1.0, 1.0], [-1.0, -1.0]]).reshape(1, 4)])" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Test loss: 0.1224619448184967\n", "Test accuracy: 1.0\n" ] } ], "source": [ "score = model5.evaluate(x4_test, y4_test, verbose=0)\n", "\n", "print('Test loss: {}'.format(score[0]))\n", "print('Test accuracy: {}'.format(score[1]))" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "slideshow": { "slide_type": "notes" } }, "outputs": [], "source": [ "import contextlib\n", "\n", "@contextlib.contextmanager\n", "def printoptions(*args, **kwargs):\n", " original = np.get_printoptions()\n", " np.set_printoptions(*args, **kwargs)\n", " try:\n", " yield\n", " finally: \n", " np.set_printoptions(**original)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[array([[ 0.7, 0.2, -0.7, 0.7],\n", " [-0.5, 0.9, 0.6, 0.6],\n", " [ 1.1, 0.2, 0.1, 0.2],\n", " [ 0.7, 0.1, 0.3, -0.7]], dtype=float32), array([ 0. , 0.1, -0.1, -0.2], dtype=float32)]\n", "[array([[ 0.7, 0.5, -1.1, -1.2],\n", " [ 0.7, 0.9, -0.6, 0.3],\n", " [ 0.1, 1.4, -0.6, 0.8],\n", " [ 1.5, 0.1, -0.1, 0.9]], dtype=float32), array([-0.4, 0.2, -0. , 0.2], dtype=float32)]\n", "[array([[-1. , 1. , -0.7, -0.3, 0.2, 1.3, -0.7, 0.9],\n", " [-0.9, 0.5, 0.8, -1.3, -1.2, 1.3, 0.4, -1. ],\n", " [ 0.9, 0.2, 0.3, 0.4, 1.3, -0.9, -0.1, -0.2],\n", " [-0.4, 0.5, 1.1, -0.6, 1.1, 0.1, -1.5, -1. ]], dtype=float32), array([-0.1, 0.1, 0.1, 0.1, 0.2, -0. , 0.1, 0.2], dtype=float32)]\n", "[array([[ 0.7, -0.5, 0.8, -0.5],\n", " [-0.3, -1.6, -0.2, 0.1],\n", " [-1.5, 0.9, 0.1, -0.5],\n", " [ 0.6, 0.7, 1. , -1.4],\n", " [ 0.7, -1.2, -1.6, 1.2],\n", " [ 1. , -1.2, 0.3, -1.5],\n", " [-0.2, 0. , 0.6, 1.3],\n", " [-0.8, 0.2, -0.6, -1. ]], dtype=float32), array([-0.6, 0.5, -0.3, 0.4], dtype=float32)]\n" ] } ], "source": [ "with printoptions(precision=1, suppress=True):\n", " for layer in model5.layers:\n", " print(layer.get_weights())" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "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.8.5" }, "livereveal": { "start_slideshow_at": "selected", "theme": "white" } }, "nbformat": 4, "nbformat_minor": 4 }