From 4a3f1f38f456e6379faadbe77f0b390103ee627f Mon Sep 17 00:00:00 2001 From: Liu Terence Date: Mon, 7 Nov 2022 20:36:03 +0800 Subject: [PATCH] add normal --- algorithm/lecture.ipynb | 272 +++++++++++++++++++---------- algorithm/optimization/gradient.py | 21 ++- 2 files changed, 192 insertions(+), 101 deletions(-) diff --git a/algorithm/lecture.ipynb b/algorithm/lecture.ipynb index bf8de0c..e85e7fc 100644 --- a/algorithm/lecture.ipynb +++ b/algorithm/lecture.ipynb @@ -24,7 +24,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "047719f4c6674039838c1b45b32bb5b4", + "model_id": "86e09e87d3d243f0af56049a842d6698", "version_major": 2, "version_minor": 0 }, @@ -38,7 +38,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "f6602e1e61e04f58bbcfa2f4325a814b", + "model_id": "4679ad18b0a843a3ac5c26c2d24e487a", "version_major": 2, "version_minor": 0 }, @@ -52,7 +52,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "564ae8a816f64bbd90c3b6b961b18706", + "model_id": "c6b74ac5faf745c987818905702c6727", "version_major": 2, "version_minor": 0 }, @@ -66,7 +66,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 2, @@ -87,7 +87,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "f3047bfdf60a41e69222e33b1fe216ef", + "model_id": "94c8fcd7d47345f39693929fc45b99da", "version_major": 2, "version_minor": 0 }, @@ -101,7 +101,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "014093558f9141699a4ea4dfe285afeb", + "model_id": "423bc193ec53442990eed7db0806f14b", "version_major": 2, "version_minor": 0 }, @@ -115,7 +115,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "0c7fc5eac02345af8210d61c64ec1d0b", + "model_id": "8360d028706b474dbc4149bb2b068f42", "version_major": 2, "version_minor": 0 }, @@ -133,70 +133,201 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "79f3577e-1e5a-46d8-a4d7-c1a45dcaaa07", - "metadata": {}, - "outputs": [], - "source": [ - "xrange = np.linspace(np.array(a.xn_list)-2, np.array(a.xn_list)+2, 10)\n", - "xrange[0]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "47c012d9-2544-4f2e-8d1d-aec2ae03551a", - "metadata": {}, - "outputs": [], - "source": [ - "np.array(a.xn_list)" - ] - }, - { - "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "7c589589", "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "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" + } + ], "source": [ "funcPlot2d(environ=\"jupyterlab\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "f28bd167", "metadata": { "slideshow": { "slide_type": "subslide" } }, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "944932c7e971408fb0b9fd55db5ef8ef", + "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": "0a891ac310934c5a87ebd3b5823280f4", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "e5e92badf1314caead45d0a4449b4f86", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "cda4607f74174db99302cf283ac433ff", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "gd2d(environ=\"jupyterlab\")" ] }, { "cell_type": "code", - "execution_count": null, - "id": "8940dc84-f52a-42a3-bf60-b8d50dd19620", - "metadata": {}, - "outputs": [], - "source": [ - "gradient = np.array([diff(expr, x1).subs(x1, xn[0]).subs(x2, xn[1]), \n", - " diff(expr, x2).subs(x1, xn[0]).subs(x2, xn[1])], dtype=float)" - ] - }, - { - "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "d5651fc9-3fcd-4e91-8d3c-04897da1ea02", "metadata": {}, - "outputs": [], + "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" + } + ], "source": [ "from optimization.gradient import *\n", "a = gd2d_compete(environ=\"jupyterlab\")" @@ -205,54 +336,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3ce3b0fa-5813-49dd-90de-b28c5d3faf46", - "metadata": {}, - "outputs": [], - "source": [ - "expr = a.wg_expr.value" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "93a54c5c-02f4-47ed-90cb-ef348d1333be", - "metadata": {}, - "outputs": [], - "source": [ - "x = symbols(\"x\")\n", - "expr = sympify(a.wg_expr.value)\n", - "f = lambdify(x, sympify(expr), \"numpy\")\n", - "f_xn = f(np.array(a.xn_list))\n", - "\n", - "xrange = np.linspace(np.array(a.xn_list)[0]-1, np.array(a.xn_list)[0]+1, 10)\n", - "tangent_line = a.df_list[0] * (x - np.array(a.xn_list[0])) + f_xn[0]\n", - "lambdify(x, tangent_line)(xrange)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "26e9ef27-5dcb-4bb3-8c30-301bc7303dcd", - "metadata": {}, - "outputs": [], - "source": [ - "a.df_list[0] * (x - np.array(a.xn_list[0])) + f_xn[0]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "40ad9639-89fd-4296-8d5c-08f7476546d7", - "metadata": {}, - "outputs": [], - "source": [ - "a.df_list[0]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "00387a2e-f4e1-431d-8b7f-e2b4b75679ab", + "id": "f0c08039-6581-4b40-9e08-df40f8395e2b", "metadata": {}, "outputs": [], "source": [] @@ -275,7 +359,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.7.13" } }, "nbformat": 4, diff --git a/algorithm/optimization/gradient.py b/algorithm/optimization/gradient.py index 4c442a3..a5741e0 100644 --- a/algorithm/optimization/gradient.py +++ b/algorithm/optimization/gradient.py @@ -46,7 +46,7 @@ class gd_1d(object): self.wg_max_iter = widgets.IntText(value="1000", description="max iteration", style={'description_width': 'initial'}) - self.wg_x_range = widgets.Text(value="-5,5", + self.wg_x_range = widgets.Text(value="-2,5", description="X-axis range", style={"description_width": "initial"}) @@ -105,11 +105,15 @@ class gd_1d(object): f_xn = f(np.array(self.xn_list)) 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) 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) tangent_y.append(lambdify(x, tangent_line)(xrange)) + normal_x.append(xrange) + normal_y.append(lambdify(x, normal_line)(xrange)) fig = go.Figure() #fig.add_scatter(x=xx1, y=fx) @@ -117,13 +121,16 @@ class gd_1d(object): 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", 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=tangent_x[k], y=tangent_y[k])], - traces= [0, 1, 2]) for k in range(len(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 - fig.update_layout(height=800, updatemenus=[dict(type="buttons",buttons=[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=1000))]) + 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.show() @@ -362,12 +369,12 @@ class gd2d_compete(object): #TODO: compute gradient fig = make_subplots(rows=1, cols=1, specs=[[{'type': 'surface'}]]) - fig.add_trace(go.Surface(x=xx1, y=xx2, z=fx), row=1, col=1) + fig.add_trace(go.Surface(contours = {"x": {"show": True}, "y":{"show": True}, "z":{"show": True}}, x=xx1, y=xx2, z=fx, opacity=0.8), row=1, col=1) fig.add_trace(go.Scatter3d(x=np.array(self.xn_p0_list)[:, 0], y=np.array(self.xn_p0_list)[:, 1], z=fx_p0, name="candidate 1", mode="lines+markers", marker=dict(size=5, color="green")), row=1, col=1) fig.add_trace(go.Scatter3d(x=np.array(self.xn_p1_list)[:, 0], y=np.array(self.xn_p1_list)[:, 1], z=fx_p1, name="candidate 2", mode="lines+markers", marker=dict(size=5, color="blue")), row=1, col=1) - frames = [go.Frame(data = [go.Surface(visible=True, showscale=False, opacity=0.6), + frames = [go.Frame(data = [go.Surface(visible=True, showscale=False, opacity=0.8), go.Scatter3d(x=np.array(self.xn_p0_list)[:self.timer, 0], y=np.array(self.xn_p0_list)[:self.timer, 1], z=fx_p0), go.Scatter3d(x=np.array(self.xn_p1_list)[:self.timer, 0], y=np.array(self.xn_p1_list)[:self.timer, 1], z=fx_p1)], traces=[0,1,2])]