From 57151930ded4da2911e728cc399f75ad2041f97b Mon Sep 17 00:00:00 2001 From: udlbook <110402648+udlbook@users.noreply.github.com> Date: Thu, 27 Nov 2025 15:50:21 -0500 Subject: [PATCH] Created using Colab --- .../ShallowNN/LinearRegions_Answers.ipynb | 440 ++++++++++++++++++ 1 file changed, 440 insertions(+) create mode 100644 notebooks/ShallowNN/LinearRegions_Answers.ipynb diff --git a/notebooks/ShallowNN/LinearRegions_Answers.ipynb b/notebooks/ShallowNN/LinearRegions_Answers.ipynb new file mode 100644 index 0000000..491d113 --- /dev/null +++ b/notebooks/ShallowNN/LinearRegions_Answers.ipynb @@ -0,0 +1,440 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyOq8T2wOrCjh5hFtI51OCMX", + "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 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", + " # Tou can use math.comb() https://www.w3schools.com/python/ref_math_comb.asp\n", + " # Replace this code\n", + " N = 1;\n", + " # BEGIN_ANSWER\n", + " N= 0\n", + " for j in range (Di+1):\n", + " N = N+ math.comb(D, j)\n", + " # END_ANSWER\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": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "AqSUfuJDigN9", + "outputId": "61be2ce7-7d38-4eab-8cae-1ec410702a3a" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Di=2, D=3, Number of regions = 7, True value = 7\n" + ] + } + ] + }, + { + "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": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "krNKPV9gjCu-", + "outputId": "21b472e5-f0d2-4794-a155-d9d3a3faaf2e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Di=10, D=50, Number of regions = 13432735556, True value = 13432735556\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Calculate the number of regions for 5D input (Di=5) and 50 hidden units (D=50)\n", + "N = number_regions(5, 50)\n", + "print(f\"Di=5, D=40, Number of regions = {int(N)}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7g4j7OXaSniQ", + "outputId": "70e4e022-5485-41e6-fce6-e0536669ed8a" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Di=5, D=40, Number of regions = 2369936\n" + ] + } + ] + }, + { + "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)}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "OfgVQzHBfgI7", + "outputId": "19c72b90-ddfe-42ba-c725-16b4bde97c3f" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Di=10, D=50, Number of regions = 13432735556\n" + ] + } + ] + }, + { + "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": [ + "# 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": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uq5IeAZTkIMg", + "outputId": "908f3d89-585c-4f45-f88d-386be37f3ea2" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Di=10, D=8, Number of regions = 256, True value = 256\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Let's do the calculation properly when D