How to use the adaptive.learner.new_learnerND.LossFunction function in adaptive

To help you get started, we’ve selected a few adaptive examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-adaptive / adaptive / adaptive / learner / new_learnerND.py View on Github external
simplex = [domain.triangulation.vertices[p] for p in subdomain]

        z = data[simplex[0]]
        if isinstance(z, Iterable):
            s = [(*x, *data[x]) for x in simplex]
            n = [(*x, *data[x]) for x in neighbor_points]
        else:
            s = [(*x, data[x]) for x in simplex]
            n = [(*x, data[x]) for x in neighbor_points]

        return sum(simplex_volume_in_embedding([*s, neigh]) for neigh in n) / len(
            neighbors
        )


class CurvatureLoss(LossFunction):
    def __init__(self, exploration=0.05):
        self.exploration = exploration

    @property
    def n_neighbors(self):
        return 1

    def __call__(self, domain, subdomain, codomain_bounds, data):
        dim = domain.ndim

        loss_input_volume = domain.volume(subdomain)
        triangle_loss = TriangleLoss()

        loss_curvature = triangle_loss(domain, subdomain, codomain_bounds, data)
        return (
            loss_curvature + self.exploration * loss_input_volume ** ((2 + dim) / dim)
github python-adaptive / adaptive / adaptive / learner / new_learnerND.py View on Github external
"""


class DistanceLoss(LossFunction):
    @property
    def n_neighbors(self):
        return 0

    def __call__(self, domain, subdomain, codomain_bounds, data):
        assert isinstance(domain, Interval)
        a, b = subdomain
        ya, yb = data[a], data[b]
        return math.sqrt((b - a) ** 2 + (yb - ya) ** 2)


class EmbeddedVolumeLoss(LossFunction):
    @property
    def n_neighbors(self):
        return 0

    def __call__(self, domain, subdomain, codomain_bounds, data):
        assert isinstance(domain, ConvexHull)
        xs = [tuple(domain.triangulation.vertices[x]) for x in subdomain]
        ys = [data[x] for x in xs]
        if isinstance(ys[0], Iterable):
            pts = [(*x, *y) for x, y in zip(xs, ys)]
        else:
            pts = [(*x, y) for x, y in zip(xs, ys)]
        return simplex_volume_in_embedding(pts)


class TriangleLoss(LossFunction):
github python-adaptive / adaptive / adaptive / learner / new_learnerND.py View on Github external
class LossFunction(metaclass=abc.ABCMeta):
    @abc.abstractproperty
    def n_neighbors(self):
        "The maximum degree of neighboring subdomains required."

    @abc.abstractmethod
    def __call__(self, domain, subdomain, codomain_bounds, data):
        """Return the loss for 'subdomain' given 'data'

        Neighboring subdomains can be obtained with
        'domain.neighbors(subdomain, self.n_neighbors)'.
        """


class DistanceLoss(LossFunction):
    @property
    def n_neighbors(self):
        return 0

    def __call__(self, domain, subdomain, codomain_bounds, data):
        assert isinstance(domain, Interval)
        a, b = subdomain
        ya, yb = data[a], data[b]
        return math.sqrt((b - a) ** 2 + (yb - ya) ** 2)


class EmbeddedVolumeLoss(LossFunction):
    @property
    def n_neighbors(self):
        return 0
github python-adaptive / adaptive / adaptive / learner / new_learnerND.py View on Github external
    @property
    def n_neighbors(self):
        return 0

    def __call__(self, domain, subdomain, codomain_bounds, data):
        assert isinstance(domain, ConvexHull)
        xs = [tuple(domain.triangulation.vertices[x]) for x in subdomain]
        ys = [data[x] for x in xs]
        if isinstance(ys[0], Iterable):
            pts = [(*x, *y) for x, y in zip(xs, ys)]
        else:
            pts = [(*x, y) for x, y in zip(xs, ys)]
        return simplex_volume_in_embedding(pts)


class TriangleLoss(LossFunction):
    @property
    def n_neighbors(self):
        return 1

    def __call__(self, domain, subdomain, codomain_bounds, data):
        assert isinstance(domain, ConvexHull)
        neighbors = domain.neighbors(subdomain, self.n_neighbors)
        if not neighbors:
            return 0
        neighbor_points = set.union(*(set(n) - set(subdomain) for n in neighbors))

        neighbor_points = [domain.triangulation.vertices[p] for p in neighbor_points]

        simplex = [domain.triangulation.vertices[p] for p in subdomain]

        z = data[simplex[0]]