Example #1
0
def main():
    args = parse_args()
    if not args.scipy_only:
        if args.sparsity > 0.:
            if args.sparsity >= 1.:
                raise (RuntimeError("Sparsity must be less than 1."))
            nnz = (1. - args.sparsity) * args.num_row * args.num_col
            v = np.random.rand(nnz)
            i = np.random.randint(0, high=args.num_row, size=nnz)
            j = np.random.randint(0, high=args.num_col, size=nnz)
            A = sp.csc_matrix((v, (i, j)), shape=(args.num_row, args.num_col))
        else:
            A = np.random.rand(args.num_row * args.num_col).reshape(
                args.num_row, args.num_col)

        start_irlb = time.time()
        X = irlb(A, args.nu, tol=args.tol, maxit=args.max_it)
        end_irlb = time.time()

    # Compare estimated values with np.linalg.svd:
    if not args.irlb_only:
        if args.sparsity > 0.:
            start_svd = time.time()
            S = splinalg.svds(A=A,
                              k=args.nu,
                              tol=args.tol,
                              maxiter=args.max_it,
                              return_singular_vectors=True)
            end_svd = time.time()
        else:
            start_svd = time.time()
            S = np.linalg.svd(A, 0)
            end_svd = time.time()

        abserr = np.max(np.abs(X[1] - S[1][0:args.nu]))

    if (args.csv):
        if args.irlb_only:
            print("{}".format(end_irlb - start_irlb))
        elif args.scipy_only:
            print("{}".format(end_svd - start_svd))
        else:
            print("{},{},{}".format(end_irlb - start_irlb, end_svd - start_svd,
                                    abserr))
    else:
        if not args.scipy_only:
            print("IRLB timing: {}".format(end_irlb - start_irlb))
        if not args.irlb_only:
            print("SVD timing: {}".format(end_svd - start_svd))
        if not args.irlb_only and not args.scipy_only:
            print("Absolute error: {}".format(abserr))
Example #2
0
def main():
    args = parse_args()
    if not args.scipy_only:
        if args.sparsity > 0.0:
            if args.sparsity >= 1.0:
                raise (RuntimeError("Sparsity must be less than 1."))
            nnz = (1.0 - args.sparsity) * args.num_row * args.num_col
            v = np.random.rand(nnz)
            i = np.random.randint(0, high=args.num_row, size=nnz)
            j = np.random.randint(0, high=args.num_col, size=nnz)
            A = sp.csc_matrix((v, (i, j)), shape=(args.num_row, args.num_col))
        else:
            A = np.random.rand(args.num_row * args.num_col).reshape(args.num_row, args.num_col)

        start_irlb = time.time()
        X = irlb(A, args.nu, tol=args.tol, maxit=args.max_it)
        end_irlb = time.time()

    # Compare estimated values with np.linalg.svd:
    if not args.irlb_only:
        if args.sparsity > 0.0:
            start_svd = time.time()
            S = splinalg.svds(A=A, k=args.nu, tol=args.tol, maxiter=args.max_it, return_singular_vectors=True)
            end_svd = time.time()
        else:
            start_svd = time.time()
            S = np.linalg.svd(A, 0)
            end_svd = time.time()

        abserr = np.max(np.abs(X[1] - S[1][0 : args.nu]))

    if args.csv:
        if args.irlb_only:
            print("{}".format(end_irlb - start_irlb))
        elif args.scipy_only:
            print("{}".format(end_svd - start_svd))
        else:
            print("{},{},{}".format(end_irlb - start_irlb, end_svd - start_svd, abserr))
    else:
        if not args.scipy_only:
            print("IRLB timing: {}".format(end_irlb - start_irlb))
        if not args.irlb_only:
            print("SVD timing: {}".format(end_svd - start_svd))
        if not args.irlb_only and not args.scipy_only:
            print("Absolute error: {}".format(abserr))
Example #3
0
def main():
    m = 2000  # Number of rows
    n = 2000  # Number of columns
    nnz = 10000  # Maximum number of nonzero elements
    nu = 5  # Number of singular values to compute

    v = np.random.rand(nnz)
    i = np.random.randint(0, high=m, size=nnz)
    j = np.random.randint(0, high=n, size=nnz)
    A = sp.csc_matrix((v, (i, j)), shape=(m, n))
    X = irlb(A, nu, tol=0.000001)
    # A gut check measurement of absolute decomposition error:
    print("TSVD: ||AV - US||_F = %f" % np.linalg.norm(A.dot(sp.csr_matrix(X[2])).todense().A - X[0] * X[1], "fro"))

    # Compare with reference svd
    S = np.linalg.svd(A.todense(), 0)
    abserr = np.max(np.abs(X[1] - S[1][0:nu]))
    print("Estmated/accurate singular values:")
    print(X[1])
    print(S[1][0:nu])
    print("||S_tsvd - S_svd||_inf = %f" % abserr)
Example #4
0
def main():
    m = 2000  # Number of rows
    n = 2000  # Number of columns
    nnz = 10000  # Maximum number of nonzero elements
    nu = 5  # Number of singular values to compute

    v = np.random.rand(nnz)
    i = np.random.randint(0, high=m, size=nnz)
    j = np.random.randint(0, high=n, size=nnz)
    A = sp.csc_matrix((v, (i, j)), shape=(m, n))
    X = irlb(A, nu, tol=0.000001)
    # A gut check measurement of absolute decomposition error:
    print("TSVD: ||AV - US||_F = %f" % np.linalg.norm(
        A.dot(sp.csr_matrix(X[2])).todense().A - X[0] * X[1], "fro"))

    # Compare with reference svd
    S = np.linalg.svd(A.todense(), 0)
    abserr = np.max(np.abs(X[1] - S[1][0:nu]))
    print("Estmated/accurate singular values:")
    print(X[1])
    print(S[1][0:nu])
    print("||S_tsvd - S_svd||_inf = %f" % abserr)