Compare commits

...

19 Commits

Author SHA1 Message Date
udlbook
33197fde36 Add files via upload 2024-11-21 16:45:29 -05:00
udlbook
6d425c04d4 Update 3_3_Shallow_Network_Regions.ipynb 2024-11-18 15:33:42 -05:00
udlbook
57c95132d3 Created using Colab 2024-11-12 17:11:44 -05:00
udlbook
2b0ac95740 Created using Colab 2024-11-08 12:31:21 -05:00
udlbook
d5f198f2d8 Add files via upload 2024-11-04 15:25:38 -05:00
udlbook
4edd8c923d Add files via upload 2024-10-30 16:51:41 -04:00
udlbook
3801b8d52d Created using Colab 2024-10-24 16:45:43 -04:00
udlbook
dc6b346bda Created using Colab 2024-10-24 16:43:14 -04:00
udlbook
5eb264540d Created using Colab 2024-10-24 16:40:27 -04:00
udlbook
7ba844f2b5 Created using Colab 2024-10-24 16:04:27 -04:00
udlbook
d101aa428b Merge pull request #236 from aleksandrskoselevs/patch-1
Update 13_4_Graph_Attention_Networks.ipynb
2024-10-15 17:24:40 -04:00
aleksandrskoselevs
8c6e40daee Update 13_4_Graph_Attention_Networks.ipynb
`phi` is defined in the book as a column vector
2024-10-11 10:54:05 +02:00
udlbook
efafb942eb Add files via upload 2024-10-01 15:14:01 -04:00
udlbook
b10a2b6940 Delete UDL_Answer_Booklet.pdf 2024-10-01 15:13:35 -04:00
udlbook
ede7247a0c Add files via upload 2024-10-01 15:13:14 -04:00
udlbook
c3b97af456 Created using Colab 2024-09-16 09:21:22 -04:00
udlbook
e1df2156a3 Created using Colab 2024-09-16 09:19:49 -04:00
udlbook
f887835646 Created using Colab 2024-09-16 09:18:12 -04:00
udlbook
e9c8d846f2 Created using Colab 2024-09-16 07:36:27 -04:00
11 changed files with 141 additions and 48 deletions

View File

