From 46bc9d5723dc230960834d3516ac99ca1a5b61fe Mon Sep 17 00:00:00 2001 From: udlbook <110402648+udlbook@users.noreply.github.com> Date: Tue, 25 Jul 2023 12:39:54 -0400 Subject: [PATCH] Created using Colaboratory --- .../Chap03/3_3_Shallow_Network_Regions.ipynb | 259 ++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 Notebooks/Chap03/3_3_Shallow_Network_Regions.ipynb diff --git a/Notebooks/Chap03/3_3_Shallow_Network_Regions.ipynb b/Notebooks/Chap03/3_3_Shallow_Network_Regions.ipynb new file mode 100644 index 0000000..dc068d8 --- /dev/null +++ b/Notebooks/Chap03/3_3_Shallow_Network_Regions.ipynb @@ -0,0 +1,259 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyMhLSGU8+odPS/CoW5PwKna", + "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 \"TO DO\". 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 libray\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=1}^{D_{i}}\\binom{D}{j}=\\sum_{j=1}^{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 will need to use math.factorial() https://www.geeksforgeeks.org/factorial-in-python/\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=2) 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 hidden 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": [ + "# Show that calculation fails when $D_i < D$\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