Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Adaptive"
]
},
{
"cell_type": "code",
"execution_count": null,
"import adaptive\n",
"def func(x, wait=True):\n",
" \"\"\"Function with a sharp peak on a smooth background\"\"\"\n",
" import numpy as np\n",
" from time import sleep\n",
" from random import randint\n",
"\n",
" x = np.asarray(x)\n",
]
},
{
"cell_type": "markdown",
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"learner = adaptive.learner.Learner1D(func, bounds=(-1.01, 1.0))\n",
"runner = adaptive.Runner(learner, goal=lambda l: l.loss(real=True) < 0.01)\n",
"adaptive.live_plot(runner)"
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# Same function evaluated on homogeneous grid with same amount of points\n",
"from functools import partial\n",
"import numpy as np\n",
"learner2 = adaptive.learner.Learner1D(func, bounds=(-1.01, 1.0))\n",
"xs = np.linspace(-1.01, 1.0, len(learner.data))\n",
"learner2.add_data(xs, map(partial(func, wait=False), xs))\n",
"learner2.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"import ipyparallel\n",
"\n",
"client = ipyparallel.Client()\n",
"\n",
"# Initialize the learner\n",
"learner = adaptive.learner.Learner1D(func)\n",
"runner = adaptive.Runner(learner, client, goal=lambda l: l.loss() < 0.1)\n",
"adaptive.live_plot(runner)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 0D Learner"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"def func(x):\n",
" import random\n",
" import numpy as np\n",
" from time import sleep\n",
" sleep(np.random.randint(0, 2))\n",
" return random.gauss(0.05, 1)\n",
"\n",
"learner = adaptive.learner.AverageLearner(func, None, 0.1)\n",
"runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 1)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"language": "python",
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
}
},
"nbformat": 4,
"nbformat_minor": 1
}