diff --git a/algorithm/.ipynb_checkpoints/interactive-checkpoint.ipynb b/algorithm/.ipynb_checkpoints/interactive-checkpoint.ipynb index fe13a87..90c5375 100644 --- a/algorithm/.ipynb_checkpoints/interactive-checkpoint.ipynb +++ b/algorithm/.ipynb_checkpoints/interactive-checkpoint.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "af1d5646-4b13-4039-9f76-8042bc9dbda3", "metadata": {}, "outputs": [], @@ -12,53 +12,10 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "0a5c51d2-8b18-4143-b6f1-73a909ccb623", "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "6da434bb5623479ea2b213b20390410a", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "VBox(children=(HBox(children=(VBox(children=(Text(value='x**3 - x**(1/2)', description='Expression:', style=De…" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "603367462f0f463ea4594c07f3a20bdd", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "90dd308b94f547c3805ff23d5ad62c6f", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "gd1 = gradient_descent_1d(environ=\"jupyterlab\")" ] @@ -70,19 +27,16 @@ "metadata": {}, "outputs": [], "source": [ - "gd2 = gradient_descent_2d()" + "gd2 = gradient_descent_2d(environ=\"jupyterlab\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "120ecde5-cee1-47c0-b1bc-c99b0cb1098e", + "id": "a05e9830-1643-47c1-a7ea-7c286888cfd4", "metadata": {}, "outputs": [], - "source": [ - "import os\n", - "running_in_voila = os.environ.get('SERVER_SOFTWARE','jupyterlab').startswith('voila')" - ] + "source": [] } ], "metadata": { @@ -101,7 +55,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.4" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/algorithm/interactive.ipynb b/algorithm/interactive.ipynb index fe13a87..ae10f88 100644 --- a/algorithm/interactive.ipynb +++ b/algorithm/interactive.ipynb @@ -19,12 +19,12 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "6da434bb5623479ea2b213b20390410a", + "model_id": "46088f2d27c748c1b8cf9ac9340abe90", "version_major": 2, "version_minor": 0 }, "text/plain": [ - "VBox(children=(HBox(children=(VBox(children=(Text(value='x**3 - x**(1/2)', description='Expression:', style=De…" + "VBox(children=(HBox(children=(VBox(children=(Text(value='x**3 - x**(1/2)', description='Expression:', style=Te…" ] }, "metadata": {}, @@ -33,7 +33,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "603367462f0f463ea4594c07f3a20bdd", + "model_id": "68d79d493fdd445a9854baf7080f018d", "version_major": 2, "version_minor": 0 }, @@ -47,7 +47,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "90dd308b94f547c3805ff23d5ad62c6f", + "model_id": "6f81d5b1dfc240be807ad8108049d130", "version_major": 2, "version_minor": 0 }, @@ -65,24 +65,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "15c6e757-cde3-422b-be7e-3f55b7752142", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "a71627f5371d4f2f91fb0ae93c22d847", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(HBox(children=(VBox(children=(Text(value='(sin(x1) - 2) ** 2 + (sin(x2) - 2) ** 2', description…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "fa8540f9cbcb4eec943319ce3b6d2d42", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "371417e579484f4084a832349172f065", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ - "gd2 = gradient_descent_2d()" + "gd2 = gradient_descent_2d(environ=\"jupyterlab\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "120ecde5-cee1-47c0-b1bc-c99b0cb1098e", + "id": "a05e9830-1643-47c1-a7ea-7c286888cfd4", "metadata": {}, "outputs": [], - "source": [ - "import os\n", - "running_in_voila = os.environ.get('SERVER_SOFTWARE','jupyterlab').startswith('voila')" - ] + "source": [] } ], "metadata": { @@ -101,7 +141,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.4" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/algorithm/optimization/.ipynb_checkpoints/gd-checkpoint.py b/algorithm/optimization/.ipynb_checkpoints/gd-checkpoint.py index a3f858a..c6df601 100644 --- a/algorithm/optimization/.ipynb_checkpoints/gd-checkpoint.py +++ b/algorithm/optimization/.ipynb_checkpoints/gd-checkpoint.py @@ -26,7 +26,7 @@ class gradient_descent_1d(object): description="Startpoint:", style={'description_width': 'initial'}) self.wg_lr = widgets.FloatText(value="1e-1", - description="learning rate:", + description="step size:", style={'description_width': 'initial'}) self.wg_epsilon = widgets.FloatText(value="1e-5", description="criterion:", @@ -109,7 +109,7 @@ class gradient_descent_2d(object): self.wg_x0 = widgets.Text(value="5,5", description="Startpoint:") self.wg_lr = widgets.FloatText(value="1e-1", - description="learning rate:") + description="step size:") self.wg_epsilon = widgets.FloatText(value="1e-5", description="criterion:") self.wg_max_iter = widgets.IntText(value="1000", @@ -182,19 +182,21 @@ class gradient_descent_2d(object): line_marker=dict(color="#de1032", width=5) fig.add_traces(go.Scatter3d(x=None, y=None, z=None, mode='lines+markers', line={"color":"#de1032", "width":5})) - frames = [go.Frame(data= [go.Scatter3d(x=np.array(self.xn_list)[:k,0], y=np.array(self.xn_list)[:k,1],z=f_xn)],traces= [1],name=f'frame{k+2}')for k in range(len(f_xn))] + frames = [go.Frame(data= [go.Scatter3d(x=np.array(self.xn_list)[:k,0], + y=np.array(self.xn_list)[:k,1], + z=f_xn)],traces= [1], + name=f'frame{k+2}')for k in range(len(f_xn))] + fig.update(frames=frames) - fig.update_layout(updatemenus=[dict(type="buttons", - buttons=[dict(label="Play", - method="animate", - args=[None])])]) - fig.update_layout( - scene = { - "xaxis": {"nticks": 20}, - "zaxis": {"nticks": 4}, - 'camera_eye': {"x": 0, "y": -1, "z": 0.5}, - "aspectratio": {"x": 1, "y": 1, "z": 0.2} - }) + fig.update_layout(updatemenus=[dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None]), + dict(label="Pause", method="animate", args= [[None], + dict(mode='immediate', + transition= { 'duration': 0 }, + frame= dict( redraw=True, duration=0))]) + ])]) + + fig.update_layout(margin=dict(l=0, r=0, b=0, t=0)) + # define slider fig.show() diff --git a/algorithm/optimization/__pycache__/gd.cpython-39.pyc b/algorithm/optimization/__pycache__/gd.cpython-39.pyc index 6369e4b..8c81221 100644 Binary files a/algorithm/optimization/__pycache__/gd.cpython-39.pyc and b/algorithm/optimization/__pycache__/gd.cpython-39.pyc differ diff --git a/algorithm/optimization/gd.py b/algorithm/optimization/gd.py index a3f858a..c6df601 100644 --- a/algorithm/optimization/gd.py +++ b/algorithm/optimization/gd.py @@ -26,7 +26,7 @@ class gradient_descent_1d(object): description="Startpoint:", style={'description_width': 'initial'}) self.wg_lr = widgets.FloatText(value="1e-1", - description="learning rate:", + description="step size:", style={'description_width': 'initial'}) self.wg_epsilon = widgets.FloatText(value="1e-5", description="criterion:", @@ -109,7 +109,7 @@ class gradient_descent_2d(object): self.wg_x0 = widgets.Text(value="5,5", description="Startpoint:") self.wg_lr = widgets.FloatText(value="1e-1", - description="learning rate:") + description="step size:") self.wg_epsilon = widgets.FloatText(value="1e-5", description="criterion:") self.wg_max_iter = widgets.IntText(value="1000", @@ -182,19 +182,21 @@ class gradient_descent_2d(object): line_marker=dict(color="#de1032", width=5) fig.add_traces(go.Scatter3d(x=None, y=None, z=None, mode='lines+markers', line={"color":"#de1032", "width":5})) - frames = [go.Frame(data= [go.Scatter3d(x=np.array(self.xn_list)[:k,0], y=np.array(self.xn_list)[:k,1],z=f_xn)],traces= [1],name=f'frame{k+2}')for k in range(len(f_xn))] + frames = [go.Frame(data= [go.Scatter3d(x=np.array(self.xn_list)[:k,0], + y=np.array(self.xn_list)[:k,1], + z=f_xn)],traces= [1], + name=f'frame{k+2}')for k in range(len(f_xn))] + fig.update(frames=frames) - fig.update_layout(updatemenus=[dict(type="buttons", - buttons=[dict(label="Play", - method="animate", - args=[None])])]) - fig.update_layout( - scene = { - "xaxis": {"nticks": 20}, - "zaxis": {"nticks": 4}, - 'camera_eye': {"x": 0, "y": -1, "z": 0.5}, - "aspectratio": {"x": 1, "y": 1, "z": 0.2} - }) + fig.update_layout(updatemenus=[dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None]), + dict(label="Pause", method="animate", args= [[None], + dict(mode='immediate', + transition= { 'duration': 0 }, + frame= dict( redraw=True, duration=0))]) + ])]) + + fig.update_layout(margin=dict(l=0, r=0, b=0, t=0)) + # define slider fig.show() diff --git a/algorithm/需求.txt b/algorithm/需求.txt new file mode 100644 index 0000000..68465d8 --- /dev/null +++ b/algorithm/需求.txt @@ -0,0 +1,5 @@ +1. 动画可以随时暂停 - 进度条 [0] ## plotly Silder +2. 下拉栏 - 典型函数 + custom function ## ipywidgets.SlectionSlider +3. 判断 n+1 和 n 是否为同一个函数,若相同则在同一个画布上画图 +4. **自定义方向+步长** +5. 输入点坐标求得 gradient -> tangent plane -> 法线 \ No newline at end of file