2023-05-25 10:22:21 +02:00
{
"cells": [
{
2023-05-25 11:00:24 +02:00
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "raw",
"metadata": {},
"source": []
},
{
2023-05-25 10:22:21 +02:00
"cell_type": "markdown",
"metadata": {},
"source": [
"## Uczenie maszynowe – zastosowania\n",
"### Zajęcia laboratoryjne\n",
"# 10. Wprowadzenie do sieci neuronowych"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Poniżej znajduje się implementacja prostej sieci neuronowej dla problemu klasyfikacji binarnej na przykładzie losowo wygenerowanego zestawu danych.\n",
"\n",
"W sieciach jednokierunkowych (ang. *feedforward*) wartości neuronów w $i$-tej warstwie są obliczane na podstawie wartości neuronów warstwy $i-1$. Mając daną $n$-warstwową sieć neuronową oraz jej parametry $\\Theta^{(1)}, \\ldots, \\Theta^{(n)} $ oraz $\\beta^{(1)}, \\ldots, \\beta^{(n)}$ liczymy: \n",
"$$a^{(i)} = g^{(i)}\\left( a^{(i-1)} \\Theta^{(i)} + \\beta^{(i)} \\right) \\; , $$\n",
"gdzie $g^{(i)}$ to tzw. **funkcje aktywacji**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Zadanie 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Część podstawowa (4 punkty)\n",
"\n",
" 1. Zaimplementuj funkcję `accuracy()` liczącą skuteczność klasyfikacji we wskazanym miejscu.\n",
" 2. Za jej pomocą oblicz i wypisz końcową skuteczność klasyfikatora.\n",
" 3. Wypisuj również wartość `accuracy` podczas uczenia (przy okazji wypisywania wartości funkcji kosztu).\n",
" 4. Zbuduj sieci neuronowe dla różnych wielkości warstwy ukrytej (`dim_hid` = 1, 2, 5, 10, 25). Porównaj skuteczność tych modeli."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Część zaawansowana (3 punkty)\n",
"\n",
"Zastosuj poniższą implementację sieci neuronowej do klasyfikacji binarnej zbioru wygenerowanego za pomocą wybranej funkcji [sklearn.datasets](http://scikit-learn.org/stable/modules/classes.html#samples-generator). Ustal rozmiary warstw wejściowej i wyjściowej, dobierz odpowiednie parametry sieci (parametr $\\alpha$, liczba epok, wielkość warstwy ukrytej). Podaj skuteczność klasyfikacji."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"from sklearn import datasets\n",
"import matplotlib.pyplot as plt\n",
"\n",
"\n",
"def generate_data():\n",
" # Keep results deterministic\n",
" np.random.seed(1234)\n",
" X, y = datasets.make_moons(200, noise=0.25)\n",
" # X, y = datasets.make_classification(200, 2, 2, 0)\n",
" return X, y\n",
"\n",
"def visualize(X, y, model=None):\n",
" x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5\n",
" y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5\n",
" h = 0.01\n",
" xx, yy = np.meshgrid(\n",
" np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))\n",
" if model:\n",
" Z = predict(model, np.c_[xx.ravel(), yy.ravel()])\n",
" Z = Z.reshape(xx.shape)\n",
" plt.contourf(xx, yy, Z, cmap=plt.cm.viridis)\n",
" plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.viridis)\n",
" plt.show()\n",
"\n",
"def initialize_model(dim_in=2, dim_hid=3, dim_out=2):\n",
" # Keep results deterministic\n",
" np.random.seed(1234)\n",
" W1 = np.random.randn(dim_in, dim_hid) / np.sqrt(dim_in)\n",
" b1 = np.zeros((1, dim_hid))\n",
" W2 = np.random.randn(dim_hid, dim_out) / np.sqrt(dim_hid)\n",
" b2 = np.zeros((1, dim_out))\n",
" return W1, b1, W2, b2\n",
"\n",
"def softmax(X):\n",
" e = np.exp(X)\n",
" return e / np.sum(e, axis=1, keepdims=True)\n",
"\n",
"def predict(model, X):\n",
" W1, b1, W2, b2 = model\n",
" z1 = X.dot(W1) + b1\n",
" a1 = np.tanh(z1)\n",
" z2 = a1.dot(W2) + b2\n",
" probs = softmax(z2)\n",
" return np.argmax(probs, axis=1)\n",
"\n",
"def calculate_cost(model, X, y):\n",
" W1, b1, W2, b2 = model\n",
" z1 = X.dot(W1) + b1\n",
" a1 = np.tanh(z1)\n",
" z2 = a1.dot(W2) + b2\n",
" probs = softmax(z2)\n",
" preds = probs[:, 1]\n",
" return -1. / len(y) * np.sum(\n",
" np.multiply(y, np.log(preds)) + np.multiply(1 - y, np.log(1 - preds)),\n",
" axis=0)\n",
"\n",
"def accuracy(model, X, y):\n",
" # TODO: 1. Napisz funkcję obliczającą skuteczność (accuracy).\n",
" # Skorzystaj z funkcji `predict`.\n",
" pass\n",
"\n",
"def train(model, X, y, alpha=0.01, epochs=10000, debug=False):\n",
" W1, b1, W2, b2 = model\n",
" m = len(X)\n",
"\n",
" for i in range(epochs):\n",
" # Forward propagation\n",
" z1 = X.dot(W1) + b1\n",
" a1 = np.tanh(z1)\n",
" z2 = a1.dot(W2) + b2\n",
" probs = softmax(z2)\n",
"\n",
" # Backpropagation\n",
" delta3 = probs\n",
" delta3[range(m), y] -= 1\n",
" dW2 = (a1.T).dot(delta3)\n",
" db2 = np.sum(delta3, axis=0, keepdims=True)\n",
" delta2 = delta3.dot(W2.T) * (1 - np.power(a1, 2))\n",
" dW1 = np.dot(X.T, delta2)\n",
" db1 = np.sum(delta2, axis=0)\n",
"\n",
" # Parameter update\n",
" W1 -= alpha * dW1\n",
" b1 -= alpha * db1\n",
" W2 -= alpha * dW2\n",
" b2 -= alpha * db2\n",
"\n",
" # Print loss\n",
" if debug and i % 1000 == 0:\n",
" model = (W1, b1, W2, b2)\n",
" print(\"Cost after iteration {}: {:.4f}\".format(i, calculate_cost(\n",
" model, X, y)))\n",
" # TODO: 3. Wypisz skuteczność (accuracy) klasyfikacji w tym miejscu.\n",
" \n",
" # TODO: 2. Wypisz końcową skuteczność (accuracy) klasyfikacji w tym miejscu.\n",
" return W1, b1, W2, b2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD4CAYAAAAAczaOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOydeZyN1RvAv+e9+53V2Pc1QpFkS7IVIlHWSiElIS3ULylKlmhRCC229lRUosVSRCL7vu/bmDGY5e73Pb8/7hhz3TtjlnvHyPv9fJR5l3Oe19z7vOc8q5BSoqGhoaFxfaBcbQE0NDQ0NAoOTelraGhoXEdoSl9DQ0PjOkJT+hoaGhrXEZrS19DQ0LiO0F9tAbKjWLFislKlSldbDA0NDY1rhg0bNiRKKYtndb5QK/1KlSqxfv36qy2GhoaGxjWDEOJIduc1846GhobGdYSm9DU0NDSuIzSlr6GhoXEdoSl9DQ0NjesITelrXJecOZrA1pU7OZ9w4WqLoqFRoBTq6B0NjVDjsDkZ++B7bFyyBYPJgMvppl3flgye0g9F0dZAGv99NKWvEXb2bzrEt+8s5OSB09RtXosHnu1AXKkiV0WWD4bMYuOSLbgcblwONwC/z11B2RtK0+XZe6+KTBoaBYkozKWVb7vtNqnF6V/brFm4nrEPTsLlcCNVicGkxxJpZvqGiZSokGX+SFhwu9x0iu2NO13ZZ6ZEhWJ8cXh6gcqjoREOhBAbpJS3ZXVe289qhA1VVXlvwIc4bS6k6ltcuJ0eUs/bmDtqXtjnl64NqOcGoCZ2RE1+A2facVSPGvTa1PNpYZdHQ6MwoJl3NMJG4okk0s7bAo6rXpX1v28J69yqfSFcGAE4fAc8B7DwE6UqNeDE/gS/a4UQ1GleO6zyaGgUFrSVvkbYiIi2oKrBV9bRRSPDNq+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mYa1y6a0r9KbF+1i+H3jMXr9uJ2eVi7aCPfTPyBaesnEFMs+mqLF4CUkpXfrmHhjN9xOdw069Y4qIIGn7K3RJkZs/ClHFfFBKhWrxKnD53h8uACp8PFqM4T0uUgQ9lnxuP28ufXq1kx729qNa7OsFkDKVe9DL9/+ifTn52D1+PF4/ZS/+46vPTZ0xm7hnCTdj4tqLxej5e0C5f6B5esWIxD244GXGcw6kOSoKahcRFtz3gVkFIysc9UHGlO3BfrtdicJJ06x+ejv7vK0gXnvSc/5O1+09jy5w52/bOXua9+jSXSjMHkv27QG/W069eKeac+znVZhl6vdsN4WRarUITvxej04HZ6girQzEhVsnPNXoY0HcHaxRuYPPBjUs+nYU914Ha62bBkC2N6TsqVXPmhQbt6mCNMgSeEoH6mXdCjr3UPMIWZrCa6PN8RnT4wsUxDI69oSv8qcPZkEmdPBppGPG4vq35YexUkyp5je06w9POVONIu2e+dNl8t+tJVS2GyGomIsaYXR2vBszP6B5hYckKVOhV5a9koajWp7sugFT4lnluklLjtbj5+8fMMp/BF3E4PW1fsJOF4YPnmcFCneS1ua3uLn+I3R5i457FWVLjxUiz/Hfc34umpjxNbIga9UY8lykz3F+7jkZFdC0ROjeuHkJh3hBCzgHuBM1LKm4KcF8D7QHvABvSRUm4MxdzXIkazMUtlZrbmXlmGgnPx51nzky8nonHH+sSVKpJxbuuKnYggjkRnmpM6zWrS6evnWPPzBoqUiKHp/Q3z5XQsXr4oDw5/gB+m/sKmpVtRs8kj0Rv1Wa78HTYnSaeD19zRG/UknT5/xW5fUkp+nb2cr8YtIOn0eW64tTJPTHyEWo2r5/h5hBC8Ou95Vi9Yx9LPV6I36GjbtxUN2t0ScG3bPi25+9Hm2FPsmCPN6HTaCl8j9ITKpj8HmAp
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"X, y = generate_data()\n",
"visualize(X, y)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cost after iteration 0: 0.4692\n",
"Cost after iteration 1000: 0.1527\n",
"Cost after iteration 2000: 0.1494\n",
"Cost after iteration 3000: 0.1478\n",
"Cost after iteration 4000: 0.1458\n",
"Cost after iteration 5000: 0.1444\n",
"Cost after iteration 6000: 0.1433\n",
"Cost after iteration 7000: 0.1423\n",
"Cost after iteration 8000: 0.1415\n",
"Cost after iteration 9000: 0.1407\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9eZRs113f+/3tfU7NXT1331FXsmZZRp5wbOHEhjCawXJMgg3IYLIiDBjwswKYB36QBNYTcXBsx2CjBTbIsAzkERkTC4ghC3CQjSyM5UmzdK90x749VlXXdM7ev/fHPqe6hnNq7uru2/uz1pW665w+Q9Wp3977N3x/xMywWCwWy5WP2OsLsFgsFstksAbfYrFYDgnW4FssFsshwRp8i8ViOSRYg2+xWCyHBGevL6AbCZHmtJPf68uwWCz7nBM3bez1JewbvvZlb5WZF6O27WuDn3byuH3p+/b6MiwWyz7nnv95/15fwr7hxafOnonbZl06FovlQPP4z57a60s4MFiDb7FYLIcEa/AtFsuB5v473rfXl3BgsAbfYrEcWCq3Ht/rSzhQWINvsVgOLO+/94N7fQkHCmvwLRaL5ZBgDb7FYjmQ2OycwbEG32KxHEhy12zt9SUcOKzBt1gsB47Krcfxsds+uteXceCwBt9isVgOCdbgWyyWA4fNzhkOa/AtFovlkGANvsViOVC88FOX9voSDizW4FssFsshwRp8i8VyoLhz9sG9voQDizX4FovlwHD+jdfs9SUcaMZi8InoI0S0QkRfid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y4KzmYbB19vb4EKx82+YQW7
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"model1 = train(initialize_model(dim_hid=5), X, y, debug=True)\n",
"visualize(X, y, model1)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cost after iteration 0: 0.6739\n",
"Cost after iteration 1000: 0.3618\n",
"Cost after iteration 2000: 0.3618\n",
"Cost after iteration 3000: 0.3618\n",
"Cost after iteration 4000: 0.3618\n",
"Cost after iteration 5000: 0.3618\n",
"Cost after iteration 6000: 0.3618\n",
"Cost after iteration 7000: 0.3618\n",
"Cost after iteration 8000: 0.3618\n",
"Cost after iteration 9000: 0.3618\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOy9eZxkZ13v//4+59TSVb1Pz57JZJIMCUmQRQg3gIIIioiiBAT5EQW5F1G496XAzQUV+XmVK+6iQDDIIuoFUS9clgiKyCKLLCGBrGSbLLP39N5d2znP9/7xnKqu5Zzuqu7qZWbO+/WaZKbqdJ1T1d2f8zzf5fMVVSUlJSUl5dzHbPUFpKSkpKRsDqngp6SkpJwnpIKfkpKScp6QCn5KSkrKeUIq+CkpKSnnCf5WX8BKZM2ADvjDW30ZKSkpKWcNc7VTk6q6M+65bS34A/4wT9n14q2+jJSUlJSzhk8f/fMHk55LQzopKSkp5wmp4KekpKScJ6SCn5KSknKekAp+SkpKynlCKvgpKSkp5wmp4KekpKScJ6SCn5KSknKekAp+SkpKynlCKvgpKSkp5w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XFLu7MU8X283bvQShVs6GwKfN+thk9PLle1qKLzC9ha4FaZqq4mPKqLV0CrVbRQwOvG42Yl4VXFKxYJFXR+3h0r4v4kVNmotbE/FC10s2NoplzGlsvI0CAEgUvUeh5maBCTy6GFARc+CQIkm8EUB8EI4Zkpd5NsRgRW8tZJve7PWkpX7WfqNYvbqiFqs9gswX8EOND07wuAY5t07nMOEXEzYZuwi0ux4qrlcpRorXU
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"model2 = train(initialize_model(dim_hid=1), X, y, debug=True)\n",
"visualize(X, y, model2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}