From 6d94f93a06d3a88748918687cfd5ecdb799d624f Mon Sep 17 00:00:00 2001 From: Liu Terence Date: Mon, 7 Nov 2022 21:03:45 +0800 Subject: [PATCH] update --- algorithm/lecture.ipynb | 196 +++++------------------------ algorithm/optimization/gradient.py | 29 +++-- 2 files changed, 45 insertions(+), 180 deletions(-) diff --git a/algorithm/lecture.ipynb b/algorithm/lecture.ipynb index e85e7fc..9f4116f 100644 --- a/algorithm/lecture.ipynb +++ b/algorithm/lecture.ipynb @@ -13,67 +13,14 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "302d9c60", "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "86e09e87d3d243f0af56049a842d6698", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "VBox(children=(HBox(children=(Text(value='(x - 2)**2 + 3', description='Expression:', style=TextStyle(descript…" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "4679ad18b0a843a3ac5c26c2d24e487a", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "c6b74ac5faf745c987818905702c6727", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "funcPlot1d(environ=\"jupyterlab\")" ] @@ -87,7 +34,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "94c8fcd7d47345f39693929fc45b99da", + "model_id": "776f68cc5732452382ee39d4ac337bba", "version_major": 2, "version_minor": 0 }, @@ -101,7 +48,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "423bc193ec53442990eed7db0806f14b", + "model_id": "0032d539590641b484466da05a125e19", "version_major": 2, "version_minor": 0 }, @@ -115,7 +62,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "8360d028706b474dbc4149bb2b068f42", + "model_id": "cdc7760b287b4a7881f0c2288ddcb0af", "version_major": 2, "version_minor": 0 }, @@ -125,82 +72,39 @@ }, "metadata": {}, "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "a = gd_1d(environ=\"jupyterlab\")" + "gd_1d(environ=\"jupyterlab\")" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "7c589589", "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "30975d9af00044e7b57d027118c6c640", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "VBox(children=(HBox(children=(Text(value='(1 - 8 * x1 + 7 * x1^2 - (7/3) * x1^3 + (1/4) * x1^4) * x2^2 * E^(-x…" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "062e1315bcb94b6a80f29a07988b4c80", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "fbc96b978cc74fa4be00db2472e013bc", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "funcPlot2d(environ=\"jupyterlab\")" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 2, "id": "f28bd167", "metadata": { "slideshow": { @@ -211,7 +115,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "944932c7e971408fb0b9fd55db5ef8ef", + "model_id": "3ad25604b04c4023a838429d3ceccbaa", "version_major": 2, "version_minor": 0 }, @@ -225,7 +129,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "0a891ac310934c5a87ebd3b5823280f4", + "model_id": "d913b214c8a54e1897eb161fdc41d04f", "version_major": 2, "version_minor": 0 }, @@ -239,7 +143,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "e5e92badf1314caead45d0a4449b4f86", + "model_id": "39765310aa524308a29842b1770f7f3c", "version_major": 2, "version_minor": 0 }, @@ -253,7 +157,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "cda4607f74174db99302cf283ac433ff", + "model_id": "6d9886e39bd44ab39269fb3cd8e27af0", "version_major": 2, "version_minor": 0 }, @@ -267,10 +171,10 @@ { "data": { "text/plain": [ - "" + "" ] }, - "execution_count": 5, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -281,56 +185,12 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "d5651fc9-3fcd-4e91-8d3c-04897da1ea02", "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "1f5790f38fa84928ab10f07d6b58a6ba", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "VBox(children=(HBox(children=(Dropdown(options=(('(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x…" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "f97ea7fc5f23421a87f1d01f00454fdc", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "379c84bb6ef54e8bbf679357ae5efada", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ - "from optimization.gradient import *\n", - "a = gd2d_compete(environ=\"jupyterlab\")" + "gd2d_compete(environ=\"jupyterlab\")" ] }, { diff --git a/algorithm/optimization/gradient.py b/algorithm/optimization/gradient.py index a5741e0..b2743e3 100644 --- a/algorithm/optimization/gradient.py +++ b/algorithm/optimization/gradient.py @@ -35,7 +35,7 @@ class gd_1d(object): description="Expression:", style={'description_width': 'initial'}) self.wg_x0 = widgets.FloatText(value="2", - description="Startpoint:", + description="Startpoint(x):", style={'description_width': 'initial'}) self.wg_lr = widgets.FloatText(value="1e-1", description="step size:", @@ -44,10 +44,10 @@ class gd_1d(object): description="criterion:", style={'description_width': 'initial'}) self.wg_max_iter = widgets.IntText(value="1000", - description="max iteration", + description="max iteration:", style={'description_width': 'initial'}) self.wg_x_range = widgets.Text(value="-2,5", - description="X-axis range", + description="X-axis range:", style={"description_width": "initial"}) self.button_compute = widgets.Button(description="Compute") @@ -107,7 +107,7 @@ class gd_1d(object): tangent_x, tangent_y = [], [] normal_x, normal_y = [], [] for i in range(0, len(f_xn)): - xrange = np.linspace(np.array(self.xn_list)[i]-0.5, np.array(self.xn_list)[i]+0.5, 10) + xrange = np.linspace(np.array(self.xn_list)[i]-0.25, np.array(self.xn_list)[i]+0.25, 10) tangent_line = self.df_list[i] * (x - np.array(self.xn_list)[i]) + f_xn[i] normal_line = (-1/(self.df_list[i]))*(x - np.array(self.xn_list)[i]) + f_xn[i] tangent_x.append(xrange) @@ -119,18 +119,19 @@ class gd_1d(object): #fig.add_scatter(x=xx1, y=fx) fig.add_trace(go.Scatter(x=xx1, y=fx)) - fig.add_traces(go.Scatter(x=None, y=None, mode="lines + markers", line={"color":"#de1032", "width":3, 'dash': 'dash'})) + fig.add_traces(go.Scatter(x=None, y=None, mode="lines + markers", marker=dict(size=10), line={"color":"#de1032", "width":3, 'dash': 'dash'})) fig.add_traces(go.Scatter(x=None, y=None, mode="lines", line={"color":"#debc10", "width":3, 'dash': 'dash'})) fig.add_traces(go.Scatter(x=None, y=None, mode="lines", line={"color":"#de3210", "width":3, 'dash': 'dash'})) frames = [go.Frame(data=[go.Scatter(x=xx1, y=fx), - go.Scatter(x=np.array(self.xn_list)[:k], y=f_xn), + go.Scatter(x=np.array(self.xn_list)[:k+1], y=f_xn), go.Scatter(x=tangent_x[k], y=tangent_y[k]), go.Scatter(x=normal_x[k], y=normal_y[k])], traces= [0, 1, 2, 3]) for k in range(len(f_xn))] fig.frames = frames - button_play = dict(label="Play", method="animate", args=[None, dict(fromcurrent=True, transition=dict(duration=0), frame=dict(redraw=True, duration=1000))]) + button_play = dict(label="Play", method="animate", args=[None, dict(fromcurrent=True, transition=dict(duration=0), frame=dict(redraw=True, duration=2000))]) button_pause = dict(label="Pause", method="animate", args=[[None], dict(fromcurrent=True, mode='immediate', transition={'duration': 0}, frame=dict(redraw=True, duration=0))]) - fig.update_layout(height=800, updatemenus=[dict(type="buttons",buttons=[button_play, button_pause, button_tangent])]) + fig.update_layout(height=800, updatemenus=[dict(type="buttons",buttons=[button_play, button_pause])]) + fig.update_yaxes(scaleanchor = "x", scaleratio = 1) fig.show() @@ -244,11 +245,11 @@ class gd2d(object): fig.add_trace(go.Scatter3d(x=None, y=None, z=None, marker=dict(size=5)), row=1, col=2) fig.add_trace(go.Scatter3d(x=None, y=None, z=None, marker=dict(size=5)), row=1, col=2) frames = [go.Frame(data=[go.Surface(visible=True, showscale=False, opacity=0.8), - go.Scatter3d(x=np.array(self.xn_list)[:k,0], y=np.array(self.xn_list)[:k,1], z=f_xn, marker=dict(size=5), line={"color":"#10dedb", "width":3, 'dash': 'dash'}), + go.Scatter3d(x=np.array(self.xn_list)[:k,0], y=np.array(self.xn_list)[:k,1], z=f_xn, marker=dict(size=5), line={"color":"blue", "width":3, 'dash': 'dash'}), go.Surface(visible=False, x=xx1_tangent, y=xx2_tangent, z=z[k]), go.Surface(visible=True, showscale=False, opacity=0.8), - go.Scatter3d(x=np.array(self.xn_list)[:k, 0], y=np.array(self.xn_list)[:k, 1], z=f_xn, line={"color":"#10dedb", "width":3, 'dash': 'dash'}), - go.Scatter3d(x=np.array(self.xn_list)[:k, 0].flatten(), y=np.array(self.xn_list)[:k, 1].flatten(), z=z_offset.flatten(), marker=dict(size=5), line={"color":"#58de10", "width":3, 'dash': 'dash'})], + go.Scatter3d(x=np.array(self.xn_list)[:k, 0], y=np.array(self.xn_list)[:k, 1], z=f_xn, line={"color":"blue", "width":3, 'dash': 'dash'}), + go.Scatter3d(x=np.array(self.xn_list)[:k, 0].flatten(), y=np.array(self.xn_list)[:k, 1].flatten(), z=z_offset.flatten(), marker=dict(size=5), line={"color":"green", "width":3, 'dash': 'dash'})], traces=[0, 1, 2, 3, 4, 5]) for k in range(len(f_xn))] fig.frames = frames self.fig_frames = frames @@ -262,6 +263,7 @@ class gd2d(object): yaxis_visible=True, zaxis_visible=True) fig.update_scenes(camera_projection_type = "orthographic") + fig.update_yaxes(scaleanchor = "x", scaleratio = 1) fig.show() def plot_contour(self, *args): @@ -339,6 +341,7 @@ class gd2d_compete(object): def compute(self, *args): with self.compute_output: expr = sympify(self.wg_expr.value) + x1, x2 = symbols("x1 x2") if self.timer == 0: self.xn_p0_list, self.xn_p1_list = [], [] x0 = np.array(self.wg_x0.value.split(","), dtype=float) @@ -352,8 +355,10 @@ class gd2d_compete(object): x0_p1 = self.xn_p1_list[self.timer-1] + self.wg_lr.value * direction_p1 self.xn_p0_list.append(x0_p0) self.xn_p1_list.append(x0_p1) + f = lambdify((x1, x2), expr, "numpy") + f_xn = f(np.array([x0_p0, x0_p1])[:, 0], np.array([x0_p0, x0_p1])[:, 1]) clear_output(wait=True) - print("a1({}): {}, a2({}): {}".format(self.timer, x0_p0, self.timer, x0_p1)) + print("a1({}): {}, a2({}): {}".format(self.timer, np.append(x0_p0, f_xn[0]), self.timer,np.append(x0_p1, f_xn[1]))) def plot(self, *args): with self.plot_output: