{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# **Notebook 3.3 -- Shallow network regions**\n", "\n", "The purpose of this notebook is to compute the maximum possible number of linear regions as seen in figure 3.9 of the book.\n", "\n", "Work through the cells below, running each cell in turn. In various places you will see the words \"TODO\". Follow the instructions at these places and write code to complete the functions. There are also questions interspersed in the text.\n", "\n", "Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions." ], "metadata": { "id": "DCTC8fQ6cp-n" } }, { "cell_type": "code", "source": [ "# Imports math library\n", "import numpy as np\n", "# Imports plotting library\n", "import matplotlib.pyplot as plt\n", "# Imports math library\n", "import math" ], "metadata": { "id": "W3C1ZA1gcpq_" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "The number of regions $N$ created by a shallow neural network with $D_i$ inputs and $D$ hidden units is given by Zaslavsky's formula:\n", "\n", "\\begin{equation}N = \\sum_{j=0}^{D_{i}}\\binom{D}{j}=\\sum_{j=0}^{D_{i}} \\frac{D!}{(D-j)!j!} \\end{equation} \n", "\n" ], "metadata": { "id": "TbfanfXBe84L" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "4UQ2n0RWcgOb" }, "outputs": [], "source": [ "def number_regions(Di, D):\n", " # TODO -- implement Zaslavsky's formula\n", " # You can use math.comb() https://www.w3schools.com/python/ref_math_comb.asp\n", " # Replace this code\n", " N = 1;\n", "\n", " return N" ] }, { "cell_type": "code", "source": [ "# Calculate the number of regions for 2D input (Di=2) and 3 hidden units (D=3) as in figure 3.8j\n", "N = number_regions(2, 3)\n", "print(f\"Di=2, D=3, Number of regions = {int(N)}, True value = 7\")" ], "metadata": { "id": "AqSUfuJDigN9" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Calculate the number of regions for 10D input (Di=10) and 50 hidden units (D=50)\n", "N = number_regions(10, 50)\n", "print(f\"Di=10, D=50, Number of regions = {int(N)}, True value = 13432735556\")" ], "metadata": { "id": "krNKPV9gjCu-" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "This works but there is a complication. If the number of hidden units $D$ is fewer than the number of input dimensions $D_i$ , the formula will fail. When this is the case, there are just $2^D$ regions (see figure 3.10 to understand why).\n", "\n", "Let's demonstrate this:" ], "metadata": { "id": "rk1a2LqGkO9u" } }, { "cell_type": "code", "source": [ "# Depending on how you implemented it, the calculation may fail when $D_i > D$ (not to worry...)\n", "try:\n", " N = number_regions(10, 8)\n", " print(f\"Di=10, D=8, Number of regions = {int(N)}, True value = 256\")\n", "except Exception as error:\n", " print(\"An exception occurred:\", error)\n" ], "metadata": { "id": "uq5IeAZTkIMg" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Let's do the calculation properly when D