Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
udlbook committed Nov 14, 2023
1 parent aaa1343 commit 5b009de
Showing 1 changed file with 106 additions and 26 deletions.
132 changes: 106 additions & 26 deletions Notebooks/Chap07/7_1_Backpropagation_in_Toy_Model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMMmg6voEOOY2uS6iFsD42d",
"authorship_tag": "ABX9TyOjXmTmoff61y15VqEB5sDW",
"include_colab_link": true
},
"kernelspec": {
Expand Down Expand Up @@ -83,21 +83,21 @@
"metadata": {
"id": "RIPaoVN834Lj"
},
"execution_count": null,
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Let's first define the original function for $y$ and the likelihood term:"
"Let's first define the original function for $y$ and the loss term:"
],
"metadata": {
"id": "32-ufWhc3v2c"
}
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"id": "AakK_qen3BpU"
},
Expand All @@ -106,7 +106,7 @@
"def fn(x, beta0, beta1, beta2, beta3, omega0, omega1, omega2, omega3):\n",
" return beta3+omega3 * np.cos(beta2 + omega2 * np.exp(beta1 + omega1 * np.sin(beta0 + omega0 * x)))\n",
"\n",
"def likelihood(x, y, beta0, beta1, beta2, beta3, omega0, omega1, omega2, omega3):\n",
"def loss(x, y, beta0, beta1, beta2, beta3, omega0, omega1, omega2, omega3):\n",
" diff = fn(x, beta0, beta1, beta2, beta3, omega0, omega1, omega2, omega3) - y\n",
" return diff * diff"
]
Expand All @@ -126,14 +126,26 @@
"beta0 = 1.0; beta1 = 2.0; beta2 = -3.0; beta3 = 0.4\n",
"omega0 = 0.1; omega1 = -0.4; omega2 = 2.0; omega3 = 3.0\n",
"x = 2.3; y =2.0\n",
"l_i_func = likelihood(x,y,beta0,beta1,beta2,beta3,omega0,omega1,omega2,omega3)\n",
"l_i_func = loss(x,y,beta0,beta1,beta2,beta3,omega0,omega1,omega2,omega3)\n",
"print('l_i=%3.3f'%l_i_func)"
],
"metadata": {
"id": "pwvOcCxr41X_"
"id": "pwvOcCxr41X_",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "9541922c-dfc4-4b2e-dfa3-3298812155ce"
},
"execution_count": null,
"outputs": []
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"l_i=0.139\n"
]
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -163,7 +175,7 @@
"metadata": {
"id": "7t22hALp5zkq"
},
"execution_count": null,
"execution_count": 4,
"outputs": []
},
{
Expand All @@ -178,15 +190,27 @@
{
"cell_type": "code",
"source": [
"dldomega0_fd = (likelihood(x,y,beta0,beta1,beta2,beta3,omega0+0.00001,omega1,omega2,omega3)-likelihood(x,y,beta0,beta1,beta2,beta3,omega0,omega1,omega2,omega3))/0.00001\n",
"dldomega0_fd = (loss(x,y,beta0,beta1,beta2,beta3,omega0+0.00001,omega1,omega2,omega3)-loss(x,y,beta0,beta1,beta2,beta3,omega0,omega1,omega2,omega3))/0.00001\n",
"\n",
"print('dydomega0: Function value = %3.3f, Finite difference value = %3.3f'%(dldomega0_func,dldomega0_fd))"
],
"metadata": {
"id": "1O3XmXMx-HlZ"
"id": "1O3XmXMx-HlZ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "389ed78e-9d8d-4e8b-9e6b-5f20c21407e8"
},
"execution_count": null,
"outputs": []
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"dydomega0: Function value = 5.246, Finite difference value = 5.246\n"
]
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -237,7 +261,7 @@
"metadata": {
"id": "ZWKAq6HC90qV"
},
"execution_count": null,
"execution_count": 6,
"outputs": []
},
{
Expand All @@ -254,10 +278,29 @@
"print(\"like original = %3.3f, like from forward pass = %3.3f\"%(l_i_func, l_i))\n"
],
"metadata": {
"id": "ibxXw7TUW4Sx"
"id": "ibxXw7TUW4Sx",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "4575e3eb-2b16-4e0b-c84e-9c22b443c3ce"
},
"execution_count": null,
"outputs": []
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"f0: true value = 1.230, your value = 0.000\n",
"h1: true value = 0.942, your value = 0.000\n",
"f1: true value = 1.623, your value = 0.000\n",
"h2: true value = 5.068, your value = 0.000\n",
"f2: true value = 7.137, your value = 0.000\n",
"h3: true value = 0.657, your value = 0.000\n",
"f3: true value = 2.372, your value = 0.000\n",
"like original = 0.139, like from forward pass = 0.000\n"
]
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -319,7 +362,7 @@
"metadata": {
"id": "gCQJeI--Egdl"
},
"execution_count": null,
"execution_count": 8,
"outputs": []
},
{
Expand All @@ -335,10 +378,28 @@
"print(\"dldf0: true value = %3.3f, your value = %3.3f\"%(2.281, dldf0))"
],
"metadata": {
"id": "dS1OrLtlaFr7"
"id": "dS1OrLtlaFr7",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "414f0862-ae36-4a0e-b68f-4758835b0e23"
},
"execution_count": null,
"outputs": []
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"dldf3: true value = 0.745, your value = -4.000\n",
"dldh3: true value = 2.234, your value = -12.000\n",
"dldf2: true value = -1.683, your value = 1.000\n",
"dldh2: true value = -3.366, your value = 1.000\n",
"dldf1: true value = -17.060, your value = 1.000\n",
"dldh1: true value = 6.824, your value = 1.000\n",
"dldf0: true value = 2.281, your value = 1.000\n"
]
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -380,7 +441,7 @@
"metadata": {
"id": "1I2BhqZhGMK6"
},
"execution_count": null,
"execution_count": 10,
"outputs": []
},
{
Expand All @@ -397,10 +458,29 @@
"print('dldomega0: Your value = %3.3f, Function value = %3.3f, Finite difference value = %3.3f'%(dldomega0, dldomega0_func, dldomega0_fd))"
],
"metadata": {
"id": "38eiOn2aHgHI"
"id": "38eiOn2aHgHI",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1a67a636-e832-471e-e771-54824363158a"
},
"execution_count": null,
"outputs": []
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"dldbeta3: Your value = 1.000, True value = 0.745\n",
"dldomega3: Your value = 1.000, True value = 0.489\n",
"dldbeta2: Your value = 1.000, True value = -1.683\n",
"dldomega2: Your value = 1.000, True value = -8.530\n",
"dldbeta1: Your value = 1.000, True value = -17.060\n",
"dldomega1: Your value = 1.000, True value = -16.079\n",
"dldbeta0: Your value = 1.000, True value = 2.281\n",
"dldomega0: Your value = 1.000, Function value = 5.246, Finite difference value = 5.246\n"
]
}
]
},
{
"cell_type": "markdown",
Expand Down

0 comments on commit 5b009de

Please sign in to comment.