Esempio n. 1
0
def parallel_spannogram_Dks_eps_psd(k, V, eps, delta):
    """
    Args
    ----
    k: int
        number of nodes in the output subgraph

    V: numpy array
        Rank-d approximation matrix

    eps, delta: float
        Error

    Returns
    -------

    A tuple of (metric, subgraph node index)

    Assumption
    -----------
    - Adjacency matrix (A) is large

    """
    n, d = V.shape

    supp_opt = 0
    metric_opt = 0

    Mopt = (1 + 4 / eps) ** d
    M = (math.log(delta) - math.log(Mopt)) / math.log(1 - 1 / Mopt)
    print(M)
    print('V.shape =', V.shape)

    t_dot = CumulativeTime("dot")
    t_process = CumulativeTime("process")

    count = int(round(M))
    parspan = ParallelSpannogram(V=V, k=k, threshold=10)
    for i in range(count):
        progress.render_progress(i / count, width=50)

        c = np.random.randn(d, 1)

        # Do matrix multiplication
        with t_dot.time():
            Vc = V.dot(c)

        with t_process.time():
            parspan.append(Vc)
            metric_opt, supp_opt = parspan.process(metric_opt, supp_opt)

    with t_process.time():
        metric_opt, supp_opt = parspan.flush(metric_opt, supp_opt)

    print()
    print(t_dot)
    print(t_process)

    return metric_opt, supp_opt
Esempio n. 2
0
def spannogram_Dks_eps_psd(k, V, eps, delta):
    """
    Args
    ----
    k: int
        number of nodes in the output subgraph

    V: numpy array
        Rank-d approximation matrix

    eps, delta: float
        Error

    Returns
    -------

    A tuple of (metric, subgraph node index)

    Assumption
    -----------
    - Adjacency matrix (A) is large

    """
    n, d = V.shape

    supp_opt = 0
    metric_opt = 0

    Mopt = (1 + 4 / eps) ** d
    M = (math.log(delta) - math.log(Mopt)) / math.log(1 - 1 / Mopt)
    print(M)
    print('V.shape =', V.shape)

    t_dot = CumulativeTime("dot")
    t_argsort = CumulativeTime("argsort")
    t_norm = CumulativeTime("norm-scatter-square")

    count = int(round(M))
    for i in range(count):
        progress.render_progress(i / count, width=50)

        c = np.random.randn(d, 1)

        # Do matrix multiplication
        with t_dot.time():
            Vc = V.dot(c)

        # Do sort
        with t_argsort.time():
            indx = np.argsort(Vc, axis=0)

        # Get last k
        topk = indx[-k:]
        # Assume A is huge
        with t_norm.time():
            metric = np.linalg.norm(V[topk, :]) ** 2
        if metric > metric_opt:
            metric_opt = metric
            supp_opt = topk
    print()
    print(t_dot)
    print(t_argsort)
    print(t_norm)
    return metric_opt, supp_opt