Skip to content
Snippets Groups Projects

Resolve "(Learner1D) add possibility to use the direct neighbors in the loss"

Merged Jorn Hoofwijk requested to merge 119-add-second-order-loss-to-adaptive into master
1 unresolved thread
Compare and Show latest version
6 files
+ 162
36
Compare changes
  • Side-by-side
  • Inline
Files
6
@@ -15,6 +15,65 @@ from ..notebook_integration import ensure_holoviews
from ..utils import cache_latest
def use_nn_neighbors(n):
"""Decorator to specify how many neighboring intervals the loss function uses.
This decorator can wrap around a loss function to let `~adaptive.Learner1D`
know that you would like to look at the N-nearest neighboring intervals.
The loss function is then guaranteed to receive the data of at least the
nn-neighbors and a dict that tells you what the neighboring points of these
are. And the Learner1D will then make sure that the loss is updated whenever
on of the nn-neighbours changes.
Examples
--------
This is a part of the curvature loss function
>>> @use_nn_neighbors(1)
...def triangle_loss(interval, scale, data, neighbors):
... x_left, x_right = interval
... xs = [neighbors[x_left][0], x_left, x_right, neighbors[x_right][1]]
... # at the boundary, neighbours[<left boundary x>] is (None, <some other x>)
... xs = [x for x in xs if x is not None]
... if len(xs) <= 2:
... return (x_right - x_left) / scale[0]
...
... y_scale = scale[1] or 1
... ys_scaled = [data[x] / y_scale for x in xs]
... xs_scaled = [x / scale[0] for x in xs]
... N = len(xs) - 2
... pts = [(x, y) for x, y in zip(xs_scaled, ys_scaled)]
... return sum(volume(pts[i:i+3]) for i in range(N)) / N
Or you may define a loss that favours the (local) minima of a function.
>>>@use_nn_neighbors(1)
...def loss(interval, scale, data, neighbors):
... x_left, x_right = interval
... n_left = neighbors[x_left][0]
... n_right = neighbors[x_right][1]
... is_min = True
...
... if n_left is not None and data[x_left] > data[n_left]:
... is_min = False
... if n_right is not None and data[x_right] > data[n_right]:
... is_min = False
...
... loss = (x_right - x_left) / scale[0]
...
... if is_min:
... return loss * 100
... return loss
"""
def _wrapped(loss_per_interval):
loss_per_interval.nn_neighbors = n
return loss_per_interval
return _wrapped
@use_nn_neighbors(0)
def uniform_loss(interval, scale, data, neighbors):
"""Loss function that samples the domain uniformly.
@@ -36,6 +95,7 @@ def uniform_loss(interval, scale, data, neighbors):
return dx
@use_nn_neighbors(0)
def default_loss(interval, scale, data, neighbors):
"""Calculate loss on a single interval.
@@ -70,13 +130,11 @@ def _loss_of_multi_interval(xs, ys):
return sum(vol(pts[i:i+3]) for i in range(N)) / N
@use_nn_neighbors(1)
def triangle_loss(interval, scale, data, neighbors):
x_left, x_right = interval
xs = [x_left, x_right]
if x_left in neighbors:
xs.insert(0, neighbors[x_left][1])
if x_right in neighbors:
xs.append(neighbors[x_right][0])
xs = [neighbors[x_left][0], x_left, x_right, neighbors[x_right][1]]
xs = [x for x in xs if x is not None]
if len(xs) <= 2:
return (x_right - x_left) / scale[0]
@@ -88,6 +146,7 @@ def triangle_loss(interval, scale, data, neighbors):
def get_curvature_loss(area_factor=1, euclid_factor=0.02, horizontal_factor=0.02):
@use_nn_neighbors(1)
def curvature_loss(interval, scale, data, neighbors):
triangle_loss_ = triangle_loss(interval, scale, data, neighbors)
default_loss_ = default_loss(interval, scale, data, neighbors)
@@ -122,12 +181,12 @@ def _get_neighbors_from_list(xs):
def _get_intervals(x, neighbors, nn_neighbors):
n = nn_neighbors
index = neighbors.bisect_left(x)
points = [neighbors.iloc[i] for i in range(index - n - 1, index + 2 + n)
if 0 <= i < len(neighbors)]
intervals = zip(points, points[1:])
return list(intervals)
nn = nn_neighbors
i = neighbors.index(x)
start = max(0, i - nn - 1)
end = min(len(neighbors), i + nn + 2)
points = neighbors.keys()[start:end]
return list(zip(points, points[1:]))
class Learner1D(BaseLearner):
@@ -144,10 +203,12 @@ class Learner1D(BaseLearner):
A function that returns the loss for a single interval of the domain.
If not provided, then a default is used, which uses the scaled distance
in the x-y plane as the loss. See the notes for more details.
nn_neighbors : int, default: 0
nn_neighbors : int, optional, default: None
The number of neighboring intervals that the loss function
takes into account. If ``loss_per_interval`` doesn't use the neighbors
at all, then it should be 0.
at all, then it should be 0. By default we try to access the
``loss_per_interval.nn_neighbors`` attribute which is set for all
implemented loss functions.
Attributes
----------
@@ -173,14 +234,15 @@ class Learner1D(BaseLearner):
A map containing points as keys to its neighbors as a tuple.
"""
def __init__(self, function, bounds, loss_per_interval=None, nn_neighbors=0):
def __init__(self, function, bounds, loss_per_interval=None):
self.function = function
self.nn_neighbors = nn_neighbors
if nn_neighbors == 0:
self.loss_per_interval = loss_per_interval or default_loss
if hasattr(loss_per_interval, 'nn_neighbors'):
self.nn_neighbors = loss_per_interval.nn_neighbors
else:
self.loss_per_interval = loss_per_interval or get_curvature_loss()
self.nn_neighbors = 0
self.loss_per_interval = loss_per_interval or default_loss
# A dict storing the loss function for each interval x_n.
self.losses = {}
Loading