@@ -4,7 +4,6 @@
"metadata": { "metadata": {
"colab": { "colab": {
"provenance": [], "provenance": [],
"authorship_tag": "ABX9TyNioITtfAcfxEfM3UOfQyb9",
"include_colab_link": true "include_colab_link": true
}, },
"kernelspec": { "kernelspec": {
@@ -62,7 +61,7 @@
"source": [ "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", "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", "\n",
"\\begin{equation}N = \\sum_{j=0}^{D_{i}}\\binom{D}{j}=\\sum_{j=0}^{D_{i}} \\frac{D!}{(D-j)!j!} \\end{equation} <br>\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" "\n"
], ],
"metadata": { "metadata": {
@@ -221,7 +220,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"# Now let's plot the graph from figure 3.9a (takes ~1min)\n", "# Now let's plot the graph from figure 3.9b (takes ~1min)\n",
"dims = np.array([1,5,10,50,100])\n", "dims = np.array([1,5,10,50,100])\n",
"regions = np.zeros((dims.shape[0], 200))\n", "regions = np.zeros((dims.shape[0], 200))\n",
"params = np.zeros((dims.shape[0], 200))\n", "params = np.zeros((dims.shape[0], 200))\n",

View File

@@ -130,7 +130,8 @@
"\n", "\n",
" print('Iter %d, a=%3.3f, b=%3.3f, c=%3.3f, d=%3.3f'%(n_iter, a,b,c,d))\n", " print('Iter %d, a=%3.3f, b=%3.3f, c=%3.3f, d=%3.3f'%(n_iter, a,b,c,d))\n",
"\n", "\n",
" # Rule #1 If the HEIGHT at point A is less than the HEIGHT at points B, C, and D then halve values of B, C, and D\n", " # Rule #1 If the HEIGHT at point A is less than the HEIGHT at points B, C, and D then move them to they are half\n",
" # as far from A as they start\n",
" # i.e. bring them closer to the original point\n", " # i.e. bring them closer to the original point\n",
" # TODO REPLACE THE BLOCK OF CODE BELOW WITH THIS RULE\n", " # TODO REPLACE THE BLOCK OF CODE BELOW WITH THIS RULE\n",
" if (0):\n", " if (0):\n",

View File

@@ -1,18 +1,16 @@
{ {
"cells": [ "cells": [
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"colab_type": "text", "id": "view-in-github",
"id": "view-in-github" "colab_type": "text"
}, },
"source": [ "source": [
"<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Notebooks/Chap06/6_2_Gradient_Descent.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" "<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Notebooks/Chap06/6_2_Gradient_Descent.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "el8l05WQEO46" "id": "el8l05WQEO46"
@@ -111,7 +109,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "QU5mdGvpTtEG" "id": "QU5mdGvpTtEG"
@@ -140,7 +137,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "eB5DQvU5hYNx" "id": "eB5DQvU5hYNx"
@@ -162,7 +158,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "F3trnavPiHpH" "id": "F3trnavPiHpH"
@@ -218,7 +213,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "s9Duf05WqqSC" "id": "s9Duf05WqqSC"
@@ -252,7 +246,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "RS1nEcYVuEAM" "id": "RS1nEcYVuEAM"
@@ -290,7 +283,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "5EIjMM9Fw2eT" "id": "5EIjMM9Fw2eT"
@@ -333,11 +325,11 @@
" print('Iter %d, a=%3.3f, b=%3.3f, c=%3.3f, d=%3.3f'%(n_iter, a,b,c,d))\n", " print('Iter %d, a=%3.3f, b=%3.3f, c=%3.3f, d=%3.3f'%(n_iter, a,b,c,d))\n",
" print('a %f, b%f, c%f, d%f'%(lossa,lossb,lossc,lossd))\n", " print('a %f, b%f, c%f, d%f'%(lossa,lossb,lossc,lossd))\n",
"\n", "\n",
" # Rule #1 If point A is less than points B, C, and D then halve points B,C, and D\n", " # Rule #1 If point A is less than points B, C, and D then halve distance from A to points B,C, and D\n",
" if np.argmin((lossa,lossb,lossc,lossd))==0:\n", " if np.argmin((lossa,lossb,lossc,lossd))==0:\n",
" b = b/2\n", " b = a+ (b-a)/2\n",
" c = c/2\n", " c = a+ (c-a)/2\n",
" d = d/2\n", " d = a+ (d-a)/2\n",
" continue;\n", " continue;\n",
"\n", "\n",
" # Rule #2 If point b is less than point c then\n", " # Rule #2 If point b is less than point c then\n",
@@ -412,8 +404,8 @@
], ],
"metadata": { "metadata": {
"colab": { "colab": {
"include_colab_link": true, "provenance": [],
"provenance": [] "include_colab_link": true
}, },
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python 3",
@@ -425,4 +417,4 @@
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 0
} }

View File

@@ -1,18 +1,16 @@
{ {
"cells": [ "cells": [
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"colab_type": "text", "id": "view-in-github",
"id": "view-in-github" "colab_type": "text"
}, },
"source": [ "source": [
"<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Notebooks/Chap06/6_3_Stochastic_Gradient_Descent.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" "<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Notebooks/Chap06/6_3_Stochastic_Gradient_Descent.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "el8l05WQEO46" "id": "el8l05WQEO46"
@@ -122,7 +120,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "QU5mdGvpTtEG" "id": "QU5mdGvpTtEG"
@@ -150,7 +147,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "eB5DQvU5hYNx" "id": "eB5DQvU5hYNx"
@@ -172,7 +168,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "F3trnavPiHpH" "id": "F3trnavPiHpH"
@@ -228,7 +223,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "s9Duf05WqqSC" "id": "s9Duf05WqqSC"
@@ -279,7 +273,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "RS1nEcYVuEAM" "id": "RS1nEcYVuEAM"
@@ -316,7 +309,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "5EIjMM9Fw2eT" "id": "5EIjMM9Fw2eT"
@@ -359,11 +351,11 @@
" print('Iter %d, a=%3.3f, b=%3.3f, c=%3.3f, d=%3.3f'%(n_iter, a,b,c,d))\n", " print('Iter %d, a=%3.3f, b=%3.3f, c=%3.3f, d=%3.3f'%(n_iter, a,b,c,d))\n",
" print('a %f, b%f, c%f, d%f'%(lossa,lossb,lossc,lossd))\n", " print('a %f, b%f, c%f, d%f'%(lossa,lossb,lossc,lossd))\n",
"\n", "\n",
" # Rule #1 If point A is less than points B, C, and D then halve points B,C, and D\n", " # Rule #1 If point A is less than points B, C, and D then change B,C,D so they are half their current distance from A\n",
" if np.argmin((lossa,lossb,lossc,lossd))==0:\n", " if np.argmin((lossa,lossb,lossc,lossd))==0:\n",
" b = b/2\n", " b = a+ (b-a)/2\n",
" c = c/2\n", " c = a+ (c-a)/2\n",
" d = d/2\n", " d = a+ (d-a)/2\n",
" continue;\n", " continue;\n",
"\n", "\n",
" # Rule #2 If point b is less than point c then\n", " # Rule #2 If point b is less than point c then\n",
@@ -577,9 +569,8 @@
], ],
"metadata": { "metadata": {
"colab": { "colab": {
"authorship_tag": "ABX9TyNk5FN4qlw3pk8BwDVWw1jN", "provenance": [],
"include_colab_link": true, "include_colab_link": true
"provenance": []
}, },
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python 3",
@@ -591,4 +582,4 @@
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 0
} }

View File

@@ -4,7 +4,6 @@
"metadata": { "metadata": {
"colab": { "colab": {
"provenance": [], "provenance": [],
"authorship_tag": "ABX9TyM2kkHLr00J4Jeypw41sTkQ",
"include_colab_link": true "include_colab_link": true
}, },
"kernelspec": { "kernelspec": {
@@ -230,8 +229,8 @@
"# We'll need the indicator function\n", "# We'll need the indicator function\n",
"def indicator_function(x):\n", "def indicator_function(x):\n",
" x_in = np.array(x)\n", " x_in = np.array(x)\n",
" x_in[x_in>=0] = 1\n", " x_in[x_in>0] = 1\n",
" x_in[x_in<0] = 0\n", " x_in[x_in<=0] = 0\n",
" return x_in\n", " return x_in\n",
"\n", "\n",
"# Main backward pass routine\n", "# Main backward pass routine\n",
@@ -353,4 +352,4 @@
"outputs": [] "outputs": []
} }
] ]
} }

View File

@@ -4,7 +4,7 @@
"metadata": { "metadata": {
"colab": { "colab": {
"provenance": [], "provenance": [],
"authorship_tag": "ABX9TyNAcc98STMeyQgh9SbVHWG+", "authorship_tag": "ABX9TyORZF8xy4X1yf4oRhRq8Rtm",
"include_colab_link": true "include_colab_link": true
}, },
"kernelspec": { "kernelspec": {
@@ -65,10 +65,19 @@
"source": [ "source": [
"# Run this once to load the train and test data straight into a dataloader class\n", "# Run this once to load the train and test data straight into a dataloader class\n",
"# that will provide the batches\n", "# that will provide the batches\n",
"\n",
"# (It may complain that some files are missing because the files seem to have been\n",
"# reorganized on the underlying website, but it still seems to work). If everything is working\n",
"# properly, then the whole notebook should run to the end without further problems\n",
"# even before you make changes.\n",
"batch_size_train = 64\n", "batch_size_train = 64\n",
"batch_size_test = 1000\n", "batch_size_test = 1000\n",
"\n",
"# TODO Change this directory to point towards an existing directory\n",
"myDir = '/files/'\n",
"\n",
"train_loader = torch.utils.data.DataLoader(\n", "train_loader = torch.utils.data.DataLoader(\n",
" torchvision.datasets.MNIST('/files/', train=True, download=True,\n", " torchvision.datasets.MNIST(myDir, train=True, download=True,\n",
" transform=torchvision.transforms.Compose([\n", " transform=torchvision.transforms.Compose([\n",
" torchvision.transforms.ToTensor(),\n", " torchvision.transforms.ToTensor(),\n",
" torchvision.transforms.Normalize(\n", " torchvision.transforms.Normalize(\n",
@@ -77,7 +86,7 @@
" batch_size=batch_size_train, shuffle=True)\n", " batch_size=batch_size_train, shuffle=True)\n",
"\n", "\n",
"test_loader = torch.utils.data.DataLoader(\n", "test_loader = torch.utils.data.DataLoader(\n",
" torchvision.datasets.MNIST('/files/', train=False, download=True,\n", " torchvision.datasets.MNIST(myDir, train=False, download=True,\n",
" transform=torchvision.transforms.Compose([\n", " transform=torchvision.transforms.Compose([\n",
" torchvision.transforms.ToTensor(),\n", " torchvision.transforms.ToTensor(),\n",
" torchvision.transforms.Normalize(\n", " torchvision.transforms.Normalize(\n",

View File

@@ -109,7 +109,7 @@
"# Choose random values for the parameters\n", "# Choose random values for the parameters\n",
"omega = np.random.normal(size=(D,D))\n", "omega = np.random.normal(size=(D,D))\n",
"beta = np.random.normal(size=(D,1))\n", "beta = np.random.normal(size=(D,1))\n",
"phi = np.random.normal(size=(1,2*D))" "phi = np.random.normal(size=(2*D,1))"
], ],
"metadata": { "metadata": {
"id": "79TSK7oLMobe" "id": "79TSK7oLMobe"
@@ -210,4 +210,4 @@
} }
} }
] ]
} }

View File

@@ -0,0 +1,51 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyM1pe3HkxLrjbeKezq1MlM5",
"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": [
"<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Trees/LinearRegression_LeastSquares.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Least Squares Loss"
],
"metadata": {
"id": "uORlKyPv02ge"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "bbF6SE_F0tU8"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
}
]
}

View File

@@ -0,0 +1,51 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMIJ9DpOBppPZXAJ5wms6s8",
"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": [
"<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Trees/LinearRegression_LossFunction.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Loss function"
],
"metadata": {
"id": "uORlKyPv02ge"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "bbF6SE_F0tU8"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
}
]
}

Binary file not shown.

Binary file not shown.