Exemplo n.º 1
0
def triangle_loss(simplex, values, value_scale, neighbors, neighbor_values):
    """
    Computes the average of the volumes of the simplex combined with each
    neighbouring point.

    Parameters
    ----------
    simplex : list of tuples
        Each entry is one point of the simplex.
    values : list of values
        The scaled function values of each of the simplex points.
    value_scale : float
        The scale of values, where ``values = function_values * value_scale``.
    neighbors : list of tuples
        The neighboring points of the simplex, ordered such that simplex[0]
        exactly opposes neighbors[0], etc.
    neighbor_values : list of values
        The function values for each of the neighboring points.

    Returns
    -------
    loss : float
    """

    neighbors = [n for n in neighbors if n is not None]
    neighbor_values = [v for v in neighbor_values if v is not None]
    if len(neighbors) == 0:
        return 0

    s = [(*x, *to_list(y)) for x, y in zip(simplex, values)]
    n = [(*x, *to_list(y)) for x, y in zip(neighbors, neighbor_values)]

    return sum(simplex_volume_in_embedding([*s, neighbor]) for neighbor in n) / len(
        neighbors
    )
Exemplo n.º 2
0
def default_loss(simplex, ys):
    # return std_loss(simplex, ys)
    if isinstance(ys[0], Iterable):
        pts = [(*x, *y) for x, y in zip(simplex, ys)]
    else:
        pts = [(*x, y) for x, y in zip(simplex, ys)]
    return simplex_volume_in_embedding(pts)
Exemplo n.º 3
0
def default_loss(simplex, values, value_scale):
    """
    Computes the average of the volumes of the simplex.

    Parameters
    ----------
    simplex : list of tuples
        Each entry is one point of the simplex.
    values : list of values
        The scaled function values of each of the simplex points.
    value_scale : float
        The scale of values, where ``values = function_values * value_scale``.

    Returns
    -------
    loss : float
    """
    if isinstance(values[0], Iterable):
        pts = [(*x, *y) for x, y in zip(simplex, values)]
    else:
        pts = [(*x, y) for x, y in zip(simplex, values)]
    return simplex_volume_in_embedding(pts)
Exemplo n.º 4
0
def triangle_loss(ip):
    r"""Computes the average of the volumes of the simplex combined with each
    neighbouring point.

    Parameters
    ----------
    ip : `scipy.interpolate.LinearNDInterpolator` instance

    Returns
    -------
    triangle_loss : list
        The mean volume per triangle.

    Notes
    -----
    This loss function is *extremely* slow. It is here because it gives the
    same result as the `adaptive.LearnerND`\s
    `~adaptive.learner.learnerND.triangle_loss`.
    """
    tri = ip.tri

    def get_neighbors(i, ip):
        n = np.array([tri.simplices[n] for n in tri.neighbors[i] if n != -1])
        # remove the vertices that are in the simplex
        c = np.setdiff1d(n.reshape(-1), tri.simplices[i])
        return np.concatenate((tri.points[c], ip.values[c]), axis=-1)

    simplices = np.concatenate(
        [tri.points[tri.simplices], ip.values[tri.simplices]], axis=-1
    )
    neighbors = [get_neighbors(i, ip) for i in range(len(tri.simplices))]

    return [
        sum(simplex_volume_in_embedding(np.vstack([simplex, n])) for n in neighbors[i])
        / len(neighbors[i])
        for i, simplex in enumerate(simplices)
    ]