Skip to content

Commit 13e2fc1

Browse files
authored
Merge pull request #85 from martinRenou/add_voila_presentation
Add Voila presentation
2 parents 6ca05af + 1f45c95 commit 13e2fc1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+6710
-0
lines changed

2019-11-15-voila/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Installation
2+
------------
3+
4+
Building this presentation requires npm. From the talk directory, run:
5+
6+
```bash
7+
npm install
8+
```
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Notebook served by Voilà"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"#### Notebook copied from https://github.com/ChakriCherukuri/mlviz"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"<h2>Gradient Descent</h2>\n",
22+
"* Given a the multi-variable function $\\large {F(x)}$ differentiable in a neighborhood of a point $\\large a$\n",
23+
"* $\\large F(x)$ decreases fastest if one goes from $\\large a$ in the direction of the negative gradient of $\\large F$ at $\\large a$, $\\large -\\nabla{F(a)}$\n",
24+
"\n",
25+
"<h3>Gradient Descent Algorithm:</h3>\n",
26+
"* Choose a starting point, $\\large x_0$\n",
27+
"* Choose the sequence $\\large x_0, x_1, x_2, ...$ such that\n",
28+
"$ \\large x_{n+1} = x_n - \\eta \\nabla(F(x_n) $\n",
29+
"\n",
30+
"So convergence of the gradient descent depends on the starting point $\\large x_0$ and the learning rate $\\large \\eta$"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"from time import sleep\n",
40+
"\n",
41+
"import numpy as np\n",
42+
"\n",
43+
"from ipywidgets import *\n",
44+
"import bqplot.pyplot as plt\n",
45+
"from bqplot import Toolbar"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"f = lambda x: np.exp(-x) * np.sin(5 * x)\n",
55+
"df = lambda x: -np.exp(-x) * np.sin(5 * x) + 5 * np.cos(5 *x) * np.exp(-x)"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"x = np.linspace(0.5, 2.5, 500)\n",
65+
"y = f(x)"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"def update_sol_path(x, y):\n",
75+
" with sol_path.hold_sync():\n",
76+
" sol_path.x = x\n",
77+
" sol_path.y = y\n",
78+
" \n",
79+
" with sol_points.hold_sync():\n",
80+
" sol_points.x = x\n",
81+
" sol_points.y = y"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"def gradient_descent(x0, f, df, eta=.1, tol=1e-6, num_iters=10):\n",
91+
" x = [x0]\n",
92+
" i = 0\n",
93+
" \n",
94+
" while i < num_iters:\n",
95+
" x_prev = x[-1]\n",
96+
" grad = df(x_prev)\n",
97+
" x_curr = x_prev - eta * grad\n",
98+
" x.append(x_curr)\n",
99+
" sol_lbl.value = sol_lbl_tmpl.format(x_curr)\n",
100+
" sleep(.5)\n",
101+
" \n",
102+
" update_sol_path(x, [f(i) for i in x])\n",
103+
" \n",
104+
" if np.abs(x_curr - x_prev) < tol:\n",
105+
" break\n",
106+
" i += 1"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"metadata": {
113+
"scrolled": false
114+
},
115+
"outputs": [],
116+
"source": [
117+
"txt_layout = Layout(width='150px')\n",
118+
"x0_box = FloatText(description='x0', layout=txt_layout, value=2.4)\n",
119+
"eta_box = FloatText(description='Learning Rate', \n",
120+
" style={'description_width':'initial'}, \n",
121+
" layout=txt_layout, value=.1)\n",
122+
"\n",
123+
"go_btn = Button(description='GO', button_style='success', layout=Layout(width='50px'))\n",
124+
"reset_btn = Button(description='Reset', button_style='success', layout=Layout(width='100px'))\n",
125+
"\n",
126+
"sol_lbl_tmpl = 'x = {:.4f}'\n",
127+
"sol_lbl = Label()\n",
128+
"# sol_lbl.layout.width = '300px'\n",
129+
"\n",
130+
"# plot of curve and solution\n",
131+
"fig_layout = Layout(width='720px', height='500px')\n",
132+
"fig = plt.figure(layout=fig_layout, title='Gradient Descent', display_toolbar=True)\n",
133+
"fig.pyplot = Toolbar(figure=fig)\n",
134+
"\n",
135+
"curve = plt.plot(x, y, colors=['dodgerblue'], stroke_width=2)\n",
136+
"sol_path = plt.plot([], [], colors=['#ccc'], opacities=[.7])\n",
137+
"sol_points = plt.plot([], [], 'mo', default_size=20)\n",
138+
"\n",
139+
"def optimize():\n",
140+
" f.marks = [curve]\n",
141+
" gradient_descent(x0_box.value, f, df, eta=eta_box.value)\n",
142+
"\n",
143+
"def reset():\n",
144+
" curve.scales['x'].min = .4\n",
145+
" curve.scales['x'].max = 2.5\n",
146+
" \n",
147+
" curve.scales['y'].min = -.5\n",
148+
" curve.scales['y'].max = .4\n",
149+
" sol_path.x = sol_path.y = []\n",
150+
" sol_points.x = sol_points.y = []\n",
151+
" sol_lbl.value = ''\n",
152+
" \n",
153+
"go_btn.on_click(lambda btn: optimize())\n",
154+
"reset_btn.on_click(lambda btn: reset())\n",
155+
"\n",
156+
"final_fig = VBox([fig, fig.pyplot], \n",
157+
" layout=Layout(overflow_x='hidden'))\n",
158+
"HBox([final_fig, VBox([x0_box, eta_box, go_btn, reset_btn, sol_lbl])])"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": null,
164+
"metadata": {},
165+
"outputs": [],
166+
"source": []
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": null,
171+
"metadata": {},
172+
"outputs": [],
173+
"source": []
174+
}
175+
],
176+
"metadata": {
177+
"kernelspec": {
178+
"display_name": "Python 3",
179+
"language": "python",
180+
"name": "python3"
181+
},
182+
"language_info": {
183+
"codemirror_mode": {
184+
"name": "ipython",
185+
"version": 3
186+
},
187+
"file_extension": ".py",
188+
"mimetype": "text/x-python",
189+
"name": "python",
190+
"nbconvert_exporter": "python",
191+
"pygments_lexer": "ipython3",
192+
"version": "3.8.0"
193+
}
194+
},
195+
"nbformat": 4,
196+
"nbformat_minor": 1
197+
}

0 commit comments

Comments
 (0)