예제 #1
0
def solve(C, p, D, k, *, solve_dense, eta, is_kl_not_js, j, L):
    n = C.shape[1]

    best_y = warm_kl_like.solve(C,
                                p,
                                D,
                                k,
                                solve_dense=solve_dense,
                                eta=eta,
                                is_kl_not_js=is_kl_not_js,
                                j=j)
    S = best_y != 0
    q = C[:, S] @ best_y[S]
    best_divergence = D(p, q)

    for l in L:
        spaces = identification.shift(C=C, p=p, D=D, q=q)
        S[sorting.argmins(spaces, l)] = True

        y = numpy.zeros(n)
        y[S] = solve_dense(C[:, S], p)

        S.fill(False)
        S[sorting.argmaxs(y, k)] = True

        q = C[:, S] @ y[S]

    y = numpy.zeros(n)
    y[S] = solve_dense(C[:, S], p)
    divergence = D(p, C[:, S] @ y[S])
    if divergence < best_divergence:
        best_y = y

    return best_y
def solve(C, p, D, k, *, solve_dense, L):
    m, n = C.shape
    S = numpy.full(n, False)
    q = numpy.zeros(m)
    best_divergence = math.inf

    for l in L:
        spaces = identification.shift(C=C, p=p, D=D, q=q)
        S[sorting.argmins(spaces, l)] = True

        y = numpy.zeros(n)
        y[S] = solve_dense(C[:, S], p)

        S.fill(False)
        S[sorting.argmaxs(y, k)] = True

        y = numpy.zeros(n)
        y[S] = solve_dense(C[:, S], p)

        q = C[:, S] @ y[S]
        divergence = D(p, q)

        if divergence < best_divergence:
            best_y = y
            best_divergence = divergence

    return best_y
def solve(C, p, D, k, *, solve_dense, etas, is_kl_not_js, J, L):
    n = C.shape[1]

    best_divergence = math.inf
    solve_dense_cache = {}

    def cached_solve_dense(S):
        indices = frozenset(numpy.nonzero(S)[0])
        try:
            return solve_dense_cache[indices]
        except KeyError:
            y = solve_dense(C[:, S], p)
            solve_dense_cache[indices] = y
            return y

    for eta in etas:
        ys = warm.iterate(C=C,
                          p=p,
                          D=D,
                          eta=eta,
                          is_kl_not_js=is_kl_not_js,
                          q=None)

        for y in itertools.islice((y for i, y in enumerate(ys) if i in J),
                                  len(J)):
            S = numpy.full(n, False)
            S[sorting.argmaxs(y, k)] = True
            y = numpy.zeros(n)
            y[S] = cached_solve_dense(S)

            q = C[:, S] @ y[S]
            divergence = D(p, q)
            if divergence < best_divergence:
                best_y = y
                best_divergence = divergence

            for l in L:
                spaces = identification.shift(C=C, p=p, D=D, q=q)

                S[sorting.argmins(spaces, l)] = True
                y = numpy.zeros(n)
                y[S] = cached_solve_dense(S)

                S.fill(False)
                S[sorting.argmaxs(y, k)] = True
                y = numpy.zeros(n)
                y[S] = cached_solve_dense(S)

                q = C[:, S] @ y[S]
                divergence = D(p, q)
                if divergence < best_divergence:
                    best_y = y
                    best_divergence = divergence

    return best_y
예제 #4
0
def solve(C, p, D, k, *, solve_dense, alpha, beta):
    m, n = C.shape
    S = numpy.full(n, False)
    q = numpy.zeros(m)

    while numpy.count_nonzero(S) < k:
        spaces = identification.shift(C=C[:, ~S], p=p, D=D, q=q)
        T = numpy.flatnonzero(~S)[sorting.argmins(spaces, alpha)]
        S[T] = True

        y = numpy.zeros(n)
        y[S] = solve_dense(C[:, S], p)

        count = max(beta, numpy.count_nonzero(S) - k)
        T = numpy.flatnonzero(S)[sorting.argmins(y[S], count)]
        S[T] = False

        y = numpy.zeros(n)
        y[S] = solve_dense(C[:, S], p)

        q = C[:, S] @ y[S]

    return y
예제 #5
0
def solve(C, p, _, k, *, solve_dense, l):
    n = C.shape[1]
    S = numpy.full(n, True)
    y = solve_dense(C, p)

    while numpy.count_nonzero(S) > k:
        surplus = numpy.count_nonzero(S) - k
        l_i = l if isinstance(l, int) else l(surplus)
        drops = max(min(l_i, surplus), 1)
        T = numpy.flatnonzero(S)[sorting.argmins(y[S], drops)]
        S[T] = False

        y = numpy.zeros(n)
        y[S] = solve_dense(C[:, S], p)

    return y
def solve(C, p, D, k, *, solve_dense, L):
    m, n = C.shape
    S = numpy.full(n, False)
    q = numpy.zeros(m)

    for l in L:
        spaces = identification.shift(C=C, p=p, D=D, q=q)
        S[sorting.argmins(spaces, l)] = True

        y = numpy.zeros(n)
        y[S] = solve_dense(C[:, S], p)

        S.fill(False)
        S[sorting.argmaxs(y, k)] = True

        q = C[:, S] @ y[S]

    y = numpy.zeros(n)
    y[S] = solve_dense(C[:, S], p)

    return y
def test_argmins(values, count, expected_indices):
    actual_indices = sorting.argmins(values, count)

    assert all(numpy.sort(actual_indices) == expected_indices)