Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add resolution_loss_function for Learner1D #310

Merged
merged 2 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions adaptive/learner/learner1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,43 @@ def triangle_loss(xs, ys):
return sum(vol(pts[i : i + 3]) for i in range(N)) / N


def resolution_loss_function(min_length=0, max_length=1):
"""Loss function that is similar to the `default_loss` function, but you
can set the maximum and minimum size of an interval.

Works with `~adaptive.Learner1D` only.

The arguments `min_length` and `max_length` should be in between 0 and 1
because the total size is normalized to 1.

Returns
-------
loss_function : callable

Examples
--------
>>> def f(x):
... return x**2
>>>
>>> loss = resolution_loss_function(min_length=0.01, max_length=1)
>>> learner = adaptive.Learner1D(f, bounds=[(-1, -1), (1, 1)], loss_per_triangle=loss)
"""

@uses_nth_neighbors(0)
def resolution_loss(xs, ys):
loss = uniform_loss(xs, ys)
if loss < min_length:
# Return zero such that this interval won't be chosen again
return 0
if loss > max_length:
# Return infinite such that this interval will be picked
return np.inf
loss = default_loss(xs, ys)
return loss

return resolution_loss


def curvature_loss_function(area_factor=1, euclid_factor=0.02, horizontal_factor=0.02):
# XXX: add a doc-string
@uses_nth_neighbors(1)
Expand Down
1 change: 1 addition & 0 deletions docs/source/tutorial/tutorial.custom_loss.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ tl;dr, one can use the following *loss functions* that
+ `adaptive.learner.learner1D.default_loss`
+ `adaptive.learner.learner1D.uniform_loss`
+ `adaptive.learner.learner1D.curvature_loss_function`
+ `adaptive.learner.learner1D.resolution_loss_function`
+ `adaptive.learner.learner1D.abs_min_log_loss`
+ `adaptive.learner.learner2D.default_loss`
+ `adaptive.learner.learner2D.uniform_loss`
Expand Down