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

pass value_scale to the LearnerND's loss_per_simplex function #219

Merged
merged 1 commit into from Sep 20, 2019

Conversation

basnijholt
Copy link
Member

@basnijholt basnijholt commented Sep 20, 2019

Currently, the LearnerND cannot access the unscaled value in the loss_per_simplex.

This PR fixes this by passing the scale. This fixes a regression introduced when we added scaling.

Now the following code:

import numpy as np
import adaptive
adaptive.notebook_extension()

def isoline_loss_function(y_iso, sigma, priority=1):
    from adaptive.learner.learnerND import default_loss

    def gaussian(x, mu, sigma):
        return np.exp(-(x - mu) ** 2 / sigma ** 2)

    def loss(simplex, values, value_scale):
        distance = np.mean([abs(y_iso * value_scale - y) for y in values])
        return priority * gaussian(distance, 0, sigma) + default_loss(simplex, values, value_scale)

    return loss

level = 0.5
loss = isoline_loss_function(y_iso=level, sigma=1, priority=0.3)

def f(xy):
    x, y = xy
    return x ** 2 + y ** 2 / 2 + y ** 3

learner = adaptive.LearnerND(
    f,
    bounds=[(-1, 1), (-1, 1)],
    loss_per_simplex=loss,
)

adaptive.runner.simple(learner, goal=lambda l: l.npoints > 1000)
%opts Image [colorbar=True]
learner.plot_isoline(level, tri_alpha=0.3)

produces
image

instead of

image

@jbweston
Copy link
Contributor

Seems ok, don't know why tests are failing

@jbweston
Copy link
Contributor

@basnijholt the test failures are genuine.

>      loss_curvature = triangle_loss(simplex, values, neighbors, neighbor_values)
E       TypeError: triangle_loss() missing 1 required positional argument: 'neighbor_values'

Threre may be others

@basnijholt
Copy link
Member Author

Thanks! I've already pushed a fix.

@basnijholt
Copy link
Member Author

@jbweston merge if you are OK with this 👍

@jbweston jbweston merged commit fc297c3 into master Sep 20, 2019
@basnijholt basnijholt deleted the value_scale_ND branch September 20, 2019 12:21
@basnijholt basnijholt mentioned this pull request Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants