Skip to content

Commit

Permalink
update day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelina committed Jun 10, 2018
1 parent f06306a commit fe723bc
Showing 1 changed file with 0 additions and 82 deletions.
82 changes: 0 additions & 82 deletions labs/notebooks/basic_tutorials/Exercises_0.10_to_0.15.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -256,88 +256,6 @@
"metadata": {},
"source": [
"# Exercise 0.13 \n",
"1) Consider the function $f(x) = (x + 2)^2 − 16 \\text{exp}(−(x − 2)^2)$. Draw a plot around the $x \\in [−8, 8]$ region."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.arange(-8,8,0.001)\n",
"y = list(map(lambda u: get_y(u),x))\n",
"plt.plot(x,y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2) What is $\\frac{\\partial f}{\\partial x}$?\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3) Use gradient descent to find a local minimum starting from $x0 = −4$ and $x0 = +4$, with $η = .01$. Plot all of the intermediate estimates that you obtain in the same plot."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X = np.linspace(-8, 8, 1000)\n",
"Y = (X+2)**2 - 16*np.exp(-((X-2)**2))\n",
"\n",
"# derivative of the function f\n",
"def get_Y_dev(x):\n",
" return (2*x+4)-16*(-2*x + 4)*np.exp(-((x-2)**2))\n",
"\n",
"def grad_desc(start_x, eps, prec):\n",
" '''\n",
" runs the gradient descent algorithm and returns the list of estimates\n",
" example of use grad_desc(start_x=3.9, eps=0.01, prec=0.00001)\n",
" '''\n",
" x_new = start_x\n",
" x_old = start_x + prec * 2\n",
" res = [x_new]\n",
" while abs(x_old-x_new) > prec:\n",
" x_old = x_new\n",
" x_new = x_old - eps * get_Y_dev(x_new)\n",
" res.append(x_new)\n",
" return np.array(res)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res = grad_desc(4, 0.01, 0.00001)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.arange(-8,8,0.001)\n",
"y = list(map(lambda u: get_y(u),x))\n",
"plt.plot(x,y)\n",
"plt.plot(res, (res+2)**2 - 16*np.exp(-((res-2)**2)), '+')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 0.14 \n",
"Consider the linear regression problem (ordinary least squares) on the Galton dataset, with a single response\n",
"variable\n",
"\n",
Expand Down

0 comments on commit fe723bc

Please sign in to comment.