Skip to content

Commit 2d0444b

Browse files
authored
Merge pull request #310 from python-adaptive/resolution-loss-1d
add resolution_loss_function for Learner1D
2 parents a7fb70f + 6d48456 commit 2d0444b

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

adaptive/learner/learner1D.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,43 @@ def triangle_loss(xs, ys):
7777
return sum(vol(pts[i : i + 3]) for i in range(N)) / N
7878

7979

80+
def resolution_loss_function(min_length=0, max_length=1):
81+
"""Loss function that is similar to the `default_loss` function, but you
82+
can set the maximum and minimum size of an interval.
83+
84+
Works with `~adaptive.Learner1D` only.
85+
86+
The arguments `min_length` and `max_length` should be in between 0 and 1
87+
because the total size is normalized to 1.
88+
89+
Returns
90+
-------
91+
loss_function : callable
92+
93+
Examples
94+
--------
95+
>>> def f(x):
96+
... return x**2
97+
>>>
98+
>>> loss = resolution_loss_function(min_length=0.01, max_length=1)
99+
>>> learner = adaptive.Learner1D(f, bounds=[(-1, -1), (1, 1)], loss_per_triangle=loss)
100+
"""
101+
102+
@uses_nth_neighbors(0)
103+
def resolution_loss(xs, ys):
104+
loss = uniform_loss(xs, ys)
105+
if loss < min_length:
106+
# Return zero such that this interval won't be chosen again
107+
return 0
108+
if loss > max_length:
109+
# Return infinite such that this interval will be picked
110+
return np.inf
111+
loss = default_loss(xs, ys)
112+
return loss
113+
114+
return resolution_loss
115+
116+
80117
def curvature_loss_function(area_factor=1, euclid_factor=0.02, horizontal_factor=0.02):
81118
# XXX: add a doc-string
82119
@uses_nth_neighbors(1)

docs/source/tutorial/tutorial.custom_loss.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ tl;dr, one can use the following *loss functions* that
4747
+ `adaptive.learner.learner1D.default_loss`
4848
+ `adaptive.learner.learner1D.uniform_loss`
4949
+ `adaptive.learner.learner1D.curvature_loss_function`
50+
+ `adaptive.learner.learner1D.resolution_loss_function`
5051
+ `adaptive.learner.learner1D.abs_min_log_loss`
5152
+ `adaptive.learner.learner2D.default_loss`
5253
+ `adaptive.learner.learner2D.uniform_loss`

0 commit comments

Comments
 (0)