adaptive.Learner2D¶
Learner2D
¶
Learner2D(function: Callable, bounds: tuple[tuple[Real, Real], tuple[Real, Real]], loss_per_triangle: Callable | None = None)
Bases: BaseLearner
Learns and predicts a function 'f: ℝ^2 → ℝ^N'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
callable
|
The function to learn. Must take a tuple of two real parameters and return a real number. |
required |
bounds
|
list of 2-tuples
|
A list |
required |
loss_per_triangle
|
callable
|
A function that returns the loss for every triangle. If not provided, then a default is used, which uses the deviation from a linear estimate, as well as triangle area, to determine the loss. See the notes for more details. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
data |
dict
|
Sampled points and values. |
pending_points |
set
|
Points that still have to be evaluated and are currently
interpolated, see |
stack_size |
int, default: 10
|
The size of the new candidate points stack. Set it to 1
to recalculate the best points at each call to |
aspect_ratio |
float, int, default: 1
|
Average ratio of |
Notes
Adapted from an initial implementation by Pauli Virtanen.
The sample points are chosen by estimating the point where the linear and cubic interpolants based on the existing points have maximal disagreement. This point is then taken as the next point to be sampled.
In practice, this sampling protocol results to sparser sampling of smooth regions, and denser sampling of regions where the function changes rapidly, which is useful if the function is expensive to compute.
This sampling procedure is not extremely fast, so to benefit from it, your function needs to be slow enough to compute.
loss_per_triangle takes a single parameter, ip, which is a
scipy.interpolate.LinearNDInterpolator. You can use the
undocumented attributes tri and values of ip to get a
scipy.spatial.Delaunay and a vector of function values.
These can be used to compute the loss. The functions
areas and
deviations to calculate the
areas and deviations from a linear interpolation
over each triangle.
Source code in adaptive/learner/learner2D.py
bounds
instance-attribute
¶
vdim
property
¶
vdim: int
Length of the output of learner.function.
If the output is unsized (when it's a scalar)
then vdim = 1.
As long as no data is known vdim = 1.
to_numpy
¶
Data as NumPy array of size (npoints, 3) if learner.function returns a scalar
and (npoints, 2+vdim) if learner.function returns a vector of length vdim.
Source code in adaptive/learner/learner2D.py
to_dataframe
¶
to_dataframe(with_default_function_args: bool = True, function_prefix: str = 'function.', x_name: str = 'x', y_name: str = 'y', z_name: str = 'z') -> DataFrame
Return the data as a pandas.DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_default_function_args
|
bool
|
Include the |
True
|
function_prefix
|
str
|
Prefix to the |
'function.'
|
seed_name
|
str
|
Name of the seed parameter, by default "seed" |
required |
x_name
|
str
|
Name of the input x value, by default "x" |
'x'
|
y_name
|
str
|
Name of the input y value, by default "y" |
'y'
|
z_name
|
str
|
Name of the output value, by default "z" |
'z'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in adaptive/learner/learner2D.py
load_dataframe
¶
load_dataframe(df: DataFrame, with_default_function_args: bool = True, function_prefix: str = 'function.', x_name: str = 'x', y_name: str = 'y', z_name: str = 'z')
Load data from a pandas.DataFrame.
If with_default_function_args is True, then learner.function's
default arguments are set (using functools.partial) from the values
in the pandas.DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The data to load. |
required |
with_default_function_args
|
bool
|
The |
True
|
function_prefix
|
str
|
The |
'function.'
|
x_name
|
str
|
The |
'x'
|
y_name
|
str
|
The |
'y'
|
z_name
|
str
|
The |
'z'
|
Source code in adaptive/learner/learner2D.py
interpolated_on_grid
¶
Get the interpolated data on a grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of points in x and y. If None (default) this number is evaluated by looking at the size of the smallest triangle. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
xs |
1D numpy.ndarray
|
|
ys |
1D numpy.ndarray
|
|
interpolated_on_grid |
2D numpy.ndarray
|
|
Source code in adaptive/learner/learner2D.py
ip
¶
ip() -> LinearNDInterpolator
Deprecated, use self.interpolator(scaled=True)
Source code in adaptive/learner/learner2D.py
interpolator
¶
interpolator(*, scaled: bool = False) -> LinearNDInterpolator
A scipy.interpolate.LinearNDInterpolator instance
containing the learner's data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scaled
|
bool
|
Use True if all points are inside the
unit-square [(-0.5, 0.5), (-0.5, 0.5)] or False if
the data points are inside the |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
interpolator |
LinearNDInterpolator
|
|
Examples:
>>> xs, ys = [np.linspace(*b, num=100) for b in learner.bounds]
>>> ip = learner.interpolator()
>>> zs = ip(xs[:, None], ys[None, :])
Source code in adaptive/learner/learner2D.py
inside_bounds
¶
tell
¶
Source code in adaptive/learner/learner2D.py
tell_pending
¶
Source code in adaptive/learner/learner2D.py
ask
¶
Source code in adaptive/learner/learner2D.py
loss
¶
Source code in adaptive/learner/learner2D.py
remove_unfinished
¶
plot
¶
Plot the Learner2D's current state.
This plot function interpolates the data on a regular grid. The gridspacing is evaluated by checking the size of the smallest triangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of points in x and y. If None (default) this number is evaluated by looking at the size of the smallest triangle. |
None
|
tri_alpha
|
float
|
The opacity |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
plot |
Overlay or HoloMap
|
A |
Notes
The plot object that is returned if learner.function returns a
vector cannot be used with the live_plotting functionality.
Source code in adaptive/learner/learner2D.py
Custom loss functions¶
default_loss
¶
default_loss(ip: LinearNDInterpolator) -> ndarray
Loss function that combines deviations and areas of the triangles.
Works with Learner2D only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ip
|
scipy.interpolate.LinearNDInterpolator instance
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
losses |
ndarray
|
Loss per triangle in |
Source code in adaptive/learner/learner2D.py
minimize_triangle_surface_loss
¶
minimize_triangle_surface_loss(ip: LinearNDInterpolator) -> ndarray
Loss function that is similar to the distance loss function in the
Learner1D. The loss is the area spanned by the 3D
vectors of the vertices.
Works with Learner2D only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ip
|
scipy.interpolate.LinearNDInterpolator instance
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
losses |
ndarray
|
Loss per triangle in |
Examples:
>>> from adaptive.learner.learner2D import minimize_triangle_surface_loss
>>> def f(xy):
... x, y = xy
... return x**2 + y**2
>>>
>>> learner = adaptive.Learner2D(f, bounds=[(-1, -1), (1, 1)],
... loss_per_triangle=minimize_triangle_surface_loss)
>>>
Source code in adaptive/learner/learner2D.py
uniform_loss
¶
uniform_loss(ip: LinearNDInterpolator) -> ndarray
Loss function that samples the domain uniformly.
Works with Learner2D only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ip
|
scipy.interpolate.LinearNDInterpolator instance
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
losses |
ndarray
|
Loss per triangle in |
Examples:
>>> from adaptive.learner.learner2D import uniform_loss
>>> def f(xy):
... x, y = xy
... return x**2 + y**2
>>>
>>> learner = adaptive.Learner2D(
... f,
... bounds=[(-1, -1), (1, 1)],
... loss_per_triangle=uniform_loss,
... )
>>>
Source code in adaptive/learner/learner2D.py
resolution_loss_function
¶
resolution_loss_function(min_distance: float = 0, max_distance: float = 1) -> Callable[[LinearNDInterpolator], ndarray]
Loss function that is similar to the default_loss function, but you
can set the maximimum and minimum size of a triangle.
Works with Learner2D only.
The arguments min_distance and max_distance should be in between 0 and 1
because the total area is normalized to 1.
Returns:
| Name | Type | Description |
|---|---|---|
loss_function |
callable
|
|
Examples:
>>> def f(xy):
... x, y = xy
... return x**2 + y**2
>>>
>>> loss = resolution_loss_function(min_distance=0.01, max_distance=1)
>>> learner = adaptive.Learner2D(f, bounds=[(-1, -1), (1, 1)], loss_per_triangle=loss)
Source code in adaptive/learner/learner2D.py
Helper functions¶
areas
¶
areas(ip: LinearNDInterpolator) -> ndarray
Returns the area per triangle of the triangulation inside
a LinearNDInterpolator instance.
Is useful when defining custom loss functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ip
|
scipy.interpolate.LinearNDInterpolator instance
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
areas |
ndarray
|
The area per triangle in |
Source code in adaptive/learner/learner2D.py
deviations
¶
deviations(ip: LinearNDInterpolator) -> ndarray
Returns the deviation of the linear estimate.
Is useful when defining custom loss functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ip
|
scipy.interpolate.LinearNDInterpolator instance
|
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
deviations |
ndarray
|
The deviation per triangle. |