Skip to content
Snippets Groups Projects
Commit 9ba68ad1 authored by Joseph Weston's avatar Joseph Weston
Browse files

add logging to runners and add an example to the notebook

parent 2a8ac78e
No related branches found
No related tags found
1 merge request!5Feature/logging
......@@ -17,6 +17,8 @@ class Runner:
goal : callable, optional
The end condition for the calculation. This function must take the
learner as its sole argument, and return True if we should stop.
log : bool, default: False
If True, record the method calls made to the learner by this runner
ioloop : asyncio.AbstractEventLoop, optional
The ioloop in which to run the learning algorithm. If not provided,
the default event loop is used.
......@@ -27,12 +29,17 @@ class Runner:
The underlying task. May be cancelled to stop the runner.
learner : Learner
The underlying learner. May be queried for its state
log : list or None
Record of the method calls made to the learner, in the format
'(method_name, *args)'.
"""
def __init__(self, learner, executor=None, goal=None, *, ioloop=None):
def __init__(self, learner, executor=None, goal=None, *,
log=False, ioloop=None):
self.ioloop = ioloop if ioloop else asyncio.get_event_loop()
self.executor = _ensure_async_executor(executor, self.ioloop)
self.learner = learner
self.log = [] if log else None
if goal is None:
def goal(_):
......@@ -50,6 +57,7 @@ class Runner:
first_completed = asyncio.FIRST_COMPLETED
xs = dict()
done = [None] * _get_executor_ncores(self.executor)
do_log = self.log is not None
if len(done) == 0:
raise RuntimeError('Executor has no workers')
......@@ -58,6 +66,8 @@ class Runner:
while not self.goal(self.learner):
# Launch tasks to replace the ones that completed
# on the last iteration.
if do_log:
self.log.append(('choose_points', len(done)))
for x in self.learner.choose_points(len(done)):
xs[self.executor.submit(self.learner.function, x)] = x
......@@ -69,6 +79,8 @@ class Runner:
for fut in done:
x = xs.pop(fut)
y = await fut
if do_log:
self.log.append(('add_point', x, y))
self.learner.add_point(x, y)
finally:
# cancel any outstanding tasks
......@@ -77,6 +89,21 @@ class Runner:
raise RuntimeError('Some futures remain uncancelled')
def replay_log(learner, log):
"""Apply a sequence of method calls to a learner.
This is useful for debugging runners.
Parameters
----------
learner : learner.BaseLearner
log : list
contains tuples: '(method_name, *args)'.
"""
for method, *args in log:
getattr(learner, method)(*args)
# Internal functionality
class _AsyncExecutor:
......
%% Cell type:markdown id: tags:
# Adaptive
%% Cell type:markdown id: tags:
[`adaptive`](https://gitlab.kwant-project.org/qt/adaptive-evaluation) is a package for adaptively sampling functions with support for parallel evaluation.
This is an introductory notebook that shows some basic use cases.
`adaptive` needs the following packages:
+ Python 3.6
+ holowiews
+ bokeh
%% Cell type:code id: tags:
``` python
import adaptive
adaptive.notebook_extension()
```
%% Cell type:markdown id: tags:
# 1D function learner
%% Cell type:markdown id: tags:
We start with the most common use-case: sampling a 1D function $\ f: ℝ → ℝ$.
We will use the following function, which is a smooth (linear) background with a sharp peak at a random location:
%% Cell type:code id: tags:
``` python
import functools
from random import random
offset = random() - 0.5
def f(x, offset=0, wait=True):
from time import sleep
from random import random
a = 0.01
if wait:
sleep(random())
return x + a**2 / (a**2 + (x - offset)**2)
```
%% Cell type:markdown id: tags:
We start by initializing a 1D "learner", which will suggest points to evaluate, and adapt its suggestions as more and more points are evaluated.
%% Cell type:code id: tags:
``` python
learner = adaptive.learner.Learner1D(f, bounds=(-1.0, 1.0))
```
%% Cell type:markdown id: tags:
Next we create a "runner" that will request points from the learner and evaluate 'f' on them.
By default the runner will evaluate the points in parallel using local processes ([`concurrent.futures.ProcessPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#processpoolexecutor)).
%% Cell type:code id: tags:
``` python
# The end condition is when the "loss" is less than 0.1. In the context of the
# 1D learner this means that we will resolve features in 'func' with width 0.1 or wider.
runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.1)
```
%% Cell type:markdown id: tags:
When instantiated in a Jupyter notebook the runner does its job in the background and does not block the IPython kernel.
We can use this to create a plot that updates as new data arrives:
%% Cell type:code id: tags:
``` python
adaptive.live_plot(runner)
```
%% Cell type:markdown id: tags:
We can now compare the adaptive sampling to a homogeneous sampling with the same number of points:
%% Cell type:code id: tags:
``` python
if not runner.task.done():
raise RuntimeError('Wait for the runner to finish before executing the cells below!')
```
%% Cell type:code id: tags:
``` python
import numpy as np
learner2 = adaptive.learner.Learner1D(f, bounds=(-1.01, 1.0))
xs = np.linspace(-1.0, 1.0, len(learner.data))
learner2.add_data(xs, map(functools.partial(f, wait=False), xs))
learner2.plot()
```
%% Cell type:markdown id: tags:
# Averaging learner
%% Cell type:markdown id: tags:
The next type of learner averages a function until the uncertainty in the average meets some condition.
This is useful for sampling a random variable. The function passed to the learner must formally take a single parameter,
which should be used like a "seed" for the (pseudo-) random variable (although in the current implementation the seed parameter can be ignored by the function).
%% Cell type:code id: tags:
``` python
def g(n):
import random
from time import sleep
sleep(random.random() / 5)
# Properly save and restore the RNG state
state = random.getstate()
random.seed(n)
val = random.gauss(0.5, 1)
random.setstate(state)
return val
```
%% Cell type:code id: tags:
``` python
learner = adaptive.AverageLearner(g, None, 0.03)
runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 1)
adaptive.live_plot(runner)
```
%% Cell type:markdown id: tags:
## Alternative executors
%% Cell type:markdown id: tags:
Often you will want to evaluate the function on some remote computing resources. `adaptive` works out of the box with any framework that implements a [PEP 3148](https://www.python.org/dev/peps/pep-3148/) compliant executor that returns `concurrent.futures.Future` objects.
%% Cell type:markdown id: tags:
### `concurrent.futures`
%% Cell type:markdown id: tags:
By default a runner creates a `ProcessPoolExecutor`, but you can also pass one explicitly e.g. to limit the number of workers:
%% Cell type:code id: tags:
``` python
from concurrent.futures import ProcessPoolExecutor
executor = ProcessPoolExecutor(max_workers=4)
learner = adaptive.learner.Learner1D(f, bounds=(-1, 1))
runner = adaptive.Runner(learner, executor=executor, goal=lambda l: l.loss() < 0.1)
adaptive.live_plot(runner)
```
%% Cell type:markdown id: tags:
### IPyparallel
%% Cell type:code id: tags:
``` python
import ipyparallel
client = ipyparallel.Client()
# f is a closure, so we have to use cloudpickle -- this is independent of 'adaptive'
client[:].use_cloudpickle()
learner = adaptive.learner.Learner1D(f, bounds=(-1, 1))
runner = adaptive.Runner(learner, executor=client, goal=lambda l: l.loss() < 0.1)
adaptive.live_plot(runner)
```
%% Cell type:markdown id: tags:
---
%% Cell type:markdown id: tags:
# Advanced Topics
%% Cell type:markdown id: tags:
## Cancelling a runner
%% Cell type:markdown id: tags:
Sometimes you want to interactively explore a parameter space, and want the function to be evaluated at finer and finer resolution and manually control when the calculation stops.
If no `goal` is provided to a runner then the runner will run until cancelled:
%% Cell type:code id: tags:
``` python
learner = adaptive.learner.Learner1D(f, bounds=(-1.0, 1.0))
runner = adaptive.Runner(learner)
adaptive.live_plot(runner)
```
%% Cell type:code id: tags:
``` python
runner.task.cancel()
```
%% Cell type:markdown id: tags:
## Debugging Problems
%% Cell type:markdown id: tags:
Runners work in the background with respect to the IPython kernel, which makes it convenient, but also means that inspecting errors is more difficult because exceptions will not be raised directly in the notebook. Often the only indication you will have that something has gone wrong is that nothing will be happening.
Let's look at the following example, where the function to be learned will raise an exception 10% of the time.
%% Cell type:code id: tags:
``` python
def will_raise(x):
from random import random
from time import sleep
sleep(random())
if random() < 0.1:
raise RuntimeError('something went wrong!')
return x**2
learner = adaptive.Learner1D(will_raise, (-1, 1))
runner = adaptive.Runner(learner) # without 'goal' the runner will run forever unless cancelled
adaptive.live_plot(runner)
```
%% Cell type:markdown id: tags:
The above runner should continue forever, but we notice that it stops after a few points are evaluated.
First we should check that the runner has really finished:
%% Cell type:code id: tags:
``` python
runner.task.done()
```
%% Cell type:markdown id: tags:
If it has indeed finished then we should check the `result` of the runner. This should be `None` if the runner stopped successfully. If the runner stopped due to an exception then asking for the result will raise the exception with the stack trace:
%% Cell type:code id: tags:
``` python
runner.task.result()
```
%% Cell type:markdown id: tags:
### Logging runners
%% Cell type:markdown id: tags:
Runners do their job in the background, which makes introspection quite cumbersome. One way to inspect runners is to instantiate one with `log=True`:
%% Cell type:code id: tags:
``` python
learner = adaptive.learner.Learner1D(f, bounds=(-1, 1))
runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 0.1,
log=True)
adaptive.live_plot(runner)
```
%% Cell type:markdown id: tags:
This gives a the runner a `log` attribute, which is a list of the `learner` methods that were called, as well as their arguments. This is useful because executors typically execute their tasks in a non-deterministic order.
This can be used with `adaptive.runner.replay_log` to perfom the same set of operations on another runner:
%% Cell type:code id: tags:
``` python
reconstructed_learner = adaptive.learner.Learner1D(f, bounds=(-1, 1))
adaptive.runner.replay_log(reconstructed_learner, runner.log)
```
%% Cell type:code id: tags:
``` python
learner.plot().opts(style=dict(size=6)) * reconstructed_learner.plot()
```
%% Cell type:markdown id: tags:
## Using Runners from a script
%% Cell type:markdown id: tags:
Runners can also be used from a Python script independently of the notebook:
```python
import adaptive
def f(x):
return x
learner = adaptive.Learner1D(f, (-1, 1))
runner = adaptive.Runner(learner, goal=lambda: l: l.loss() < 0.1)
runner.run_sync() # Block until completion.
```
Under the hood the runner uses [`asyncio`](https://docs.python.org/3/library/asyncio.html). You don't need to worry about this most of the time, unless your script uses asyncio itself. If this is the case you should be aware that instantiating a `Runner` schedules a new task on the current event loop, and you can simply
```python
await runner.task
```
inside a coroutine to await completion of the runner.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment