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
Compare and
3 files
+ 129
31
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 101
28
@@ -3,16 +3,19 @@ from copy import deepcopy
import heapq
import itertools
import math
from collections import Iterable
import numpy as np
import sortedcontainers
from .base_learner import BaseLearner
from .learnerND import volume
from .triangulation import simplex_volume_in_embedding
from ..notebook_integration import ensure_holoviews
from ..utils import cache_latest
def uniform_loss(interval, scale, function_values):
def uniform_loss(interval, scale, function_values, neighbors):
"""Loss function that samples the domain uniformly.
Works with `~adaptive.Learner1D` only.
@@ -33,7 +36,7 @@ def uniform_loss(interval, scale, function_values):
return dx
def default_loss(interval, scale, function_values):
def default_loss(interval, scale, function_values, neighbors):
"""Calculate loss on a single interval.
Currently returns the rescaled length of the interval. If one of the
@@ -56,6 +59,45 @@ def default_loss(interval, scale, function_values):
return loss
def _loss_of_multi_interval(xs, ys):
N = len(xs) - 2
if isinstance(ys[0], Iterable):
pts = [(x, *y) for x, y in zip(xs, ys)]
vol = simplex_volume_in_embedding
else:
pts = [(x, y) for x, y in zip(xs, ys)]
vol = volume
return sum(vol(pts[i:i+3]) for i in range(N)) / N
def triangle_loss(interval, scale, function_values, 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])
if len(xs) <= 2:
return (x_right - x_left) / scale[0]
else:
y_scale = scale[1] or 1
ys_scaled = [function_values[x] / y_scale for x in xs]
xs_scaled = [x / scale[0] for x in xs]
return _loss_of_multi_interval(xs_scaled, ys_scaled)
def get_curvature_loss(area_factor=1, euclid_factor=0.02, horizontal_factor=0.02):
def curvature_loss(interval, scale, function_values, neighbors):
triangle_loss_ = triangle_loss(interval, scale, function_values, neighbors)
default_loss_ = default_loss(interval, scale, function_values, neighbors)
dx = (interval[1] - interval[0]) / scale[0]
return (area_factor * (triangle_loss_**0.5)
+ euclid_factor * default_loss_
+ horizontal_factor * dx)
return curvature_loss
def linspace(x_left, x_right, n):
"""This is equivalent to
'np.linspace(x_left, x_right, n, endpoint=False)[1:]',
@@ -79,6 +121,13 @@ def _get_neighbors_from_list(xs):
return sortedcontainers.SortedDict(neighbors)
def _get_interval(x, neighbors, nn_neighbors):
n = nn_neighbors
index = neighbors.bisect_left(x)
return [neighbors.iloc[i] for i in range(index - n - 1, index + 2 + n)
if 0 <= i < len(neighbors)]
class Learner1D(BaseLearner):
"""Learns and predicts a function 'f:ℝ → ℝ^N'.
@@ -93,6 +142,10 @@ 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
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.
Attributes
----------
@@ -103,9 +156,9 @@ class Learner1D(BaseLearner):
Notes
-----
`loss_per_interval` takes 3 parameters: ``interval``, ``scale``, and
``function_values``, and returns a scalar; the loss over the interval.
`loss_per_interval` takes 4 parameters: ``interval``, ``scale``,
``data``, and ``neighbors``, and returns a scalar; the loss over
the interval.
interval : (float, float)
The bounds of the interval.
scale : (float, float)
@@ -114,11 +167,18 @@ class Learner1D(BaseLearner):
function_values : dict(float → float)
A map containing evaluated function values. It is guaranteed
to have values for both of the points in 'interval'.
neighbors : dict(float → (float, float))
A map containing points as keys to its neighbors as a tuple.
"""
def __init__(self, function, bounds, loss_per_interval=None):
def __init__(self, function, bounds, loss_per_interval=None, nn_neighbors=0):
self.function = function
self.loss_per_interval = loss_per_interval or default_loss
self.nn_neighbors = nn_neighbors
if nn_neighbors == 0:
self.loss_per_interval = loss_per_interval or default_loss
else:
self.loss_per_interval = loss_per_interval or get_curvature_loss()
# A dict storing the loss function for each interval x_n.
self.losses = {}
@@ -176,25 +236,36 @@ class Learner1D(BaseLearner):
losses = self.losses if real else self.losses_combined
return max(losses.values()) if len(losses) > 0 else float('inf')
def _get_loss_in_interval(self, x_left, x_right):
assert x_left is not None and x_right is not None
if x_right - x_left < self._dx_eps:
return 0
# we need to compute the loss for this interval
interval = (x_left, x_right)
return self.loss_per_interval(
interval, self._scale, self.data, self.neighbors)
def _update_interpolated_loss_in_interval(self, x_left, x_right):
if x_left is not None and x_right is not None:
dx = x_right - x_left
if dx < self._dx_eps:
loss = 0
else:
loss = self.loss_per_interval((x_left, x_right),
self._scale, self.data)
self.losses[x_left, x_right] = loss
# Iterate over all interpolated intervals in between
# x_left and x_right and set the newly interpolated loss.
a, b = x_left, None
while b != x_right:
b = self.neighbors_combined[a][1]
self.losses_combined[a, b] = (b - a) * loss / dx
a = b
if x_left is None or x_right is None:
return
loss = self._get_loss_in_interval(x_left, x_right)
self.losses[x_left, x_right] = loss
# Iterate over all interpolated intervals in between
# x_left and x_right and set the newly interpolated loss.
a, b = x_left, None
dx = x_right - x_left
while b != x_right:
b = self.neighbors_combined[a][1]
self.losses_combined[a, b] = (b - a) * loss / dx
a = b
def _update_losses(self, x, real=True):
"""Update all losses that depend on x"""
# When we add a new point x, we should update the losses
# (x_left, x_right) are the "real" neighbors of 'x'.
x_left, x_right = self._find_neighbors(x, self.neighbors)
@@ -207,10 +278,12 @@ class Learner1D(BaseLearner):
if real:
# We need to update all interpolated losses in the interval
# (x_left, x) and (x, x_right). Since the addition of the point
# 'x' could change their loss.
self._update_interpolated_loss_in_interval(x_left, x)
self._update_interpolated_loss_in_interval(x, x_right)
# (x_left, x), (x, x_right) and the nn_neighbors nearest
# neighboring intervals. Since the addition of the
# point 'x' could change their loss.
points = _get_interval(x, self.neighbors, self.nn_neighbors)
for ival in zip(points, points[1:]):
self._update_interpolated_loss_in_interval(*ival)
# Since 'x' is in between (x_left, x_right),
# we get rid of the interval.
@@ -358,7 +431,7 @@ class Learner1D(BaseLearner):
self.losses = {}
for x_left, x_right in intervals:
self.losses[x_left, x_right] = (
self.loss_per_interval((x_left, x_right), self._scale, self.data)
self._get_loss_in_interval(x_left, x_right)
if x_right - x_left >= self._dx_eps else 0)
# List with "real" intervals that have interpolated intervals inside
Loading