Compare commits
73 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b41646bf3 | ||
|
|
16afbcdf83 | ||
|
|
b0add1f8e2 | ||
|
|
03ebe5a039 | ||
|
|
41e8262f20 | ||
|
|
2c6e1cb9f8 | ||
|
|
6c99c6b7eb | ||
|
|
0988ae8bd0 | ||
|
|
2cca6dec75 | ||
|
|
49d74b66a9 | ||
|
|
13c0ad30fe | ||
|
|
95549683c4 | ||
|
|
9649ce382b | ||
|
|
666cbb02d5 | ||
|
|
f0337130cb | ||
|
|
472571aef0 | ||
|
|
13b39c2f72 | ||
|
|
84a11d68ed | ||
|
|
653d2f7b84 | ||
|
|
a7ed3e2c34 | ||
|
|
40a2c3ca8b | ||
|
|
fb66cd682d | ||
|
|
88e8526fa7 | ||
|
|
667346fbdd | ||
|
|
4e564088a1 | ||
|
|
f1c07f53bf | ||
|
|
623b9782e7 | ||
|
|
60c5a48477 | ||
|
|
b4688bda68 | ||
|
|
faf34e0887 | ||
|
|
8f2ef53eab | ||
|
|
2f0339341c | ||
|
|
f8acbaab82 | ||
|
|
2aaaef0838 | ||
|
|
9a2039d392 | ||
|
|
6d76e47849 | ||
|
|
b5c65665b6 | ||
|
|
dd9a56d96b | ||
|
|
9b71ac0487 | ||
|
|
eaff933ff7 | ||
|
|
c3dfe95700 | ||
|
|
7082ae8620 | ||
|
|
6072ad4450 | ||
|
|
33197fde36 | ||
|
|
6d425c04d4 | ||
|
|
57c95132d3 | ||
|
|
2b0ac95740 | ||
|
|
d5f198f2d8 | ||
|
|
4edd8c923d | ||
|
|
1adb96e006 | ||
|
|
3801b8d52d | ||
|
|
dc6b346bda | ||
|
|
5eb264540d | ||
|
|
7ba844f2b5 | ||
|
|
be86733a93 | ||
|
|
d101aa428b | ||
|
|
8c6e40daee | ||
|
|
efafb942eb | ||
|
|
b10a2b6940 | ||
|
|
ede7247a0c | ||
|
|
c3b97af456 | ||
|
|
e1df2156a3 | ||
|
|
f887835646 | ||
|
|
e9c8d846f2 | ||
|
|
b7869e8b41 | ||
|
|
747ec9efe1 | ||
|
|
58dfb0390c | ||
|
|
3aeb8db4cd | ||
|
|
305a055079 | ||
|
|
87cf590af9 | ||
|
|
ccedbb72e7 | ||
|
|
b423a67855 | ||
|
|
3c8dab14e6 |
432
Blogs/BorealisODENumerical.ipynb
Normal file
432
Blogs/BorealisODENumerical.ipynb
Normal file
@@ -0,0 +1,432 @@
|
|||||||
|
{
|
||||||
|
"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/Blogs/BorealisODENumerical.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "JXsO7ce7oqeq"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Numerical methods for ODEs\n",
|
||||||
|
"\n",
|
||||||
|
"This blog contains code that accompanies the RBC Borealis blog on numerical methods for ODEs. Contact udlbookmail@gmail.com if you find any problems."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "AnvAKtP_oqes"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Import relevant libraries"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "UF-gJyZggyrl"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import matplotlib.pyplot as plt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "szWLVrSSoqet"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Define the ODE that we will be experimenting with."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "NkrGZLL6iM3P"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# The ODE that we will experiment with\n",
|
||||||
|
"def ode_lin_homog(t,x):\n",
|
||||||
|
" return 0.5 * x ;\n",
|
||||||
|
"\n",
|
||||||
|
"# The derivative of the ODE function with respect to x (needed for Taylor's method)\n",
|
||||||
|
"def ode_lin_homog_deriv_x(t,x):\n",
|
||||||
|
" return 0.5 ;\n",
|
||||||
|
"\n",
|
||||||
|
"# The derivative of the ODE function with respect to t (needed for Taylor's method)\n",
|
||||||
|
"def ode_lin_homog_deriv_t(t,x):\n",
|
||||||
|
" return 0.0 ;\n",
|
||||||
|
"\n",
|
||||||
|
"# The closed form solution (so we can measure the error)\n",
|
||||||
|
"def ode_lin_homog_soln(t,C=0.5):\n",
|
||||||
|
" return C * np.exp(0.5 * t) ;"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "In1C9wZkoqet"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"This is a generic method that runs the numerical methods. It takes the initial conditions ($t_0$, $x_0$), the final time $t_1$ and the step size $h$. It also takes the ODE function itself and its derivatives (only used for Taylor's method). Finally, the parameter \"step_function\" is the method used to update (e.g., Euler's methods, Runge-Kutte 4-step)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "VZfZDJAfmyrf"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def run_numerical(x_0, t_0, t_1, h, ode_func, ode_func_deriv_x, ode_func_deriv_t, ode_soln, step_function):\n",
|
||||||
|
" x = [x_0]\n",
|
||||||
|
" t = [t_0]\n",
|
||||||
|
" while (t[-1] <= t_1):\n",
|
||||||
|
" x = x+[step_function(x[-1],t[-1],h, ode_func, ode_func_deriv_x, ode_func_deriv_t)]\n",
|
||||||
|
" t = t + [t[-1]+h]\n",
|
||||||
|
"\n",
|
||||||
|
" # Returns x,y plot plus total numerical error at last point.\n",
|
||||||
|
" return t, x, np.abs(ode_soln(t[-1])-x[-1])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "Vfkc3-_7oqet"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Run the numerical method with step sizes of 2.0, 1.0, 0.5, 0.25, 0.125, 0.0675 and plot the results"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "1tyGbMZhoqeu"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def run_and_plot(ode, ode_deriv_x, ode_deriv_t, ode_solution, step_function):\n",
|
||||||
|
" # Specify the grid of points to draw the ODE\n",
|
||||||
|
" t = np.arange(0.04, 4.0, 0.2)\n",
|
||||||
|
" x = np.arange(0.04, 4.0, 0.2)\n",
|
||||||
|
" T, X = np.meshgrid(t,x)\n",
|
||||||
|
"\n",
|
||||||
|
" # ODE equation at these grid points (used to draw quiver-plot)\n",
|
||||||
|
" dx = ode(T,X)\n",
|
||||||
|
" dt = np.ones(dx.shape)\n",
|
||||||
|
"\n",
|
||||||
|
" # The ground truth solution\n",
|
||||||
|
" t2= np.arange(0,10,0.1)\n",
|
||||||
|
" x2 = ode_solution(t2)\n",
|
||||||
|
"\n",
|
||||||
|
" #####################################x_0, t_0, t_1, h #################################################\n",
|
||||||
|
" t_sim1,x_sim1,error1 = run_numerical(0.5, 0.0, 4.0, 2.0000, ode, ode_deriv_x, ode_deriv_t, ode_solution, step_function)\n",
|
||||||
|
" t_sim2,x_sim2,error2 = run_numerical(0.5, 0.0, 4.0, 1.0000, ode, ode_deriv_x, ode_deriv_t, ode_solution, step_function)\n",
|
||||||
|
" t_sim3,x_sim3,error3 = run_numerical(0.5, 0.0, 4.0, 0.5000, ode, ode_deriv_x, ode_deriv_t, ode_solution, step_function)\n",
|
||||||
|
" t_sim4,x_sim4,error4 = run_numerical(0.5, 0.0, 4.0, 0.2500, ode, ode_deriv_x, ode_deriv_t, ode_solution, step_function)\n",
|
||||||
|
" t_sim5,x_sim5,error5 = run_numerical(0.5, 0.0, 4.0, 0.1250, ode, ode_deriv_x, ode_deriv_t, ode_solution, step_function)\n",
|
||||||
|
" t_sim6,x_sim6,error6 = run_numerical(0.5, 0.0, 4.0, 0.0675, ode, ode_deriv_x, ode_deriv_t, ode_solution, step_function)\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot the ODE and ground truth solution\n",
|
||||||
|
" fig,ax = plt.subplots()\n",
|
||||||
|
" ax.quiver(T,X,dt,dx, scale=35.0)\n",
|
||||||
|
" ax.plot(t2,x2,'r-')\n",
|
||||||
|
"\n",
|
||||||
|
" # Plot the numerical approximations\n",
|
||||||
|
" ax.plot(t_sim1,x_sim1,'.-',markeredgecolor='#773c23ff',markerfacecolor='#d18362', color='#d18362', markersize=10)\n",
|
||||||
|
" ax.plot(t_sim2,x_sim2,'.-',markeredgecolor='#773c23ff',markerfacecolor='#d18362', color='#d18362', markersize=10)\n",
|
||||||
|
" ax.plot(t_sim3,x_sim3,'.-',markeredgecolor='#773c23ff',markerfacecolor='#d18362', color='#d18362', markersize=10)\n",
|
||||||
|
" ax.plot(t_sim4,x_sim4,'.-',markeredgecolor='#773c23ff',markerfacecolor='#d18362', color='#d18362', markersize=10)\n",
|
||||||
|
" ax.plot(t_sim5,x_sim5,'.-',markeredgecolor='#773c23ff',markerfacecolor='#d18362', color='#d18362', markersize=10)\n",
|
||||||
|
" ax.plot(t_sim6,x_sim6,'.-',markeredgecolor='#773c23ff',markerfacecolor='#d18362', color='#d18362', markersize=10)\n",
|
||||||
|
"\n",
|
||||||
|
" ax.set_aspect('equal')\n",
|
||||||
|
" ax.set_xlim(0,4)\n",
|
||||||
|
" ax.set_ylim(0,4)\n",
|
||||||
|
"\n",
|
||||||
|
" plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "JYrq8QIwvOIy"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Euler Method\n",
|
||||||
|
"\n",
|
||||||
|
"Define the Euler method and set up functions for plotting."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "N73xMnCukVVX"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def euler_step(x_0, t_0, h, ode_func, ode_func_deriv_x=None, ode_func_deriv_t=None):\n",
|
||||||
|
" return x_0 + h * ode_func(t_0, x_0) ;"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "4B1_PGEcsZ9H"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"run_and_plot(ode_lin_homog, None, None, ode_lin_homog_soln, euler_step)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "FfwNihtkvJeX"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Heun's Method"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "srHfNDcDxI1o"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def heun_step(x_0, t_0, h, ode_func, ode_func_deriv_x=None, ode_func_deriv_t=None):\n",
|
||||||
|
" f_x0_t0 = ode_func(t_0, x_0)\n",
|
||||||
|
" return x_0 + h/2 * ( f_x0_t0 + ode_func(t_0+h, x_0+h*f_x0_t0)) ;"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "WOApHz9xoqev"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"run_and_plot(ode_lin_homog, None, None, ode_lin_homog_soln, heun_step)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "0XSzzFDIvRhm"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Modified Euler method"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "fSXprgVJ5Yep"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def modified_euler_step(x_0, t_0, h, ode_func, ode_func_deriv_x=None, ode_func_deriv_t=None):\n",
|
||||||
|
" f_x0_t0 = ode_func(t_0, x_0)\n",
|
||||||
|
" return x_0 + h * ode_func(t_0+h/2, x_0+ h * f_x0_t0/2) ;"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "8LKSrCD2oqev"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"run_and_plot(ode_lin_homog, None, None, ode_lin_homog_soln, modified_euler_step)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "yp8ZBpwooqev"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Second order Taylor's method"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "NtBBgzWLoqev"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def taylor_2nd_order(x_0, t_0, h, ode_func, ode_func_deriv_x, ode_func_deriv_t):\n",
|
||||||
|
" f1 = ode_func(t_0, x_0)\n",
|
||||||
|
" return x_0 + h * ode_func(t_0, x_0) + (h*h/2) * (ode_func_deriv_x(t_0,x_0) * ode_func(t_0, x_0) + ode_func_deriv_t(t_0, x_0))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "ioeeIohUoqev"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"run_and_plot(ode_lin_homog, ode_lin_homog_deriv_x, ode_lin_homog_deriv_t, ode_lin_homog_soln, taylor_2nd_order)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "WcuhV5lL1zAJ"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Fourth Order Runge Kutta"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "0NZN81Bpwu56"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def runge_kutta_4_step(x_0, t_0, h, ode_func, ode_func_deriv_x=None, ode_func_deriv_t=None):\n",
|
||||||
|
" f1 = ode_func(t_0, x_0)\n",
|
||||||
|
" f2 = ode_func(t_0+h/2,x_0+f1 * h/2)\n",
|
||||||
|
" f3 = ode_func(t_0+h/2,x_0+f2 * h/2)\n",
|
||||||
|
" f4 = ode_func(t_0+h, x_0+ f3*h)\n",
|
||||||
|
" return x_0 + (h/6) * (f1 + 2*f2 + 2*f3+f4)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "K-OxE9E6oqew"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"run_and_plot(ode_lin_homog, None, None, ode_lin_homog_soln, runge_kutta_4_step)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "7JifxBhhoqew"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"# Plot the error as a function of step size"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "ZoEpmlCfsi9P"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Run systematically with a number of different step sizes and store errors for each\n",
|
||||||
|
"def get_errors(ode, ode_deriv_x, ode_deriv_t, ode_solution, step_function):\n",
|
||||||
|
" # Choose the step size h to divide the plotting interval into 1,2,4,8... segments.\n",
|
||||||
|
" # The plots in the article add a few more smaller step sizes, but this takes a while to compute.\n",
|
||||||
|
" # Add them back in if you want the full plot.\n",
|
||||||
|
" all_h = (1./np.array([1,2,4,8,16,32,64,128,256,512,1024,2048,4096])).tolist()\n",
|
||||||
|
" all_err = []\n",
|
||||||
|
"\n",
|
||||||
|
" for i in range(len(all_h)):\n",
|
||||||
|
" t_sim,x_sim,err = run_numerical(0.5, 0.0, 4.0, all_h[i], ode, ode_deriv_x, ode_deriv_t, ode_solution, step_function)\n",
|
||||||
|
" all_err = all_err + [err]\n",
|
||||||
|
"\n",
|
||||||
|
" return all_h, all_err"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "X0O0KK47xF28"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Plot the errors\n",
|
||||||
|
"all_h, all_err_euler = get_errors(ode_lin_homog, ode_lin_homog_deriv_x, ode_lin_homog_deriv_t, ode_lin_homog_soln, euler_step)\n",
|
||||||
|
"all_h, all_err_heun = get_errors(ode_lin_homog, ode_lin_homog_deriv_x, ode_lin_homog_deriv_t, ode_lin_homog_soln, heun_step)\n",
|
||||||
|
"all_h, all_err_mod_euler = get_errors(ode_lin_homog, ode_lin_homog_deriv_x, ode_lin_homog_deriv_t, ode_lin_homog_soln, modified_euler_step)\n",
|
||||||
|
"all_h, all_err_taylor = get_errors(ode_lin_homog, ode_lin_homog_deriv_x, ode_lin_homog_deriv_t, ode_lin_homog_soln, taylor_2nd_order)\n",
|
||||||
|
"all_h, all_err_rk = get_errors(ode_lin_homog, ode_lin_homog_deriv_x, ode_lin_homog_deriv_t, ode_lin_homog_soln, runge_kutta_4_step)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"fig, ax = plt.subplots()\n",
|
||||||
|
"ax.loglog(all_h, all_err_euler,'ro-')\n",
|
||||||
|
"ax.loglog(all_h, all_err_heun,'bo-')\n",
|
||||||
|
"ax.loglog(all_h, all_err_mod_euler,'go-')\n",
|
||||||
|
"ax.loglog(all_h, all_err_taylor,'co-')\n",
|
||||||
|
"ax.loglog(all_h, all_err_rk,'mo-')\n",
|
||||||
|
"ax.set_ylim(1e-13,1e1)\n",
|
||||||
|
"ax.set_xlim(1e-6,1e1)\n",
|
||||||
|
"ax.set_aspect(0.5)\n",
|
||||||
|
"ax.set_xlabel('Step size, $h$')\n",
|
||||||
|
"ax.set_ylabel('Error')\n",
|
||||||
|
"plt.show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "BttOqpeo9MsJ"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Note that for this ODE, the Heun, Modified Euler and Taylor methods provide EXACTLY the same updates, and so the error curves for all three are identical (subject to difference is numerical rounding errors). This is not in general the case, although the general trend would be the same for each."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"colab": {
|
||||||
|
"provenance": [],
|
||||||
|
"include_colab_link": true
|
||||||
|
},
|
||||||
|
"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.9.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"# **Notebook 1.1 -- Background Mathematics**\n",
|
"# **Notebook 1.1 -- Background Mathematics**\n",
|
||||||
"\n",
|
"\n",
|
||||||
"The purpose of this Python notebook is to make sure you can use CoLab and to familiarize yourself with some of the background mathematical concepts that you are going to need to understand deep learning. <br><br> It's not meant to be difficult and it may be that you know some or all of this information already.<br><br> Math is *NOT* a spectator sport. You won't learn it by just listening to lectures or reading books. It really helps to interact with it and explore yourself. <br><br> 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",
|
"The purpose of this Python notebook is to make sure you can use CoLab and to familiarize yourself with some of the background mathematical concepts that you are going to need to understand deep learning. <br><br> It's not meant to be difficult and it may be that you know some or all of this information already.<br><br> Math is *NOT* a spectator sport. You won't learn it by just listening to lectures or reading books. It really helps to interact with it and explore yourself. <br><br> 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",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"The purpose of this notebook is to explore the linear regression model discussed in Chapter 2 of the book.\n",
|
"The purpose of this notebook is to explore the linear regression model discussed in Chapter 2 of the book.\n",
|
||||||
"\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",
|
"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",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
@@ -174,7 +174,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"source": [
|
||||||
"# TO DO -- Change the parameters manually to fit the model\n",
|
"# TODO -- Change the parameters manually to fit the model\n",
|
||||||
"# First fix phi1 and try changing phi0 until you can't make the loss go down any more\n",
|
"# First fix phi1 and try changing phi0 until you can't make the loss go down any more\n",
|
||||||
"# Then fix phi0 and try changing phi1 until you can't make the loss go down any more\n",
|
"# Then fix phi0 and try changing phi1 until you can't make the loss go down any more\n",
|
||||||
"# Repeat this process until you find a set of parameters that fit the model as in figure 2.2d\n",
|
"# Repeat this process until you find a set of parameters that fit the model as in figure 2.2d\n",
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"The purpose of this notebook is to gain some familiarity with shallow neural networks with 1D inputs. It works through an example similar to figure 3.3 and experiments with different activation functions. <br>\n",
|
"The purpose of this notebook is to gain some familiarity with shallow neural networks with 1D inputs. It works through an example similar to figure 3.3 and experiments with different activation functions. <br>\n",
|
||||||
"\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",
|
"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",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"The purpose of this notebook is to gain some familiarity with shallow neural networks with 2D inputs. It works through an example similar to figure 3.8 and experiments with different activation functions. <br><br>\n",
|
"The purpose of this notebook is to gain some familiarity with shallow neural networks with 2D inputs. It works through an example similar to figure 3.8 and experiments with different activation functions. <br><br>\n",
|
||||||
"\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",
|
"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",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
"metadata": {
|
"metadata": {
|
||||||
"colab": {
|
"colab": {
|
||||||
"provenance": [],
|
"provenance": [],
|
||||||
"authorship_tag": "ABX9TyNioITtfAcfxEfM3UOfQyb9",
|
|
||||||
"include_colab_link": true
|
"include_colab_link": true
|
||||||
},
|
},
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
@@ -33,7 +32,7 @@
|
|||||||
"\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",
|
"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",
|
"\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",
|
"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",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
@@ -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",
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"The purpose of this practical is to experiment with different activation functions. <br>\n",
|
"The purpose of this practical is to experiment with different activation functions. <br>\n",
|
||||||
"\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",
|
"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",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"The purpose of this notebook is to understand what happens when we feed one neural network into another. It works through an example similar to 4.1 and varies both networks\n",
|
"The purpose of this notebook is to understand what happens when we feed one neural network into another. It works through an example similar to 4.1 and varies both networks\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions"
|
||||||
],
|
],
|
||||||
@@ -343,7 +343,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"source": [
|
||||||
"# TO DO\n",
|
"# TODO\n",
|
||||||
"# How many linear regions would there be if we ran N copies of the first network, feeding the result of the first\n",
|
"# How many linear regions would there be if we ran N copies of the first network, feeding the result of the first\n",
|
||||||
"# into the second, the second into the third and so on, and then passed the result into the original second\n",
|
"# into the second, the second into the third and so on, and then passed the result into the original second\n",
|
||||||
"# network (blue curve above)\n",
|
"# network (blue curve above)\n",
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"The purpose of this notebook is to understand how a neural network with two hidden layers build more complicated functions by clipping and recombining the representations at the intermediate hidden variables.\n",
|
"The purpose of this notebook is to understand how a neural network with two hidden layers build more complicated functions by clipping and recombining the representations at the intermediate hidden variables.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions"
|
||||||
],
|
],
|
||||||
@@ -169,7 +169,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"source": [
|
||||||
"# Define parameters (note first dimension of theta and phi is padded to make indices match\n",
|
"# Define parameters (note first dimension of theta and psi is padded to make indices match\n",
|
||||||
"# notation in book)\n",
|
"# notation in book)\n",
|
||||||
"theta = np.zeros([4,2])\n",
|
"theta = np.zeros([4,2])\n",
|
||||||
"psi = np.zeros([4,4])\n",
|
"psi = np.zeros([4,4])\n",
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
"metadata": {
|
"metadata": {
|
||||||
"colab": {
|
"colab": {
|
||||||
"provenance": [],
|
"provenance": [],
|
||||||
"authorship_tag": "ABX9TyO2DaD75p+LGi7WgvTzjrk1",
|
|
||||||
"include_colab_link": true
|
"include_colab_link": true
|
||||||
},
|
},
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
@@ -31,9 +30,9 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# **Notebook 4.3 Deep neural networks**\n",
|
"# **Notebook 4.3 Deep neural networks**\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This network investigates converting neural networks to matrix form.\n",
|
"This notebook investigates converting neural networks to matrix form.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
@@ -150,7 +149,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"source": [
|
"source": [
|
||||||
"Now we'll define the same neural network, but this time, we will use matrix form. When you get this right, it will draw the same plot as above."
|
"Now we'll define the same neural network, but this time, we will use matrix form as in equation 4.15. When you get this right, it will draw the same plot as above."
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "XCJqo_AjfAra"
|
"id": "XCJqo_AjfAra"
|
||||||
@@ -176,8 +175,8 @@
|
|||||||
"n1_in_mat = np.reshape(n1_in,(n_dim_in,n_data))\n",
|
"n1_in_mat = np.reshape(n1_in,(n_dim_in,n_data))\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# This runs the network for ALL of the inputs, x at once so we can draw graph\n",
|
"# This runs the network for ALL of the inputs, x at once so we can draw graph\n",
|
||||||
"h1 = ReLU(np.matmul(beta_0,np.ones((1,n_data))) + np.matmul(Omega_0,n1_in_mat))\n",
|
"h1 = ReLU(beta_0 + np.matmul(Omega_0,n1_in_mat))\n",
|
||||||
"n1_out = np.matmul(beta_1,np.ones((1,n_data))) + np.matmul(Omega_1,h1)\n",
|
"n1_out = beta_1 + np.matmul(Omega_1,h1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Draw the network and check that it looks the same as the non-matrix case\n",
|
"# Draw the network and check that it looks the same as the non-matrix case\n",
|
||||||
"plot_neural(n1_in, n1_out)"
|
"plot_neural(n1_in, n1_out)"
|
||||||
@@ -247,9 +246,9 @@
|
|||||||
"n1_in_mat = np.reshape(n1_in,(n_dim_in,n_data))\n",
|
"n1_in_mat = np.reshape(n1_in,(n_dim_in,n_data))\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# This runs the network for ALL of the inputs, x at once so we can draw graph (hence extra np.ones term)\n",
|
"# This runs the network for ALL of the inputs, x at once so we can draw graph (hence extra np.ones term)\n",
|
||||||
"h1 = ReLU(np.matmul(beta_0,np.ones((1,n_data))) + np.matmul(Omega_0,n1_in_mat))\n",
|
"h1 = ReLU(beta_0 + np.matmul(Omega_0,n1_in_mat))\n",
|
||||||
"h2 = ReLU(np.matmul(beta_1,np.ones((1,n_data))) + np.matmul(Omega_1,h1))\n",
|
"h2 = ReLU(beta_1 + np.matmul(Omega_1,h1))\n",
|
||||||
"n1_out = np.matmul(beta_2,np.ones((1,n_data))) + np.matmul(Omega_2,h2)\n",
|
"n1_out = beta_2 + np.matmul(Omega_2,h2)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Draw the network and check that it looks the same as the non-matrix version\n",
|
"# Draw the network and check that it looks the same as the non-matrix version\n",
|
||||||
"plot_neural(n1_in, n1_out)"
|
"plot_neural(n1_in, n1_out)"
|
||||||
@@ -291,10 +290,10 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# If you set the parameters to the correct sizes, the following code will run\n",
|
"# If you set the parameters to the correct sizes, the following code will run\n",
|
||||||
"h1 = ReLU(np.matmul(beta_0,np.ones((1,n_data))) + np.matmul(Omega_0,x));\n",
|
"h1 = ReLU(beta_0 + np.matmul(Omega_0,x));\n",
|
||||||
"h2 = ReLU(np.matmul(beta_1,np.ones((1,n_data))) + np.matmul(Omega_1,h1));\n",
|
"h2 = ReLU(beta_1 + np.matmul(Omega_1,h1));\n",
|
||||||
"h3 = ReLU(np.matmul(beta_2,np.ones((1,n_data))) + np.matmul(Omega_2,h2));\n",
|
"h3 = ReLU(beta_2 + np.matmul(Omega_2,h2));\n",
|
||||||
"y = np.matmul(beta_3,np.ones((1,n_data))) + np.matmul(Omega_3,h3)\n",
|
"y = beta_3 + np.matmul(Omega_3,h3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if h1.shape[0] is not D_1 or h1.shape[1] is not n_data:\n",
|
"if h1.shape[0] is not D_1 or h1.shape[1] is not n_data:\n",
|
||||||
" print(\"h1 is wrong shape\")\n",
|
" print(\"h1 is wrong shape\")\n",
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the least squares loss and the equivalence of maximum likelihood and minimum negative log likelihood.\n",
|
"This notebook investigates the least squares loss and the equivalence of maximum likelihood and minimum negative log likelihood.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the binary cross-entropy loss. It follows from applying the formula in section 5.2 to a loss function based on the Bernoulli distribution.\n",
|
"This notebook investigates the binary cross-entropy loss. It follows from applying the formula in section 5.2 to a loss function based on the Bernoulli distribution.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the multi-class cross-entropy loss. It follows from applying the formula in section 5.2 to a loss function based on the Categorical distribution.\n",
|
"This notebook investigates the multi-class cross-entropy loss. It follows from applying the formula in section 5.2 to a loss function based on the Categorical distribution.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
@@ -236,11 +236,10 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Let's double check we get the right answer before proceeding\n",
|
"# Here are three examples\n",
|
||||||
"print(\"Correct answer = %3.3f, Your answer = %3.3f\"%(0.2,categorical_distribution(np.array([[0]]),np.array([[0.2],[0.5],[0.3]]))))\n",
|
"print(categorical_distribution(np.array([[0]]),np.array([[0.2],[0.5],[0.3]])))\n",
|
||||||
"print(\"Correct answer = %3.3f, Your answer = %3.3f\"%(0.5,categorical_distribution(np.array([[1]]),np.array([[0.2],[0.5],[0.3]]))))\n",
|
"print(categorical_distribution(np.array([[1]]),np.array([[0.2],[0.5],[0.3]])))\n",
|
||||||
"print(\"Correct answer = %3.3f, Your answer = %3.3f\"%(0.3,categorical_distribution(np.array([[2]]),np.array([[0.2],[0.5],[0.3]]))))\n",
|
"print(categorical_distribution(np.array([[2]]),np.array([[0.2],[0.5],[0.3]])))"
|
||||||
"\n"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates how to find the minimum of a 1D function using line search as described in Figure 6.10.\n",
|
"This notebook investigates how to find the minimum of a 1D function using line search as described in Figure 6.10.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
@@ -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",
|
||||||
|
|||||||
@@ -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"
|
||||||
@@ -22,7 +20,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook recreates the gradient descent algorithm as shown in figure 6.1.\n",
|
"This notebook recreates the gradient descent algorithm as shown in figure 6.1.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
@@ -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",
|
||||||
|
|||||||
@@ -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"
|
||||||
@@ -22,7 +20,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates gradient descent and stochastic gradient descent and recreates figure 6.5 from the book\n",
|
"This notebook investigates gradient descent and stochastic gradient descent and recreates figure 6.5 from the book\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -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",
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the use of momentum as illustrated in figure 6.7 from the book.\n",
|
"This notebook investigates the use of momentum as illustrated in figure 6.7 from the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the Adam algorithm as illustrated in figure 6.9 from the book.\n",
|
"This notebook investigates the Adam algorithm as illustrated in figure 6.9 from the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
@@ -185,11 +185,11 @@
|
|||||||
" for c_step in range(n_steps):\n",
|
" for c_step in range(n_steps):\n",
|
||||||
" # Measure the gradient as in equation 6.13 (first line)\n",
|
" # Measure the gradient as in equation 6.13 (first line)\n",
|
||||||
" m = get_loss_gradient(grad_path[0,c_step], grad_path[1,c_step]);\n",
|
" m = get_loss_gradient(grad_path[0,c_step], grad_path[1,c_step]);\n",
|
||||||
" # TO DO -- compute the squared gradient as in equation 6.13 (second line)\n",
|
" # TODO -- compute the squared gradient as in equation 6.13 (second line)\n",
|
||||||
" # Replace this line:\n",
|
" # Replace this line:\n",
|
||||||
" v = np.ones_like(grad_path[:,0])\n",
|
" v = np.ones_like(grad_path[:,0])\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # TO DO -- apply the update rule (equation 6.14)\n",
|
" # TODO -- apply the update rule (equation 6.14)\n",
|
||||||
" # Replace this line:\n",
|
" # Replace this line:\n",
|
||||||
" grad_path[:,c_step+1] = grad_path[:,c_step]\n",
|
" grad_path[:,c_step+1] = grad_path[:,c_step]\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -254,7 +254,7 @@
|
|||||||
" v_tilde = v\n",
|
" v_tilde = v\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # TO DO -- apply the update rule (equation 6.17)\n",
|
" # TODO -- apply the update rule (equation 6.17)\n",
|
||||||
" # Replace this line:\n",
|
" # Replace this line:\n",
|
||||||
" grad_path[:,c_step+1] = grad_path[:,c_step]\n",
|
" grad_path[:,c_step+1] = grad_path[:,c_step]\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook computes the derivatives of the toy function discussed in section 7.3 of the book.\n",
|
"This notebook computes the derivatives of the toy function discussed in section 7.3 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
"metadata": {
|
"metadata": {
|
||||||
"colab": {
|
"colab": {
|
||||||
"provenance": [],
|
"provenance": [],
|
||||||
"authorship_tag": "ABX9TyM2kkHLr00J4Jeypw41sTkQ",
|
|
||||||
"include_colab_link": true
|
"include_colab_link": true
|
||||||
},
|
},
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
@@ -33,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook runs the backpropagation algorithm on a deep neural network as described in section 7.4 of the book.\n",
|
"This notebook runs the backpropagation algorithm on a deep neural network as described in section 7.4 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
@@ -68,7 +67,7 @@
|
|||||||
"# Set seed so we always get the same random numbers\n",
|
"# Set seed so we always get the same random numbers\n",
|
||||||
"np.random.seed(0)\n",
|
"np.random.seed(0)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Number of layers\n",
|
"# Number of hidden layers\n",
|
||||||
"K = 5\n",
|
"K = 5\n",
|
||||||
"# Number of neurons per layer\n",
|
"# Number of neurons per layer\n",
|
||||||
"D = 6\n",
|
"D = 6\n",
|
||||||
@@ -115,7 +114,7 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"source": [
|
"source": [
|
||||||
"Now let's run our random network. The weight matrices $\\boldsymbol\\Omega_{1\\ldots K}$ are the entries of the list \"all_weights\" and the biases $\\boldsymbol\\beta_{1\\ldots K}$ are the entries of the list \"all_biases\"\n",
|
"Now let's run our random network. The weight matrices $\\boldsymbol\\Omega_{0\\ldots K}$ are the entries of the list \"all_weights\" and the biases $\\boldsymbol\\beta_{0\\ldots K}$ are the entries of the list \"all_biases\"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"We know that we will need the preactivations $\\mathbf{f}_{0\\ldots K}$ and the activations $\\mathbf{h}_{1\\ldots K}$ for the forward pass of backpropagation, so we'll store and return these as well.\n"
|
"We know that we will need the preactivations $\\mathbf{f}_{0\\ldots K}$ and the activations $\\mathbf{h}_{1\\ldots K}$ for the forward pass of backpropagation, so we'll store and return these as well.\n"
|
||||||
],
|
],
|
||||||
@@ -142,14 +141,14 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" # Run through the layers, calculating all_f[0...K-1] and all_h[1...K]\n",
|
" # Run through the layers, calculating all_f[0...K-1] and all_h[1...K]\n",
|
||||||
" for layer in range(K):\n",
|
" for layer in range(K):\n",
|
||||||
" # Update preactivations and activations at this layer according to eqn 7.16\n",
|
" # Update preactivations and activations at this layer according to eqn 7.17\n",
|
||||||
" # Remember to use np.matmul for matrix multiplications\n",
|
" # Remember to use np.matmul for matrix multiplications\n",
|
||||||
" # TODO -- Replace the lines below\n",
|
" # TODO -- Replace the lines below\n",
|
||||||
" all_f[layer] = all_h[layer]\n",
|
" all_f[layer] = all_h[layer]\n",
|
||||||
" all_h[layer+1] = all_f[layer]\n",
|
" all_h[layer+1] = all_f[layer]\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Compute the output from the last hidden layer\n",
|
" # Compute the output from the last hidden layer\n",
|
||||||
" # TO DO -- Replace the line below\n",
|
" # TODO -- Replace the line below\n",
|
||||||
" all_f[K] = np.zeros_like(all_biases[-1])\n",
|
" all_f[K] = np.zeros_like(all_biases[-1])\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Retrieve the output\n",
|
" # Retrieve the output\n",
|
||||||
@@ -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",
|
||||||
@@ -249,23 +248,23 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" # Now work backwards through the network\n",
|
" # Now work backwards through the network\n",
|
||||||
" for layer in range(K,-1,-1):\n",
|
" for layer in range(K,-1,-1):\n",
|
||||||
" # TODO Calculate the derivatives of the loss with respect to the biases at layer from all_dl_df[layer]. (eq 7.21)\n",
|
" # TODO Calculate the derivatives of the loss with respect to the biases at layer from all_dl_df[layer]. (eq 7.22)\n",
|
||||||
" # NOTE! To take a copy of matrix X, use Z=np.array(X)\n",
|
" # NOTE! To take a copy of matrix X, use Z=np.array(X)\n",
|
||||||
" # REPLACE THIS LINE\n",
|
" # REPLACE THIS LINE\n",
|
||||||
" all_dl_dbiases[layer] = np.zeros_like(all_biases[layer])\n",
|
" all_dl_dbiases[layer] = np.zeros_like(all_biases[layer])\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # TODO Calculate the derivatives of the loss with respect to the weights at layer from all_dl_df[layer] and all_h[layer] (eq 7.22)\n",
|
" # TODO Calculate the derivatives of the loss with respect to the weights at layer from all_dl_df[layer] and all_h[layer] (eq 7.23)\n",
|
||||||
" # Don't forget to use np.matmul\n",
|
" # Don't forget to use np.matmul\n",
|
||||||
" # REPLACE THIS LINE\n",
|
" # REPLACE THIS LINE\n",
|
||||||
" all_dl_dweights[layer] = np.zeros_like(all_weights[layer])\n",
|
" all_dl_dweights[layer] = np.zeros_like(all_weights[layer])\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # TODO: calculate the derivatives of the loss with respect to the activations from weight and derivatives of next preactivations (second part of last line of eq 7.24)\n",
|
" # TODO: calculate the derivatives of the loss with respect to the activations from weight and derivatives of next preactivations (second part of last line of eq 7.25)\n",
|
||||||
" # REPLACE THIS LINE\n",
|
" # REPLACE THIS LINE\n",
|
||||||
" all_dl_dh[layer] = np.zeros_like(all_h[layer])\n",
|
" all_dl_dh[layer] = np.zeros_like(all_h[layer])\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
" if layer > 0:\n",
|
" if layer > 0:\n",
|
||||||
" # TODO Calculate the derivatives of the loss with respect to the pre-activation f (use derivative of ReLu function, first part of last line of eq. 7.24)\n",
|
" # TODO Calculate the derivatives of the loss with respect to the pre-activation f (use derivative of ReLu function, first part of last line of eq. 7.25)\n",
|
||||||
" # REPLACE THIS LINE\n",
|
" # REPLACE THIS LINE\n",
|
||||||
" all_dl_df[layer-1] = np.zeros_like(all_f[layer-1])\n",
|
" all_dl_df[layer-1] = np.zeros_like(all_f[layer-1])\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -300,7 +299,7 @@
|
|||||||
"delta_fd = 0.000001\n",
|
"delta_fd = 0.000001\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Test the dervatives of the bias vectors\n",
|
"# Test the dervatives of the bias vectors\n",
|
||||||
"for layer in range(K):\n",
|
"for layer in range(K+1):\n",
|
||||||
" dl_dbias = np.zeros_like(all_dl_dbiases[layer])\n",
|
" dl_dbias = np.zeros_like(all_dl_dbiases[layer])\n",
|
||||||
" # For every element in the bias\n",
|
" # For every element in the bias\n",
|
||||||
" for row in range(all_biases[layer].shape[0]):\n",
|
" for row in range(all_biases[layer].shape[0]):\n",
|
||||||
@@ -324,7 +323,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Test the derivatives of the weights matrices\n",
|
"# Test the derivatives of the weights matrices\n",
|
||||||
"for layer in range(K):\n",
|
"for layer in range(K+1):\n",
|
||||||
" dl_dweight = np.zeros_like(all_dl_dweights[layer])\n",
|
" dl_dweight = np.zeros_like(all_dl_dweights[layer])\n",
|
||||||
" # For every element in the bias\n",
|
" # For every element in the bias\n",
|
||||||
" for row in range(all_weights[layer].shape[0]):\n",
|
" for row in range(all_weights[layer].shape[0]):\n",
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook explores weight initialization in deep neural networks as described in section 7.5 of the book.\n",
|
"This notebook explores weight initialization in deep neural networks as described in section 7.5 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
@@ -191,10 +191,10 @@
|
|||||||
"# You can see that the values of the hidden units are increasing on average (the variance is across all hidden units at the layer\n",
|
"# You can see that the values of the hidden units are increasing on average (the variance is across all hidden units at the layer\n",
|
||||||
"# and the 1000 training examples\n",
|
"# and the 1000 training examples\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# TO DO\n",
|
"# TODO\n",
|
||||||
"# Change this to 50 layers with 80 hidden units per layer\n",
|
"# Change this to 50 layers with 80 hidden units per layer\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# TO DO\n",
|
"# TODO\n",
|
||||||
"# Now experiment with sigma_sq_omega to try to stop the variance of the forward computation exploding"
|
"# Now experiment with sigma_sq_omega to try to stop the variance of the forward computation exploding"
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@@ -340,10 +340,10 @@
|
|||||||
"# You can see that the gradients of the hidden units are increasing on average (the standard deviation is across all hidden units at the layer\n",
|
"# You can see that the gradients of the hidden units are increasing on average (the standard deviation is across all hidden units at the layer\n",
|
||||||
"# and the 100 training examples\n",
|
"# and the 100 training examples\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# TO DO\n",
|
"# TODO\n",
|
||||||
"# Change this to 50 layers with 80 hidden units per layer\n",
|
"# Change this to 50 layers with 80 hidden units per layer\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# TO DO\n",
|
"# TODO\n",
|
||||||
"# Now experiment with sigma_sq_omega to try to stop the variance of the gradients exploding\n"
|
"# Now experiment with sigma_sq_omega to try to stop the variance of the gradients exploding\n"
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
@@ -1,28 +1,10 @@
|
|||||||
{
|
{
|
||||||
"nbformat": 4,
|
|
||||||
"nbformat_minor": 0,
|
|
||||||
"metadata": {
|
|
||||||
"colab": {
|
|
||||||
"provenance": [],
|
|
||||||
"gpuType": "T4",
|
|
||||||
"authorship_tag": "ABX9TyOuKMUcKfOIhIL2qTX9jJCy",
|
|
||||||
"include_colab_link": true
|
|
||||||
},
|
|
||||||
"kernelspec": {
|
|
||||||
"name": "python3",
|
|
||||||
"display_name": "Python 3"
|
|
||||||
},
|
|
||||||
"language_info": {
|
|
||||||
"name": "python"
|
|
||||||
},
|
|
||||||
"accelerator": "GPU"
|
|
||||||
},
|
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "view-in-github",
|
"colab_type": "text",
|
||||||
"colab_type": "text"
|
"id": "view-in-github"
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Notebooks/Chap08/8_1_MNIST_1D_Performance.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/Chap08/8_1_MNIST_1D_Performance.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
||||||
@@ -30,33 +12,38 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "L6chybAVFJW2"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"# **Notebook 8.1: MNIST_1D_Performance**\n",
|
"# **Notebook 8.1: MNIST_1D_Performance**\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This notebook runs a simple neural network on the MNIST1D dataset as in figure 8.2a. It uses code from https://github.com/greydanus/mnist1d to generate the data.\n",
|
"This notebook runs a simple neural network on the MNIST1D dataset as in figure 8.2a. It uses code from https://github.com/greydanus/mnist1d to generate the data.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "L6chybAVFJW2"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"execution_count": null,
|
||||||
"# Run this if you're in a Colab to install MNIST 1D repository\n",
|
|
||||||
"%pip install git+https://github.com/greydanus/mnist1d"
|
|
||||||
],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "ifVjS4cTOqKz"
|
"id": "ifVjS4cTOqKz"
|
||||||
},
|
},
|
||||||
"execution_count": null,
|
"outputs": [],
|
||||||
"outputs": []
|
"source": [
|
||||||
|
"# Run this if you're in a Colab to install MNIST 1D repository\n",
|
||||||
|
"%pip install git+https://github.com/greydanus/mnist1d"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "qyE7G1StPIqO"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import torch, torch.nn as nn\n",
|
"import torch, torch.nn as nn\n",
|
||||||
"from torch.utils.data import TensorDataset, DataLoader\n",
|
"from torch.utils.data import TensorDataset, DataLoader\n",
|
||||||
@@ -64,49 +51,47 @@
|
|||||||
"import numpy as np\n",
|
"import numpy as np\n",
|
||||||
"import matplotlib.pyplot as plt\n",
|
"import matplotlib.pyplot as plt\n",
|
||||||
"import mnist1d"
|
"import mnist1d"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "qyE7G1StPIqO"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"source": [
|
|
||||||
"Let's generate a training and test dataset using the MNIST1D code. The dataset gets saved as a .pkl file so it doesn't have to be regenerated each time."
|
|
||||||
],
|
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "F7LNq72SP6jO"
|
"id": "F7LNq72SP6jO"
|
||||||
}
|
},
|
||||||
|
"source": [
|
||||||
|
"Let's generate a training and test dataset using the MNIST1D code. The dataset gets saved as a .pkl file so it doesn't have to be regenerated each time."
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "YLxf7dJfPaqw"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"!mkdir ./sample_data\n",
|
|
||||||
"\n",
|
|
||||||
"args = mnist1d.data.get_dataset_args()\n",
|
"args = mnist1d.data.get_dataset_args()\n",
|
||||||
"data = mnist1d.data.get_dataset(args, path='./sample_data/mnist1d_data.pkl', download=False, regenerate=False)\n",
|
"data = mnist1d.data.get_dataset(args, path='./mnist1d_data.pkl', download=False, regenerate=False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# The training and test input and outputs are in\n",
|
"# The training and test input and outputs are in\n",
|
||||||
"# data['x'], data['y'], data['x_test'], and data['y_test']\n",
|
"# data['x'], data['y'], data['x_test'], and data['y_test']\n",
|
||||||
"print(\"Examples in training set: {}\".format(len(data['y'])))\n",
|
"print(\"Examples in training set: {}\".format(len(data['y'])))\n",
|
||||||
"print(\"Examples in test set: {}\".format(len(data['y_test'])))\n",
|
"print(\"Examples in test set: {}\".format(len(data['y_test'])))\n",
|
||||||
"print(\"Length of each example: {}\".format(data['x'].shape[-1]))"
|
"print(\"Length of each example: {}\".format(data['x'].shape[-1]))"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "YLxf7dJfPaqw"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "FxaB5vc0uevl"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"D_i = 40 # Input dimensions\n",
|
"D_i = 40 # Input dimensions\n",
|
||||||
"D_k = 100 # Hidden dimensions\n",
|
"D_k = 100 # Hidden dimensions\n",
|
||||||
"D_o = 10 # Output dimensions\n",
|
"D_o = 10 # Output dimensions\n",
|
||||||
"# TO DO:\n",
|
"# TODO:\n",
|
||||||
"# Define a model with two hidden layers of size 100\n",
|
"# Define a model with two hidden layers of size 100\n",
|
||||||
"# And ReLU activations between them\n",
|
"# And ReLU activations between them\n",
|
||||||
"# Replace this line (see Figure 7.8 of book for help):\n",
|
"# Replace this line (see Figure 7.8 of book for help):\n",
|
||||||
@@ -114,7 +99,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def weights_init(layer_in):\n",
|
"def weights_init(layer_in):\n",
|
||||||
" # TO DO:\n",
|
" # TODO:\n",
|
||||||
" # Initialize the parameters with He initialization\n",
|
" # Initialize the parameters with He initialization\n",
|
||||||
" # Replace this line (see figure 7.8 of book for help)\n",
|
" # Replace this line (see figure 7.8 of book for help)\n",
|
||||||
" print(\"Initializing layer\")\n",
|
" print(\"Initializing layer\")\n",
|
||||||
@@ -122,15 +107,15 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"# Call the function you just defined\n",
|
"# Call the function you just defined\n",
|
||||||
"model.apply(weights_init)\n"
|
"model.apply(weights_init)\n"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "FxaB5vc0uevl"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "_rX6N3VyyQTY"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# choose cross entropy loss function (equation 5.24)\n",
|
"# choose cross entropy loss function (equation 5.24)\n",
|
||||||
"loss_function = torch.nn.CrossEntropyLoss()\n",
|
"loss_function = torch.nn.CrossEntropyLoss()\n",
|
||||||
@@ -139,9 +124,9 @@
|
|||||||
"# object that decreases learning rate by half every 10 epochs\n",
|
"# object that decreases learning rate by half every 10 epochs\n",
|
||||||
"scheduler = StepLR(optimizer, step_size=10, gamma=0.5)\n",
|
"scheduler = StepLR(optimizer, step_size=10, gamma=0.5)\n",
|
||||||
"x_train = torch.tensor(data['x'].astype('float32'))\n",
|
"x_train = torch.tensor(data['x'].astype('float32'))\n",
|
||||||
"y_train = torch.tensor(data['y'].transpose().astype('long'))\n",
|
"y_train = torch.tensor(data['y'].transpose().astype('int64'))\n",
|
||||||
"x_test= torch.tensor(data['x_test'].astype('float32'))\n",
|
"x_test= torch.tensor(data['x_test'].astype('float32'))\n",
|
||||||
"y_test = torch.tensor(data['y_test'].astype('long'))\n",
|
"y_test = torch.tensor(data['y_test'].astype('int64'))\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# load the data into a class that creates the batches\n",
|
"# load the data into a class that creates the batches\n",
|
||||||
"data_loader = DataLoader(TensorDataset(x_train,y_train), batch_size=100, shuffle=True, worker_init_fn=np.random.seed(1))\n",
|
"data_loader = DataLoader(TensorDataset(x_train,y_train), batch_size=100, shuffle=True, worker_init_fn=np.random.seed(1))\n",
|
||||||
@@ -186,15 +171,15 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" # tell scheduler to consider updating learning rate\n",
|
" # tell scheduler to consider updating learning rate\n",
|
||||||
" scheduler.step()"
|
" scheduler.step()"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "_rX6N3VyyQTY"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"id": "yI-l6kA_EH9G"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Plot the results\n",
|
"# Plot the results\n",
|
||||||
"fig, ax = plt.subplots()\n",
|
"fig, ax = plt.subplots()\n",
|
||||||
@@ -215,25 +200,38 @@
|
|||||||
"ax.set_title('Train loss %3.2f, Test loss %3.2f'%(losses_train[-1],losses_test[-1]))\n",
|
"ax.set_title('Train loss %3.2f, Test loss %3.2f'%(losses_train[-1],losses_test[-1]))\n",
|
||||||
"ax.legend()\n",
|
"ax.legend()\n",
|
||||||
"plt.show()"
|
"plt.show()"
|
||||||
],
|
]
|
||||||
"metadata": {
|
|
||||||
"id": "yI-l6kA_EH9G"
|
|
||||||
},
|
|
||||||
"execution_count": null,
|
|
||||||
"outputs": []
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "q-yT6re6GZS4"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"**TO DO**\n",
|
"**TODO**\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Play with the model -- try changing the number of layers, hidden units, learning rate, batch size, momentum or anything else you like. See if you can improve the test results.\n",
|
"Play with the model -- try changing the number of layers, hidden units, learning rate, batch size, momentum or anything else you like. See if you can improve the test results.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Is it a good idea to optimize the hyperparameters in this way? Will the final result be a good estimate of the true test performance?"
|
"Is it a good idea to optimize the hyperparameters in this way? Will the final result be a good estimate of the true test performance?"
|
||||||
|
]
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "q-yT6re6GZS4"
|
"accelerator": "GPU",
|
||||||
|
"colab": {
|
||||||
|
"authorship_tag": "ABX9TyOuKMUcKfOIhIL2qTX9jJCy",
|
||||||
|
"gpuType": "T4",
|
||||||
|
"include_colab_link": true,
|
||||||
|
"provenance": []
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"name": "python"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
]
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the bias-variance trade-off for the toy model used throughout chapter 8 and reproduces the bias/variance trade off curves seen in figure 8.9.\n",
|
"This notebook investigates the bias-variance trade-off for the toy model used throughout chapter 8 and reproduces the bias/variance trade off curves seen in figure 8.9.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"It uses the MNIST-1D database which can be found at https://github.com/greydanus/mnist1d\n",
|
"It uses the MNIST-1D database which can be found at https://github.com/greydanus/mnist1d\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
@@ -217,7 +217,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"The following code produces the double descent curve by training the model with different numbers of hidden units and plotting the test error.\n",
|
"The following code produces the double descent curve by training the model with different numbers of hidden units and plotting the test error.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"TO DO:\n",
|
"TODO:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"*Before* you run the code, and considering that there are 4000 training examples predict:<br>\n",
|
"*Before* you run the code, and considering that there are 4000 training examples predict:<br>\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the strange properties of high-dimensional spaces as discussed in the notes at the end of chapter 8.\n",
|
"This notebook investigates the strange properties of high-dimensional spaces as discussed in the notes at the end of chapter 8.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates adding L2 regularization to the loss function for the Gabor model as in figure 9.1.\n",
|
"This notebook investigates adding L2 regularization to the loss function for the Gabor model as in figure 9.1.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates how the finite step sizes in gradient descent cause the trajectory to deviate and how this can be explained by adding an implicit regularization term. It recreates figure 9.3 from the book.\n",
|
"This notebook investigates how the finite step sizes in gradient descent cause the trajectory to deviate and how this can be explained by adding an implicit regularization term. It recreates figure 9.3 from the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates how ensembling can improve the performance of models. We'll work with the simplified neural network model (figure 8.4 of book) which we can fit in closed form, and so we can eliminate any errors due to not finding the global maximum.\n",
|
"This notebook investigates how ensembling can improve the performance of models. We'll work with the simplified neural network model (figure 8.4 of book) which we can fit in closed form, and so we can eliminate any errors due to not finding the global maximum.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the Bayesian approach to model fitting and reproduces figure 9.11 from the book.\n",
|
"This notebook investigates the Bayesian approach to model fitting and reproduces figure 9.11 from the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates data augmentation for the MNIST-1D model.\n",
|
"This notebook investigates data augmentation for the MNIST-1D model.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
@@ -107,10 +107,7 @@
|
|||||||
" # Initialize the parameters with He initialization\n",
|
" # Initialize the parameters with He initialization\n",
|
||||||
" if isinstance(layer_in, nn.Linear):\n",
|
" if isinstance(layer_in, nn.Linear):\n",
|
||||||
" nn.init.kaiming_uniform_(layer_in.weight)\n",
|
" nn.init.kaiming_uniform_(layer_in.weight)\n",
|
||||||
" layer_in.bias.data.fill_(0.0)\n",
|
" layer_in.bias.data.fill_(0.0)\n"
|
||||||
"\n",
|
|
||||||
"# Call the function you just defined\n",
|
|
||||||
"model.apply(weights_init)"
|
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "JfIFWFIL33eF"
|
"id": "JfIFWFIL33eF"
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates 1D convolutional layers.\n",
|
"This notebook investigates 1D convolutional layers.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates a 1D convolutional network for MNIST-1D as in figure 10.7 and 10.8a.\n",
|
"This notebook investigates a 1D convolutional network for MNIST-1D as in figure 10.7 and 10.8a.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the 2D convolution operation. It asks you to hand code the convolution so we can be sure that we are computing the same thing as in PyTorch. The next notebook uses the convolutional layers in PyTorch directly.\n",
|
"This notebook investigates the 2D convolution operation. It asks you to hand code the convolution so we can be sure that we are computing the same thing as in PyTorch. The next notebook uses the convolutional layers in PyTorch directly.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -31,9 +31,9 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# **Notebook 10.4: Downsampling and Upsampling**\n",
|
"# **Notebook 10.4: Downsampling and Upsampling**\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the down sampling and downsampling methods discussed in section 10.4 of the book.\n",
|
"This notebook investigates the upsampling and downsampling methods discussed in section 10.4 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
@@ -73,7 +73,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"def subsample(x_in):\n",
|
"def subsample(x_in):\n",
|
||||||
" x_out = np.zeros(( int(np.ceil(x_in.shape[0]/2)), int(np.ceil(x_in.shape[1]/2)) ))\n",
|
" x_out = np.zeros(( int(np.ceil(x_in.shape[0]/2)), int(np.ceil(x_in.shape[1]/2)) ))\n",
|
||||||
" # TO DO -- write the subsampling routine\n",
|
" # TODO -- write the subsampling routine\n",
|
||||||
" # Replace this line\n",
|
" # Replace this line\n",
|
||||||
" x_out = x_out\n",
|
" x_out = x_out\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -159,7 +159,7 @@
|
|||||||
"# Now let's try max-pooling\n",
|
"# Now let's try max-pooling\n",
|
||||||
"def maxpool(x_in):\n",
|
"def maxpool(x_in):\n",
|
||||||
" x_out = np.zeros(( int(np.floor(x_in.shape[0]/2)), int(np.floor(x_in.shape[1]/2)) ))\n",
|
" x_out = np.zeros(( int(np.floor(x_in.shape[0]/2)), int(np.floor(x_in.shape[1]/2)) ))\n",
|
||||||
" # TO DO -- write the maxpool routine\n",
|
" # TODO -- write the maxpool routine\n",
|
||||||
" # Replace this line\n",
|
" # Replace this line\n",
|
||||||
" x_out = x_out\n",
|
" x_out = x_out\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -230,7 +230,7 @@
|
|||||||
"# Finally, let's try mean pooling\n",
|
"# Finally, let's try mean pooling\n",
|
||||||
"def meanpool(x_in):\n",
|
"def meanpool(x_in):\n",
|
||||||
" x_out = np.zeros(( int(np.floor(x_in.shape[0]/2)), int(np.floor(x_in.shape[1]/2)) ))\n",
|
" x_out = np.zeros(( int(np.floor(x_in.shape[0]/2)), int(np.floor(x_in.shape[1]/2)) ))\n",
|
||||||
" # TO DO -- write the meanpool routine\n",
|
" # TODO -- write the meanpool routine\n",
|
||||||
" # Replace this line\n",
|
" # Replace this line\n",
|
||||||
" x_out = x_out\n",
|
" x_out = x_out\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -316,7 +316,7 @@
|
|||||||
"# Let's first use the duplication method\n",
|
"# Let's first use the duplication method\n",
|
||||||
"def duplicate(x_in):\n",
|
"def duplicate(x_in):\n",
|
||||||
" x_out = np.zeros(( x_in.shape[0]*2, x_in.shape[1]*2 ))\n",
|
" x_out = np.zeros(( x_in.shape[0]*2, x_in.shape[1]*2 ))\n",
|
||||||
" # TO DO -- write the duplication routine\n",
|
" # TODO -- write the duplication routine\n",
|
||||||
" # Replace this line\n",
|
" # Replace this line\n",
|
||||||
" x_out = x_out\n",
|
" x_out = x_out\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -388,7 +388,7 @@
|
|||||||
"# The input x_high_res is the original high res image, from which you can deduce the position of the maximum index\n",
|
"# The input x_high_res is the original high res image, from which you can deduce the position of the maximum index\n",
|
||||||
"def max_unpool(x_in, x_high_res):\n",
|
"def max_unpool(x_in, x_high_res):\n",
|
||||||
" x_out = np.zeros(( x_in.shape[0]*2, x_in.shape[1]*2 ))\n",
|
" x_out = np.zeros(( x_in.shape[0]*2, x_in.shape[1]*2 ))\n",
|
||||||
" # TO DO -- write the subsampling routine\n",
|
" # TODO -- write the subsampling routine\n",
|
||||||
" # Replace this line\n",
|
" # Replace this line\n",
|
||||||
" x_out = x_out\n",
|
" x_out = x_out\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -460,7 +460,7 @@
|
|||||||
" x_out = np.zeros(( x_in.shape[0]*2, x_in.shape[1]*2 ))\n",
|
" x_out = np.zeros(( x_in.shape[0]*2, x_in.shape[1]*2 ))\n",
|
||||||
" x_in_pad = np.zeros((x_in.shape[0]+1, x_in.shape[1]+1))\n",
|
" x_in_pad = np.zeros((x_in.shape[0]+1, x_in.shape[1]+1))\n",
|
||||||
" x_in_pad[0:x_in.shape[0],0:x_in.shape[1]] = x_in\n",
|
" x_in_pad[0:x_in.shape[0],0:x_in.shape[1]] = x_in\n",
|
||||||
" # TO DO -- write the duplication routine\n",
|
" # TODO -- write the duplication routine\n",
|
||||||
" # Replace this line\n",
|
" # Replace this line\n",
|
||||||
" x_out = x_out\n",
|
" x_out = x_out\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -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": {
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"The code is adapted from https://nextjournal.com/gkoehler/pytorch-mnist\n",
|
"The code is adapted from https://nextjournal.com/gkoehler/pytorch-mnist\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
@@ -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",
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the phenomenon of shattered gradients as discussed in section 11.1.1. It replicates some of the experiments in [Balduzzi et al. (2017)](https://arxiv.org/abs/1702.08591).\n",
|
"This notebook investigates the phenomenon of shattered gradients as discussed in section 11.1.1. It replicates some of the experiments in [Balduzzi et al. (2017)](https://arxiv.org/abs/1702.08591).\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook adapts the networks for MNIST1D to use residual connections.\n",
|
"This notebook adapts the networks for MNIST1D to use residual connections.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the use of batch normalization in residual networks.\n",
|
"This notebook investigates the use of batch normalization in residual networks.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook builds a self-attention mechanism from scratch, as discussed in section 12.2 of the book.\n",
|
"This notebook builds a self-attention mechanism from scratch, as discussed in section 12.2 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook builds a multihead self-attention mechanism as in figure 12.6\n",
|
"This notebook builds a multihead self-attention mechanism as in figure 12.6\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook builds set of tokens from a text string as in figure 12.8 of the book.\n",
|
"This notebook builds set of tokens from a text string as in figure 12.8 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"I adapted this code from *SOMEWHERE*. If anyone recognizes it, can you let me know and I will give the proper attribution or rewrite if the license is not permissive.\n",
|
"I adapted this code from *SOMEWHERE*. If anyone recognizes it, can you let me know and I will give the proper attribution or rewrite if the license is not permissive.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This practical investigates neural decoding from transformer models. \n",
|
"This practical investigates neural decoding from transformer models. \n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates representing graphs with matrices as illustrated in figure 13.4 from the book.\n",
|
"This notebook investigates representing graphs with matrices as illustrated in figure 13.4 from the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates representing graphs with matrices as illustrated in figure 13.4 from the book.\n",
|
"This notebook investigates representing graphs with matrices as illustrated in figure 13.4 from the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates neighborhood sampling of graphs as in figure 13.10 from the book.\n",
|
"This notebook investigates neighborhood sampling of graphs as in figure 13.10 from the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook builds a graph attention mechanism from scratch, as discussed in section 13.8.6 of the book and illustrated in figure 13.12c\n",
|
"This notebook builds a graph attention mechanism from scratch, as discussed in section 13.8.6 of the book and illustrated in figure 13.12c\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
@@ -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"
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the GAN toy example as illustrated in figure 15.1 in the book.\n",
|
"This notebook investigates the GAN toy example as illustrated in figure 15.1 in the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the GAN toy example as illustrated in figure 15.1 in the book.\n",
|
"This notebook investigates the GAN toy example as illustrated in figure 15.1 in the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
@@ -86,6 +86,7 @@
|
|||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"source": [
|
"source": [
|
||||||
"# TODO Define the distance matrix from figure 15.8d\n",
|
"# TODO Define the distance matrix from figure 15.8d\n",
|
||||||
|
"# The index should be normalized before being used in the distance calculation.\n",
|
||||||
"# Replace this line\n",
|
"# Replace this line\n",
|
||||||
"dist_mat = np.zeros((10,10))\n",
|
"dist_mat = np.zeros((10,10))\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates a 1D normalizing flows example similar to that illustrated in figures 16.1 to 16.3 in the book.\n",
|
"This notebook investigates a 1D normalizing flows example similar to that illustrated in figures 16.1 to 16.3 in the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates a 1D normalizing flows example similar to that illustrated in figure 16.7 in the book.\n",
|
"This notebook investigates a 1D normalizing flows example similar to that illustrated in figure 16.7 in the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates a 1D normalizing flows example similar to that illustrated in figure 16.9 in the book.\n",
|
"This notebook investigates a 1D normalizing flows example similar to that illustrated in figure 16.9 in the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates a non-linear latent variable model similar to that in figures 17.2 and 17.3 of the book.\n",
|
"This notebook investigates a non-linear latent variable model similar to that in figures 17.2 and 17.3 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the reparameterization trick as described in section 17.7 of the book.\n",
|
"This notebook investigates the reparameterization trick as described in section 17.7 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -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/Chap17/17_3_Importance_Sampling.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/Chap17/17_3_Importance_Sampling.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": "t9vk9Elugvmi"
|
"id": "t9vk9Elugvmi"
|
||||||
@@ -22,7 +20,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates importance sampling as described in section 17.8.1 of the book.\n",
|
"This notebook investigates importance sampling as described in section 17.8.1 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
@@ -40,7 +38,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "f7a6xqKjkmvT"
|
"id": "f7a6xqKjkmvT"
|
||||||
@@ -126,7 +123,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "Jr4UPcqmnXCS"
|
"id": "Jr4UPcqmnXCS"
|
||||||
@@ -166,8 +162,8 @@
|
|||||||
"mean_all = np.zeros_like(n_sample_all)\n",
|
"mean_all = np.zeros_like(n_sample_all)\n",
|
||||||
"variance_all = np.zeros_like(n_sample_all)\n",
|
"variance_all = np.zeros_like(n_sample_all)\n",
|
||||||
"for i in range(len(n_sample_all)):\n",
|
"for i in range(len(n_sample_all)):\n",
|
||||||
" print(\"Computing mean and variance for expectation with %d samples\"%(n_sample_all[i]))\n",
|
" mean_all[i],variance_all[i] = compute_mean_variance(n_sample_all[i])\n",
|
||||||
" mean_all[i],variance_all[i] = compute_mean_variance(n_sample_all[i])"
|
" print(\"No samples: \", n_sample_all[i], \", Mean: \", mean_all[i], \", Variance: \", variance_all[i])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -189,7 +185,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "XTUpxFlSuOl7"
|
"id": "XTUpxFlSuOl7"
|
||||||
@@ -199,7 +194,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "6hxsl3Pxo1TT"
|
"id": "6hxsl3Pxo1TT"
|
||||||
@@ -234,7 +228,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "G9Xxo0OJsIqD"
|
"id": "G9Xxo0OJsIqD"
|
||||||
@@ -283,7 +276,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "2sVDqP0BvxqM"
|
"id": "2sVDqP0BvxqM"
|
||||||
@@ -313,8 +305,8 @@
|
|||||||
"mean_all2 = np.zeros_like(n_sample_all)\n",
|
"mean_all2 = np.zeros_like(n_sample_all)\n",
|
||||||
"variance_all2 = np.zeros_like(n_sample_all)\n",
|
"variance_all2 = np.zeros_like(n_sample_all)\n",
|
||||||
"for i in range(len(n_sample_all)):\n",
|
"for i in range(len(n_sample_all)):\n",
|
||||||
" print(\"Computing variance for expectation with %d samples\"%(n_sample_all[i]))\n",
|
" mean_all2[i], variance_all2[i] = compute_mean_variance2(n_sample_all[i])\n",
|
||||||
" mean_all2[i], variance_all2[i] = compute_mean_variance2(n_sample_all[i])"
|
" print(\"No samples: \", n_sample_all[i], \", Mean: \", mean_all2[i], \", Variance: \", variance_all2[i])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -348,7 +340,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "EtBP6NeLwZqz"
|
"id": "EtBP6NeLwZqz"
|
||||||
@@ -360,7 +351,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "_wuF-NoQu1--"
|
"id": "_wuF-NoQu1--"
|
||||||
@@ -432,8 +422,8 @@
|
|||||||
"mean_all2b = np.zeros_like(n_sample_all)\n",
|
"mean_all2b = np.zeros_like(n_sample_all)\n",
|
||||||
"variance_all2b = np.zeros_like(n_sample_all)\n",
|
"variance_all2b = np.zeros_like(n_sample_all)\n",
|
||||||
"for i in range(len(n_sample_all)):\n",
|
"for i in range(len(n_sample_all)):\n",
|
||||||
" print(\"Computing variance for expectation with %d samples\"%(n_sample_all[i]))\n",
|
" mean_all2b[i], variance_all2b[i] = compute_mean_variance2b(n_sample_all[i])\n",
|
||||||
" mean_all2b[i], variance_all2b[i] = compute_mean_variance2b(n_sample_all[i])"
|
" print(\"No samples: \", n_sample_all[i], \", Mean: \", mean_all2b[i], \", Variance: \", variance_all2b[i])"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -478,7 +468,6 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"attachments": {},
|
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "y8rgge9MNiOc"
|
"id": "y8rgge9MNiOc"
|
||||||
@@ -490,9 +479,8 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"colab": {
|
"colab": {
|
||||||
"authorship_tag": "ABX9TyNecz9/CDOggPSmy1LjT/Dv",
|
"provenance": [],
|
||||||
"include_colab_link": true,
|
"include_colab_link": true
|
||||||
"provenance": []
|
|
||||||
},
|
},
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3",
|
"display_name": "Python 3",
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the diffusion encoder as described in section 18.2 of the book.\n",
|
"This notebook investigates the diffusion encoder as described in section 18.2 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the diffusion encoder as described in section 18.3 and 18.4 of the book.\n",
|
"This notebook investigates the diffusion encoder as described in section 18.3 and 18.4 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the reparameterized model as described in section 18.5 of the book and implements algorithms 18.1 and 18.2.\n",
|
"This notebook investigates the reparameterized model as described in section 18.5 of the book and implements algorithms 18.1 and 18.2.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the reparameterized model as described in section 18.5 of the book and computers the results shown in figure 18.10c-f. These models are based on the paper \"Denoising diffusion implicit models\" which can be found [here](https://arxiv.org/pdf/2010.02502.pdf).\n",
|
"This notebook investigates the reparameterized model as described in section 18.5 of the book and computers the results shown in figure 18.10c-f. These models are based on the paper \"Denoising diffusion implicit models\" which can be found [here](https://arxiv.org/pdf/2010.02502.pdf).\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates Markov decision processes as described in section 19.1 of the book.\n",
|
"This notebook investigates Markov decision processes as described in section 19.1 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
"metadata": {
|
"metadata": {
|
||||||
"colab": {
|
"colab": {
|
||||||
"provenance": [],
|
"provenance": [],
|
||||||
"authorship_tag": "ABX9TyOlD6kmCxX3SKKuh3oJikKA",
|
|
||||||
"include_colab_link": true
|
"include_colab_link": true
|
||||||
},
|
},
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
@@ -33,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the dynamic programming approach to tabular reinforcement learning as described in figure 19.10 of the book.\n",
|
"This notebook investigates the dynamic programming approach to tabular reinforcement learning as described in figure 19.10 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
@@ -406,6 +405,10 @@
|
|||||||
" state_values_new[state] = 3.0\n",
|
" state_values_new[state] = 3.0\n",
|
||||||
" break\n",
|
" break\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" # TODO -- Write this function (from equation 19.11, but bear in mind policy is deterministic here)\n",
|
||||||
|
" # Replace this line\n",
|
||||||
|
" state_values_new[state] = 0\n",
|
||||||
|
"\n",
|
||||||
" return state_values_new\n",
|
" return state_values_new\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Greedily choose the action that maximizes the value for each state.\n",
|
"# Greedily choose the action that maximizes the value for each state.\n",
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"NOTE! There is a mistake in Figure 19.11 in the first printing of the book, so check the errata to avoid becoming confused. Apologies!\n",
|
"NOTE! There is a mistake in Figure 19.11 in the first printing of the book, so check the errata to avoid becoming confused. Apologies!\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates temporal difference methods for tabular reinforcement learning as described in section 19.3.3 of the book\n",
|
"This notebook investigates temporal difference methods for tabular reinforcement learning as described in section 19.3.3 of the book\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -34,7 +34,7 @@
|
|||||||
"This notebook investigates the method of control variates as described in figure 19.16\n",
|
"This notebook investigates the method of control variates as described in figure 19.16\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates training the network with random data, as illustrated in figure 20.1.\n",
|
"This notebook investigates training the network with random data, as illustrated in figure 20.1.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates training a network with full batch gradient descent as in figure 20.2. There is also a version (notebook takes a long time to run), but this didn't speed it up much for me. If you run out of CoLab time, you'll need to download the Python file and run locally.\n",
|
"This notebook investigates training a network with full batch gradient descent as in figure 20.2. There is also a version (notebook takes a long time to run), but this didn't speed it up much for me. If you run out of CoLab time, you'll need to download the Python file and run locally.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates training a network with full batch gradient descent as in figure 20.2. This is the GPU version (notebook takes a long time to run). If you are using Colab then you need to go change the runtime type to GPU on the Runtime menu. Even then, you may run out of time. If that's the case, you'll need to download the Python file and run locally.\n",
|
"This notebook investigates training a network with full batch gradient descent as in figure 20.2. This is the GPU version (notebook takes a long time to run). If you are using Colab then you need to go change the runtime type to GPU on the Runtime menu. Even then, you may run out of time. If that's the case, you'll need to download the Python file and run locally.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n",
|
||||||
"\n"
|
"\n"
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the phenomenon of lottery tickets as discussed in section 20.2.7. This notebook is highly derivative of the MNIST-1D code hosted by Sam Greydanus at https://github.com/greydanus/mnist1d. \n",
|
"This notebook investigates the phenomenon of lottery tickets as discussed in section 20.2.7. This notebook is highly derivative of the MNIST-1D code hosted by Sam Greydanus at https://github.com/greydanus/mnist1d. \n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook builds uses the network for classification of MNIST from Notebook 10.5. The code is adapted from https://nextjournal.com/gkoehler/pytorch-mnist, and uses the fast gradient sign attack of [Goodfellow et al. (2015)](https://arxiv.org/abs/1412.6572). Having trained, the network, we search for adversarial examples -- inputs which look very similar to class A, but are mistakenly classified as class B. We do this by starting with a correctly classified example and perturbing it according to the gradients of the network so that the output changes.\n",
|
"This notebook builds uses the network for classification of MNIST from Notebook 10.5. The code is adapted from https://nextjournal.com/gkoehler/pytorch-mnist, and uses the fast gradient sign attack of [Goodfellow et al. (2015)](https://arxiv.org/abs/1412.6572). Having trained, the network, we search for adversarial examples -- inputs which look very similar to class A, but are mistakenly classified as class B. We do this by starting with a correctly classified example and perturbing it according to the gradients of the network so that the output changes.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates a post-processing method for bias mitigation (see figure 21.2 in the book). It based on this [blog](https://www.borealisai.com/research-blogs/tutorial1-bias-and-fairness-ai/) that I wrote for Borealis AI in 2019, which itself was derived from [this blog](https://research.google.com/bigpicture/attacking-discrimination-in-ml/) by Wattenberg, Viégas, and Hardt.\n",
|
"This notebook investigates a post-processing method for bias mitigation (see figure 21.2 in the book). It based on this [blog](https://www.borealisai.com/research-blogs/tutorial1-bias-and-fairness-ai/) that I wrote for Borealis AI in 2019, which itself was derived from [this blog](https://research.google.com/bigpicture/attacking-discrimination-in-ml/) by Wattenberg, Viégas, and Hardt.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
]
|
]
|
||||||
@@ -328,7 +328,7 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# TO DO -- try to change the two thresholds so the overall probability of getting the loan is 0.6 for each group\n",
|
"# TODO -- try to change the two thresholds so the overall probability of getting the loan is 0.6 for each group\n",
|
||||||
"# Change the values in these lines\n",
|
"# Change the values in these lines\n",
|
||||||
"tau0 = 0.3\n",
|
"tau0 = 0.3\n",
|
||||||
"tau1 = -0.1\n",
|
"tau1 = -0.1\n",
|
||||||
@@ -393,7 +393,7 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# TO DO -- try to change the two thresholds so the true positive are 0.8 for each group\n",
|
"# TODO --try to change the two thresholds so the true positive are 0.8 for each group\n",
|
||||||
"# Change the values in these lines so that both points on the curves have a height of 0.8\n",
|
"# Change the values in these lines so that both points on the curves have a height of 0.8\n",
|
||||||
"tau0 = -0.1\n",
|
"tau0 = -0.1\n",
|
||||||
"tau1 = -0.7\n",
|
"tau1 = -0.7\n",
|
||||||
|
|||||||
@@ -33,7 +33,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"This notebook investigates the LIME explainability method as depicted in figure 21.3 of the book.\n",
|
"This notebook investigates the LIME explainability method as depicted in figure 21.3 of the book.\n",
|
||||||
"\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 make predictions about what is going to happen or write code to complete the functions.\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 make predictions about what is going to happen or write code to complete the functions.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions.\n"
|
||||||
],
|
],
|
||||||
|
|||||||
326
Trees/LinearRegression_FitModel.ipynb
Normal file
326
Trees/LinearRegression_FitModel.ipynb
Normal file
File diff suppressed because one or more lines are too long
357
Trees/LinearRegression_FitModel_Answers.ipynb
Normal file
357
Trees/LinearRegression_FitModel_Answers.ipynb
Normal file
File diff suppressed because one or more lines are too long
343
Trees/LinearRegression_FitModel_Quadratic.ipynb
Normal file
343
Trees/LinearRegression_FitModel_Quadratic.ipynb
Normal file
File diff suppressed because one or more lines are too long
277
Trees/LinearRegression_LossFunction.ipynb
Normal file
277
Trees/LinearRegression_LossFunction.ipynb
Normal file
File diff suppressed because one or more lines are too long
325
Trees/LinearRegression_LossFunction_Answers.ipynb
Normal file
325
Trees/LinearRegression_LossFunction_Answers.ipynb
Normal file
File diff suppressed because one or more lines are too long
248
Trees/SAT_Exhaustive.ipynb
Normal file
248
Trees/SAT_Exhaustive.ipynb
Normal file
File diff suppressed because one or more lines are too long
250
Trees/SAT_Exhaustive_Answers.ipynb
Normal file
250
Trees/SAT_Exhaustive_Answers.ipynb
Normal file
File diff suppressed because one or more lines are too long
Binary file not shown.
2229
UDL_Equations.tex
Normal file
2229
UDL_Equations.tex
Normal file
File diff suppressed because it is too large
Load Diff
BIN
UDL_Errata.pdf
BIN
UDL_Errata.pdf
Binary file not shown.
@@ -33,6 +33,94 @@ const citation = `
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const news = [
|
const news = [
|
||||||
|
{
|
||||||
|
date: "01/23/25",
|
||||||
|
content: (
|
||||||
|
<HeroNewsItemContent>
|
||||||
|
Added{" "}
|
||||||
|
<UDLLink href="https://github.com/udlbook/udlbook/raw/main/understanding-deep-learning-final.bib">
|
||||||
|
bibfile
|
||||||
|
</UDLLink>{" "} for book and
|
||||||
|
<UDLLink href="https://github.com/udlbook/udlbook/raw/main/UDL_Equations.tex">
|
||||||
|
LaTeX
|
||||||
|
</UDLLink>{" "}
|
||||||
|
for all equations
|
||||||
|
</HeroNewsItemContent>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: "12/17/24",
|
||||||
|
content: (
|
||||||
|
<HeroNewsItemContent>
|
||||||
|
|
||||||
|
<UDLLink href="https://www.youtube.com/playlist?list=PLRdABJkXXytCz19PsZ1PCQBKoZGV069k3">
|
||||||
|
Video lectures
|
||||||
|
</UDLLink>{" "}
|
||||||
|
for chapters 1-12 from Tamer Elsayed of Qatar University.
|
||||||
|
</HeroNewsItemContent>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: "12/05/24",
|
||||||
|
content: (
|
||||||
|
<HeroNewsItemContent>
|
||||||
|
New{" "}
|
||||||
|
<UDLLink href="https://rbcborealis.com/research-blogs/neural-network-gaussian-processes/">
|
||||||
|
blog
|
||||||
|
</UDLLink>{" "}
|
||||||
|
on Neural network Gaussian processes
|
||||||
|
</HeroNewsItemContent>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
date: "11/14/24",
|
||||||
|
content: (
|
||||||
|
<HeroNewsItemContent>
|
||||||
|
New{" "}
|
||||||
|
<UDLLink href=" https://rbcborealis.com/research-blogs/bayesian-neural-networks/">
|
||||||
|
blog
|
||||||
|
</UDLLink>{" "}
|
||||||
|
on Bayesian Neural Networks
|
||||||
|
</HeroNewsItemContent>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: "08/13/24",
|
||||||
|
content: (
|
||||||
|
<HeroNewsItemContent>
|
||||||
|
New{" "}
|
||||||
|
<UDLLink href="https://www.borealisai.com/research-blogs/bayesian-machine-learning-function-space/">
|
||||||
|
blog
|
||||||
|
</UDLLink>{" "}
|
||||||
|
on Bayesian machine learning (function perspective)
|
||||||
|
</HeroNewsItemContent>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: "08/05/24",
|
||||||
|
content: (
|
||||||
|
<HeroNewsItemContent>
|
||||||
|
Added{" "}
|
||||||
|
<UDLLink href="https://udlbook.github.io/udlfigures/">
|
||||||
|
interactive figures
|
||||||
|
</UDLLink>{" "}
|
||||||
|
to explore 1D linear regression, shallow and deep networks, Gabor model.
|
||||||
|
</HeroNewsItemContent>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: "07/30/24",
|
||||||
|
content: (
|
||||||
|
<HeroNewsItemContent>
|
||||||
|
New{" "}
|
||||||
|
<UDLLink href="https://www.borealisai.com/research-blogs/bayesian-machine-learning-parameter-space/">
|
||||||
|
blog
|
||||||
|
</UDLLink>{" "}
|
||||||
|
on Bayesian machine learning (parameter perspective)
|
||||||
|
</HeroNewsItemContent>
|
||||||
|
),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
date: "05/22/24",
|
date: "05/22/24",
|
||||||
content: (
|
content: (
|
||||||
@@ -184,8 +272,8 @@ export default function HeroSection() {
|
|||||||
<HeroImgWrap>
|
<HeroImgWrap>
|
||||||
<Img src={img} alt="Book Cover" />
|
<Img src={img} alt="Book Cover" />
|
||||||
</HeroImgWrap>
|
</HeroImgWrap>
|
||||||
<HeroLink href="https://github.com/udlbook/udlbook/releases/download/v4.0.1/UnderstandingDeepLearning_05_27_24_C.pdf">
|
<HeroLink href="https://github.com/udlbook/udlbook/releases/download/v5.00/UnderstandingDeepLearning_11_21_24_C.pdf">
|
||||||
Download full PDF (27 May 2024)
|
Download full PDF (21 November 2024)
|
||||||
</HeroLink>
|
</HeroLink>
|
||||||
<br />
|
<br />
|
||||||
<HeroDownloadsImg
|
<HeroDownloadsImg
|
||||||
@@ -201,7 +289,7 @@ export default function HeroSection() {
|
|||||||
<HeroLink href="https://github.com/udlbook/udlbook/raw/main/UDL_Errata.pdf">
|
<HeroLink href="https://github.com/udlbook/udlbook/raw/main/UDL_Errata.pdf">
|
||||||
Errata
|
Errata
|
||||||
</HeroLink>
|
</HeroLink>
|
||||||
</HeroColumn2>
|
</HeroColumn2> <h1></h1>
|
||||||
</HeroRow>
|
</HeroRow>
|
||||||
</HeroContent>
|
</HeroContent>
|
||||||
</HeroContainer>
|
</HeroContainer>
|
||||||
|
|||||||
@@ -280,6 +280,12 @@ export default function InstructorsSection() {
|
|||||||
</InstructorsLink>{" "}
|
</InstructorsLink>{" "}
|
||||||
with MIT Press for answer booklet.
|
with MIT Press for answer booklet.
|
||||||
<InstructorsContent></InstructorsContent>
|
<InstructorsContent></InstructorsContent>
|
||||||
|
<TopLine>Interactive figures</TopLine>
|
||||||
|
<InstructorsLink href="https://udlbook.github.io/udlfigures/">
|
||||||
|
Interactive figures </InstructorsLink>{" "}
|
||||||
|
to illustrate ideas in class
|
||||||
|
<InstructorsContent></InstructorsContent>
|
||||||
|
|
||||||
<TopLine>Full slides</TopLine>
|
<TopLine>Full slides</TopLine>
|
||||||
<InstructorsContent>
|
<InstructorsContent>
|
||||||
Slides for 20 lecture undergraduate deep learning course:
|
Slides for 20 lecture undergraduate deep learning course:
|
||||||
@@ -296,6 +302,11 @@ export default function InstructorsSection() {
|
|||||||
))}
|
))}
|
||||||
</ol>
|
</ol>
|
||||||
</InstructorsContent>
|
</InstructorsContent>
|
||||||
|
<TopLine>LaTeX for equations</TopLine>
|
||||||
|
A {" "} <InstructorsLink href="https://github.com/udlbook/udlbook/raw/main/UDL_Equations.tex">
|
||||||
|
working Latex file </InstructorsLink>{" "}
|
||||||
|
containing all of the equations
|
||||||
|
<InstructorsContent></InstructorsContent>
|
||||||
</Column1>
|
</Column1>
|
||||||
<Column2>
|
<Column2>
|
||||||
<TopLine>Figures</TopLine>
|
<TopLine>Figures</TopLine>
|
||||||
@@ -325,6 +336,11 @@ export default function InstructorsSection() {
|
|||||||
</InstructorsLink>{" "}
|
</InstructorsLink>{" "}
|
||||||
for editing equations in figures.
|
for editing equations in figures.
|
||||||
<InstructorsContent></InstructorsContent>
|
<InstructorsContent></InstructorsContent>
|
||||||
|
<TopLine>LaTeX Bibfile </TopLine>
|
||||||
|
The {" "} <InstructorsLink href="https://github.com/udlbook/udlbook/raw/main/understanding-deep-learning-final.bib">
|
||||||
|
bibfile </InstructorsLink>{" "}
|
||||||
|
containing all of the references
|
||||||
|
<InstructorsContent></InstructorsContent>
|
||||||
</Column2>
|
</Column2>
|
||||||
</InstructorsRow2>
|
</InstructorsRow2>
|
||||||
</InstructorsWrapper>
|
</InstructorsWrapper>
|
||||||
|
|||||||
23
src/components/Media/index.jsx
Normal file → Executable file
23
src/components/Media/index.jsx
Normal file → Executable file
@@ -120,23 +120,18 @@ export default function MediaSection() {
|
|||||||
by Vishal V.
|
by Vishal V.
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Amazon{" "}
|
Book{" "}
|
||||||
<MediaLink href="https://www.amazon.com/Understanding-Deep-Learning-Simon-Prince-ebook/product-reviews/B0BXKH8XY6/">
|
<MediaLink href="https://www.linkedin.com/pulse/review-understanding-deep-learning-prof-simon-prince-chandrasekharan-6egec/">
|
||||||
reviews
|
review
|
||||||
</MediaLink>
|
</MediaLink>{" "}
|
||||||
</li>
|
by Nidhin Chandrasekharan
|
||||||
<li>
|
|
||||||
Goodreads{" "}
|
|
||||||
<MediaLink href="https://www.goodreads.com/book/show/123239819-understanding-deep-learning?">
|
|
||||||
reviews{" "}
|
|
||||||
</MediaLink>
|
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Book{" "}
|
Book{" "}
|
||||||
<MediaLink href="https://medium.com/@vishalvignesh/udl-book-review-the-new-deep-learning-textbook-youll-want-to-finish-69e1557b018d">
|
<MediaLink href="https://www.justinmath.com/the-best-neural-nets-textbook/">
|
||||||
review
|
review
|
||||||
</MediaLink>{" "}
|
</MediaLink>{" "}
|
||||||
by Vishal V.
|
by Justin Skycak
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</MediaContent>
|
</MediaContent>
|
||||||
@@ -155,6 +150,10 @@ export default function MediaSection() {
|
|||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
</MediaContent>
|
</MediaContent>
|
||||||
|
<TopLine>Video lectures</TopLine>
|
||||||
|
<MediaLink href="https://www.youtube.com/playlist?list=PLRdABJkXXytCz19PsZ1PCQBKoZGV069k3">
|
||||||
|
Video lectures
|
||||||
|
</MediaLink>{" "} for chapter 1-12 from Tamer Elsayed
|
||||||
</Column2>
|
</Column2>
|
||||||
</MediaRow2>
|
</MediaRow2>
|
||||||
</MediaWrapper>
|
</MediaWrapper>
|
||||||
|
|||||||
49
src/components/More/index.jsx
Normal file → Executable file
49
src/components/More/index.jsx
Normal file → Executable file
@@ -376,6 +376,51 @@ const aiTheory = [
|
|||||||
"NTK and generalizability",
|
"NTK and generalizability",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
text: "Bayesian ML I",
|
||||||
|
link: "https://www.borealisai.com/research-blogs/bayesian-machine-learning-parameter-space/",
|
||||||
|
details: [
|
||||||
|
"Maximum likelihood",
|
||||||
|
"Maximum a posteriori",
|
||||||
|
"The Bayesian approach",
|
||||||
|
"Example: 1D linear regression",
|
||||||
|
"Practical concerns",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "Bayesian ML II",
|
||||||
|
link: "https://www.borealisai.com/research-blogs/bayesian-machine-learning-function-space/",
|
||||||
|
details: [
|
||||||
|
"Function space",
|
||||||
|
"Gaussian processes",
|
||||||
|
"Inference",
|
||||||
|
"Non-linear regression",
|
||||||
|
"Kernels and the kernel trick",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "Bayesian neural networks",
|
||||||
|
link: "https://rbcborealis.com/research-blogs/bayesian-neural-networks/",
|
||||||
|
details: [
|
||||||
|
"Sampling vs. variational approximation",
|
||||||
|
"MCMC methods",
|
||||||
|
"SWAG and MultiSWAG",
|
||||||
|
"Bayes by backprop",
|
||||||
|
"Monte Carlo dropout",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "Neural network Gaussian processes",
|
||||||
|
link: "https://rbcborealis.com/research-blogs/neural-network-gaussian-processes/",
|
||||||
|
details: [
|
||||||
|
"Shallow networks as GPs",
|
||||||
|
"Neural network Gaussian processes",
|
||||||
|
"NNGP Kernel",
|
||||||
|
"Kernel regression",
|
||||||
|
"Network stability",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const unsupervisedLearning = [
|
const unsupervisedLearning = [
|
||||||
@@ -689,7 +734,7 @@ export default function MoreSection() {
|
|||||||
</MoreRow>
|
</MoreRow>
|
||||||
<MoreRow2>
|
<MoreRow2>
|
||||||
<Column1>
|
<Column1>
|
||||||
<TopLine>Book</TopLine>
|
<TopLine>Computer vision book</TopLine>
|
||||||
<MoreOuterList>
|
<MoreOuterList>
|
||||||
{book.map((item, index) => (
|
{book.map((item, index) => (
|
||||||
<li key={index}>
|
<li key={index}>
|
||||||
@@ -817,7 +862,7 @@ export default function MoreSection() {
|
|||||||
</Column1>
|
</Column1>
|
||||||
|
|
||||||
<Column2>
|
<Column2>
|
||||||
<TopLine>AI Theory</TopLine>
|
<TopLine>ML Theory</TopLine>
|
||||||
<MoreOuterList>
|
<MoreOuterList>
|
||||||
{aiTheory.map((item, index) => (
|
{aiTheory.map((item, index) => (
|
||||||
<li key={index}>
|
<li key={index}>
|
||||||
|
|||||||
8672
understanding-deep-learning-final.bib
Normal file
8672
understanding-deep-learning-final.bib
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user