Skip to content

Latest commit

 

History

History
125 lines (89 loc) · 3.92 KB

tutorial.LearnerND.rst

File metadata and controls

125 lines (89 loc) · 3.92 KB

Tutorial ~adaptive.LearnerND

Note

Because this documentation consists of static html, the live_plot and live_info widget is not live. Download the notebook in order to see the real behaviour.

The complete source code of this tutorial can be found in :jupyter-downloadtutorial.LearnerND

import adaptive adaptive.notebook_extension(_inline_js=False)

import holoviews as hv import numpy as np

def dynamicmap_to_holomap(dm):

# XXX: change when holoviz/holoviews#3085 # is fixed. vals = {d.name: d.values for d in dm.dimensions() if d.values} return hv.HoloMap(dm.select(**vals))

Besides 1 and 2 dimensional functions, we can also learn N-D functions:  f : N → M, N ≥ 2, M ≥ 1.

Do keep in mind the speed and effectiveness of the learner drops quickly with increasing number of dimensions.

def sphere(xyz):

x, y, z = xyz a = 0.4 return x + z*2 + np.exp(-(x2 + y2 + z2 - 0.752)2/a*4)

learner = adaptive.LearnerND(sphere, bounds=[(-1, 1), (-1, 1), (-1, 1)]) runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 1e-3)

await runner.task # This is not needed in a notebook environment!

runner.live_info()

Let’s plot 2D slices of the 3D function

def plot_cut(x, direction, learner=learner):

cut_mapping = {'XYZ'.index(direction): x} return learner.plot_slice(cut_mapping, n=100)

dm = hv.DynamicMap(plot_cut, kdims=['val', 'direction']) dm = dm.redim.values(val=np.linspace(-1, 1, 11), direction=list('XYZ'))

# In a notebook one would run dm however we want a statically generated # html, so we use a HoloMap to display it here dynamicmap_to_holomap(dm)

Or we can plot 1D slices

%%opts Path {+framewise} def plot_cut(x1, x2, directions, learner=learner): cut_mapping = {'xyz'.index(d): x for d, x in zip(directions, [x1, x2])} return learner.plot_slice(cut_mapping)

dm = hv.DynamicMap(plot_cut, kdims=['v1', 'v2', 'directions']) dm = dm.redim.values(v1=np.linspace(-1, 1, 6), v2=np.linspace(-1, 1, 6), directions=['xy', 'xz', 'yz'])

# In a notebook one would run dm however we want a statically generated # html, so we use a HoloMap to display it here dynamicmap_to_holomap(dm)

The plots show some wobbles while the original function was smooth, this is a result of the fact that the learner chooses points in 3 dimensions and the simplices are not in the same face as we try to interpolate our lines. However, as always, when you sample more points the graph will become gradually smoother.

Using any convex shape as domain

Suppose you do not simply want to sample your function on a square (in 2D) or in a cube (in 3D). The LearnerND supports using a scipy.spatial.ConvexHull as your domain. This is best illustrated in the following example.

Suppose you would like to sample you function in a cube split in half diagonally. You could use the following code as an example:

import scipy

def f(xyz):

x, y, z = xyz return x*4 + y4 + z4 - (x2+y2+z2)*2

# set the bound points, you can change this to be any shape b = [(-1, -1, -1), (-1, 1, -1), (-1, -1, 1), (-1, 1, 1), ( 1, 1, -1), ( 1, -1, -1)]

# you have to convert the points into a scipy.spatial.ConvexHull hull = scipy.spatial.ConvexHull(b)

learner = adaptive.LearnerND(f, hull) adaptive.BlockingRunner(learner, goal=lambda l: l.npoints > 2000)

learner.plot_isosurface(-0.5)