From f84619a93583f1afa2bef8bf60aabf4203fd705c Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Thu, 18 Mar 2021 16:56:41 +0000 Subject: [PATCH 1/2] add resolution_loss_function for Learner1D --- adaptive/learner/learner1D.py | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/adaptive/learner/learner1D.py b/adaptive/learner/learner1D.py index 9b56db47d..f81b53ebe 100644 --- a/adaptive/learner/learner1D.py +++ b/adaptive/learner/learner1D.py @@ -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) From 6d48456b69d3a3a037201d22460035c9ab904c28 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Thu, 18 Mar 2021 17:03:29 +0000 Subject: [PATCH 2/2] add adaptive.learner.learner1D.resolution_loss_function to the docs --- docs/source/tutorial/tutorial.custom_loss.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/tutorial/tutorial.custom_loss.rst b/docs/source/tutorial/tutorial.custom_loss.rst index 43fc8f12b..528d50cdb 100644 --- a/docs/source/tutorial/tutorial.custom_loss.rst +++ b/docs/source/tutorial/tutorial.custom_loss.rst @@ -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`