Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
adaptive-paper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Quantum Tinkerer
adaptive-paper
Commits
ccd076a2
Commit
ccd076a2
authored
5 years ago
by
Bas Nijholt
Browse files
Options
Downloads
Patches
Plain Diff
add figure
parent
9db81040
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
figures.ipynb
+236
-0
236 additions, 0 deletions
figures.ipynb
figures/loss_1D.pdf
+0
-0
0 additions, 0 deletions
figures/loss_1D.pdf
with
236 additions
and
0 deletions
figures.ipynb
0 → 100644
+
236
−
0
View file @
ccd076a2
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib\n",
"\n",
"matplotlib.use(\"agg\")\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"\n",
"golden_mean = (np.sqrt(5) - 1) / 2 # Aesthetic ratio\n",
"fig_width_pt = 246.0 # Columnwidth\n",
"inches_per_pt = 1 / 72.27 # Convert pt to inches\n",
"fig_width = fig_width_pt * inches_per_pt\n",
"fig_height = fig_width * golden_mean # height in inches\n",
"fig_size = [fig_width, fig_height]\n",
"\n",
"params = {\n",
" \"backend\": \"ps\",\n",
" \"axes.labelsize\": 13,\n",
" \"font.size\": 13,\n",
" \"legend.fontsize\": 10,\n",
" \"xtick.labelsize\": 10,\n",
" \"ytick.labelsize\": 10,\n",
" \"text.usetex\": True,\n",
" \"figure.figsize\": fig_size,\n",
" \"font.family\": \"serif\",\n",
" \"font.serif\": \"Computer Modern Roman\",\n",
" \"legend.frameon\": True,\n",
" \"savefig.dpi\": 300,\n",
"}\n",
"\n",
"plt.rcParams.update(params)\n",
"plt.rc(\"text.latex\", preamble=[r\"\\usepackage{xfrac}\", r\"\\usepackage{siunitx}\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(1)\n",
"xs = np.sort(np.random.uniform(-1, 1, 3))\n",
"errs = np.abs(np.random.randn(3))\n",
"ys = xs ** 3\n",
"means = lambda x: np.convolve(x, np.ones(2) / 2, mode=\"valid\")\n",
"xs_means = means(xs)\n",
"ys_means = means(ys)\n",
"\n",
"fig, ax = plt.subplots()\n",
"plt.scatter(xs, ys, c=\"k\")\n",
"ax.errorbar(xs, ys, errs, capsize=5, c=\"k\")\n",
"ax.annotate(\n",
" s=r\"$L_{1,2} = \\sqrt{\\Delta x^2 + \\Delta \\bar{y}^2}$\",\n",
" xy=(np.mean([xs[0], xs[1], xs[1]]), np.mean([ys[0], ys[1], ys[1]])),\n",
" xytext=(xs_means[0], ys_means[0] + 1),\n",
" arrowprops=dict(arrowstyle=\"->\"),\n",
" ha=\"center\",\n",
")\n",
"\n",
"for i, (x, y, err) in enumerate(zip(xs, ys, errs)):\n",
" err_str = fr'${{\\sigma}}_{{\\bar {{y}}_{i+1}}}$'\n",
" ax.annotate(\n",
" s=err_str,\n",
" xy=(x, y + err / 2),\n",
" xytext=(x + 0.1, y + err + 0.5),\n",
" arrowprops=dict(arrowstyle=\"->\"),\n",
" ha=\"center\",\n",
" )\n",
"\n",
" ax.annotate(\n",
" s=fr\"$x_{i+1}, \\bar{{y}}_{i+1}$\",\n",
" xy=(x, y),\n",
" xytext=(x + 0.1, y - 0.5),\n",
" arrowprops=dict(arrowstyle=\"->\"),\n",
" ha=\"center\",\n",
" )\n",
"\n",
"\n",
"ax.scatter(xs, ys, c=\"green\", s=5, zorder=5, label=\"more seeds\")\n",
"ax.scatter(xs_means, ys_means, c=\"red\", s=5, zorder=5, label=\"new point\")\n",
"ax.legend()\n",
"\n",
"ax.text(\n",
" x=0.5,\n",
" y=0.0,\n",
" s=(\n",
" r\"$\\textrm{if}\\; \\max{(L_{i,i+1})} > \\textrm{average\\_priority} \\cdot \\max{\\sigma_{\\bar{y}_{i}}} \\rightarrow,\\;\\textrm{add new point}$\"\n",
" \"\\n\"\n",
" r\"$\\textrm{if}\\; \\max{(L_{i,i+1})} < \\textrm{average\\_priority} \\cdot \\max{\\sigma_{\\bar{y}_{i}}} \\rightarrow,\\;\\textrm{add new seeds}$\"\n",
" ),\n",
" horizontalalignment=\"center\",\n",
" verticalalignment=\"center\",\n",
" transform=ax.transAxes,\n",
")\n",
"ax.set_title(\"AverageLearner1D\")\n",
"ax.axis(\"off\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(1)\n",
"xs = np.array([0.1, 0.3, 0.35, 0.45])\n",
"f = lambda x: x**3\n",
"ys = f(xs)\n",
"means = lambda x: np.convolve(x, np.ones(2) / 2, mode=\"valid\")\n",
"xs_means = means(xs)\n",
"ys_means = means(ys)\n",
"\n",
"fig, ax = plt.subplots(figsize=fig_size)\n",
"ax.scatter(xs, ys, c=\"k\")\n",
"ax.plot(xs, ys, c=\"k\")\n",
"# ax.scatter()\n",
"ax.annotate(\n",
" s=r\"$L_{1,2} = \\sqrt{\\Delta x^2 + \\Delta y^2}$\",\n",
" xy=(np.mean([xs[0], xs[1]]), np.mean([ys[0], ys[1]])),\n",
" xytext=(xs[0]+0.05, ys[0] - 0.05),\n",
" arrowprops=dict(arrowstyle=\"->\"),\n",
" ha=\"center\",\n",
" zorder=10,\n",
")\n",
"\n",
"for i, (x, y) in enumerate(zip(xs, ys)):\n",
" sign = [1, -1][i % 2]\n",
" ax.annotate(\n",
" s=fr\"$x_{i+1}, y_{i+1}$\",\n",
" xy=(x, y),\n",
" xytext=(x + 0.01, y + sign * 0.04),\n",
" arrowprops=dict(arrowstyle=\"->\"),\n",
" ha=\"center\",\n",
" )\n",
" \n",
"ax.scatter(xs, ys, c=\"green\", s=5, zorder=5, label=\"existing data\")\n",
"losses = np.hypot(xs[1:] - xs[:-1], ys[1:] - ys[:-1])\n",
"ax.scatter(xs_means, ys_means, c=\"red\", s=300*losses, zorder=8, label=\"candidate points\")\n",
"xs_dense = np.linspace(xs[0], xs[-1], 400)\n",
"ax.plot(xs_dense, f(xs_dense), alpha=0.3, zorder=7, label=\"function\")\n",
"\n",
"ax.legend()\n",
"ax.axis(\"off\")\n",
"plt.savefig(\"figures/loss_1D.pdf\", bbox_inches=\"tight\", transparent=True)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import adaptive\n",
"adaptive.notebook_extension()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def f(x, offset=0.12312):\n",
" a = 0.01\n",
" return x + a**2 / (a**2 + (x - offset)**2)\n",
"\n",
"learner = adaptive.Learner1D(f, bounds=(-1, 1))\n",
"adaptive.runner.simple(learner, goal=lambda l: l.npoints > 100)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"xs, ys = zip(*learner.data.items())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"for i in range(1, len(xs)):\n",
" if i % 10 != 0:\n",
" continue\n",
" alpha = np.linspace(0.2, 1, 101)[i]\n",
" offset = i / len(xs)\n",
" xs_part, ys_part = xs[:i], ys[:i]\n",
" xs_part, ys_part = zip(*sorted(zip(xs_part, ys_part)))\n",
" ax.plot(xs_part, offset + np.array(ys_part), alpha=alpha, c='grey', lw=0.5)\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"xs_part, ys_part"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"language_info": {
"name": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
```
import numpy as np
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
golden_mean = (np.sqrt(5) - 1) / 2 # Aesthetic ratio
fig_width_pt = 246.0 # Columnwidth
inches_per_pt = 1 / 72.27 # Convert pt to inches
fig_width = fig_width_pt * inches_per_pt
fig_height = fig_width * golden_mean # height in inches
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.labelsize": 13,
"font.size": 13,
"legend.fontsize": 10,
"xtick.labelsize": 10,
"ytick.labelsize": 10,
"text.usetex": True,
"figure.figsize": fig_size,
"font.family": "serif",
"font.serif": "Computer Modern Roman",
"legend.frameon": True,
"savefig.dpi": 300,
}
plt.rcParams.update(params)
plt.rc("text.latex", preamble=[r"\usepackage{xfrac}", r"\usepackage{siunitx}"])
```
%% Cell type:code id: tags:
```
np.random.seed(1)
xs = np.sort(np.random.uniform(-1, 1, 3))
errs = np.abs(np.random.randn(3))
ys = xs ** 3
means = lambda x: np.convolve(x, np.ones(2) / 2, mode="valid")
xs_means = means(xs)
ys_means = means(ys)
fig, ax = plt.subplots()
plt.scatter(xs, ys, c="k")
ax.errorbar(xs, ys, errs, capsize=5, c="k")
ax.annotate(
s=r"$L_{1,2} = \sqrt{\Delta x^2 + \Delta \bar{y}^2}$",
xy=(np.mean([xs[0], xs[1], xs[1]]), np.mean([ys[0], ys[1], ys[1]])),
xytext=(xs_means[0], ys_means[0] + 1),
arrowprops=dict(arrowstyle="->"),
ha="center",
)
for i, (x, y, err) in enumerate(zip(xs, ys, errs)):
err_str = fr'${{\sigma}}_{{\bar {{y}}_{i+1}}}$'
ax.annotate(
s=err_str,
xy=(x, y + err / 2),
xytext=(x + 0.1, y + err + 0.5),
arrowprops=dict(arrowstyle="->"),
ha="center",
)
ax.annotate(
s=fr"$x_{i+1}, \bar{{y}}_{i+1}$",
xy=(x, y),
xytext=(x + 0.1, y - 0.5),
arrowprops=dict(arrowstyle="->"),
ha="center",
)
ax.scatter(xs, ys, c="green", s=5, zorder=5, label="more seeds")
ax.scatter(xs_means, ys_means, c="red", s=5, zorder=5, label="new point")
ax.legend()
ax.text(
x=0.5,
y=0.0,
s=(
r"$\textrm{if}\; \max{(L_{i,i+1})} > \textrm{average\_priority} \cdot \max{\sigma_{\bar{y}_{i}}} \rightarrow,\;\textrm{add new point}$"
"\n"
r"$\textrm{if}\; \max{(L_{i,i+1})} < \textrm{average\_priority} \cdot \max{\sigma_{\bar{y}_{i}}} \rightarrow,\;\textrm{add new seeds}$"
),
horizontalalignment="center",
verticalalignment="center",
transform=ax.transAxes,
)
ax.set_title("AverageLearner1D")
ax.axis("off")
plt.show()
```
%% Cell type:code id: tags:
```
np.random.seed(1)
xs = np.array([0.1, 0.3, 0.35, 0.45])
f = lambda x: x**3
ys = f(xs)
means = lambda x: np.convolve(x, np.ones(2) / 2, mode="valid")
xs_means = means(xs)
ys_means = means(ys)
fig, ax = plt.subplots(figsize=fig_size)
ax.scatter(xs, ys, c="k")
ax.plot(xs, ys, c="k")
# ax.scatter()
ax.annotate(
s=r"$L_{1,2} = \sqrt{\Delta x^2 + \Delta y^2}$",
xy=(np.mean([xs[0], xs[1]]), np.mean([ys[0], ys[1]])),
xytext=(xs[0]+0.05, ys[0] - 0.05),
arrowprops=dict(arrowstyle="->"),
ha="center",
zorder=10,
)
for i, (x, y) in enumerate(zip(xs, ys)):
sign = [1, -1][i % 2]
ax.annotate(
s=fr"$x_{i+1}, y_{i+1}$",
xy=(x, y),
xytext=(x + 0.01, y + sign * 0.04),
arrowprops=dict(arrowstyle="->"),
ha="center",
)
ax.scatter(xs, ys, c="green", s=5, zorder=5, label="existing data")
losses = np.hypot(xs[1:] - xs[:-1], ys[1:] - ys[:-1])
ax.scatter(xs_means, ys_means, c="red", s=300*losses, zorder=8, label="candidate points")
xs_dense = np.linspace(xs[0], xs[-1], 400)
ax.plot(xs_dense, f(xs_dense), alpha=0.3, zorder=7, label="function")
ax.legend()
ax.axis("off")
plt.savefig("figures/loss_1D.pdf", bbox_inches="tight", transparent=True)
plt.show()
```
%% Cell type:code id: tags:
```
import adaptive
adaptive.notebook_extension()
```
%% Cell type:code id: tags:
```
def f(x, offset=0.12312):
a = 0.01
return x + a**2 / (a**2 + (x - offset)**2)
learner = adaptive.Learner1D(f, bounds=(-1, 1))
adaptive.runner.simple(learner, goal=lambda l: l.npoints > 100)
```
%% Cell type:code id: tags:
```
xs, ys = zip(*learner.data.items())
```
%% Cell type:code id: tags:
```
fig, ax = plt.subplots()
for i in range(1, len(xs)):
if i % 10 != 0:
continue
alpha = np.linspace(0.2, 1, 101)[i]
offset = i / len(xs)
xs_part, ys_part = xs[:i], ys[:i]
xs_part, ys_part = zip(*sorted(zip(xs_part, ys_part)))
ax.plot(xs_part, offset + np.array(ys_part), alpha=alpha, c='grey', lw=0.5)
plt.show()
```
%% Cell type:code id: tags:
```
xs_part, ys_part
```
%% Cell type:code id: tags:
```
```
This diff is collapsed.
Click to expand it.
figures/loss_1D.pdf
0 → 100644
+
0
−
0
View file @
ccd076a2
File added
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment