コード例 #1
0
ファイル: svm.py プロジェクト: liq07lzucn/pogs-python
def Svm(m, n, gpu=False, double_precision=False):
    # set solver cpu/gpu according to input args
    if gpu and pogs.SolverGPU is None:
        print "\nGPU solver unavailable, using CPU solver\n"
        gpu = False

    Solver = pogs.SolverGPU if gpu else pogs.SolverCPU

    # Generate A according to:
    #   X = [randn(m/2, n) + ones(m/2, n); randn(m/2, n) - ones(m/2, n)]
    #   yhat = [ones(m/2, 1); -ones(m/2, 1)]
    #   A = [(-yhat * ones(1, n)) .* X, -yhat]
    ind_half = floor(m / 2.)

    X = randn(m, n)
    X[:ind_half, :] += 1
    X[ind_half:, :] -= 1

    yhat = ones((m, 1))
    yhat[ind_half:] *= -1

    A = -yhat * hstack((X, ones((m, 1))))

    # cast A as float/double according to input args
    A = A if double_precision else np.float32(A)

    _lambda = 1.

    # f(y)
    f = pogs.FunctionVector(m, double_precision=double_precision)
    f.b[:] = -1
    f.c[:] = _lambda
    f.h[:] = pogs.FUNCTION["MAXPOS0"]

    # g( [w; b] )
    g = pogs.FunctionVector(n + 1, double_precision=double_precision)
    g.a[:] = 0.5
    g.h[:-1] = pogs.FUNCTION["SQUARE"]
    g.h[:-1] = pogs.FUNCTION["ZERO"]

    # intialize solver
    s = Solver(A)

    # solve
    s.solve(f, g)

    # get solve time
    t = s.info.solvetime

    # tear down solver in C++/CUDA
    s.finish()

    return t
コード例 #2
0
ファイル: lp_eq.py プロジェクト: liq07lzucn/pogs-python
def LpEq(m, n, gpu=False, double_precision=False):
    # set solver cpu/gpu according to input args
    if gpu and pogs.SolverGPU is None:
        print "\nGPU solver unavailable, using CPU solver\n"
        gpu = False

    Solver = pogs.SolverGPU if gpu else pogs.SolverCPU

    # Generate A and c according to:
    #   A = 1 / n * rand(m, n)
    #   c = 1 / n * rand(n, 1)

    A = rand(m, n) / n
    c = rand(n, 1) / n

    # Generate b according to:
    #   v = rand(n, 1)
    #   b = A * v
    b = A.dot(rand(n))

    # Gather A and c into one matrix
    A = vstack((A, c.T))

    # cast A as float/double according to input args
    A = A if double_precision else float32(A)

    # f(Ax) = Ind ( (Ax-b) == 0 ) + c^Tx
    f = pogs.FunctionVector(m + 1, double_precision=double_precision)
    f.b[:-1] = b[:]
    f.h[:-1] = pogs.FUNCTION["INDEQ0"]
    f.h[-1] = pogs.FUNCTION["IDENTITY"].value

    # g(x) = Ind( x >= 0 )
    g = pogs.FunctionVector(n, double_precision=double_precision)
    g.h[:] = pogs.FUNCTION["INDGE0"]

    # intialize solver
    s = Solver(A)

    # solve
    s.solve(f, g)

    # get solve time
    t = s.info.solvetime

    # tear down solver in C++/CUDA
    s.finish()

    return t
コード例 #3
0
def Lasso(m, n, gpu=False, double_precision=False):
    # set solver cpu/gpu according to input args
    if gpu and pogs.SolverGPU is None:
        print "\nGPU solver unavailable, using CPU solver\n"
        gpu = False

    Solver = pogs.SolverGPU if gpu else pogs.SolverCPU

    # random matrix A
    A = randn(m, n)

    # cast A as float/double according to input args
    A = A if double_precision else float32(A)

    # true x vector, ~20% zeros
    x_true = (randn(n) / sqrt(n)) * float64(randn(n) < 0.8)

    # b= A*x_true + v (noise)
    b = A.dot(x_true) + 0.5 * randn(m)

    # lambda
    lambda_max = max(abs(A.T.dot(b)))

    # f(Ax) = ||Ax - b||_2^2
    f = pogs.FunctionVector(m, double_precision=double_precision)
    f.b[:] = b[:]
    f.h[:] = pogs.FUNCTION["SQUARE"]

    # g(x) = 0.2*lambda_max*||x||_1
    g = pogs.FunctionVector(n, double_precision=double_precision)
    g.a[:] = 0.2 * lambda_max
    g.h[:] = pogs.FUNCTION["ABS"]

    # use problem data A to create solver
    s = Solver(A)

    # solve
    s.solve(f, g)

    # get solve time
    t = s.info.solvetime

    # tear down solver in C++/CUDA
    s.finish()

    return t
コード例 #4
0
ファイル: nonneg_l2.py プロジェクト: liq07lzucn/pogs-python
def NonNegL2(m, n, gpu=False, double_precision=False):
    # set solver cpu/gpu according to input args
    if gpu and pogs.SolverGPU is None:
        print "\nGPU solver unavailable, using CPU solver\n"
        gpu = False

    Solver = pogs.SolverGPU if gpu else pogs.SolverCPU

    # Generate A according to:
    #   A = 1 / n * rand(m, n)
    A = rand(m, n) / n

    # cast A as float/double according to input args
    A = A if double_precision else float32(A)

    # Generate b according to:
    #   n_half = floor(2 * n / 3);
    #   b = A * [ones(n_half, 1); -ones(n - n_half, 1)] + 0.1 * randn(m, 1)
    n_half = int(floor(2. * n / 3.))
    right_vec = ones(n)
    right_vec[n_half:] *= -1
    b = A.dot(right_vec) + 0.1 * randn(m)

    # f(Ax) = 1/2 || A*x - b ||_2^2
    f = pogs.FunctionVector(m, double_precision=double_precision)
    f.b[:] = b[:]
    f.c[:] = 0.5
    f.h[:] = pogs.FUNCTION["SQUARE"]

    # f(x) = Ind( x >= 0 )
    g = pogs.FunctionVector(n, double_precision=double_precision)
    g.h[:] = pogs.FUNCTION["INDGE0"]

    # intialize solver
    s = Solver(A)

    # solve
    s.solve(f, g)

    # get solve time
    t = s.info.solvetime

    # tear down solver in C++/CUDA
    s.finish()

    return t
コード例 #5
0
ファイル: lp_ineq.py プロジェクト: liq07lzucn/pogs-python
def LpIneq(m, n, gpu=False, double_precision=False):
    # set solver cpu/gpu according to input args
    if gpu and pogs.SolverGPU is None:
        print "\nGPU solver unavailable, using CPU solver\n"
        gpu = False

    Solver = pogs.SolverGPU if gpu else pogs.SolverCPU

    # Generate A according to:
    #   A = [-1 / n *rand(m-n, n); -eye(n)]
    A = -vstack((rand(m - n, n) / n, eye(n)))

    # cast A as float/double according to input args
    A = A if double_precision else float32(A)

    # Generate b according to:
    #   b = A * rand(n, 1) + 0.2 * rand(m, 1)
    b = A.dot(rand(n)) + 0.2 * rand(m)

    # Generate c according to:
    #   c = rand(n, 1)
    c = rand(n)

    # f(x) = Ind( (Ax-b) <= 0 )
    f = pogs.FunctionVector(m, double_precision=double_precision)
    f.b[:] = b[:]
    f.h[:] = pogs.FUNCTION["IDENTITY"]

    # g(x) = c^Tx
    g = pogs.FunctionVector(n, double_precision=double_precision)
    g.a[:] = c[:]
    g.h[:] = pogs.FUNCTION["IDENTITY"]

    # intialize solver
    s = Solver(A)

    # solve
    s.solve(f, g)

    # get solve time
    t = s.info.solvetime

    # tear down solver in C++/CUDA
    s.finish()

    return t
コード例 #6
0
ファイル: logistic.py プロジェクト: liq07lzucn/pogs-python
def Logistic(m, n, gpu=False, double_precision=False):
    # set solver cpu/gpu according to input args
    if gpu and pogs.SolverGPU is None:
        print "\nGPU solver unavailable, using CPU solver\n"
        gpu = False

    Solver = pogs.SolverGPU if gpu else pogs.SolverCPU

    # random matrix A
    A = randn(m, n)

    # cast A as float/double according to input args
    A = A if double_precision else float32(A)

    # true x vector, ~20% zeros
    x_true = (randn(n) / n) * float64(randn(n) < 0.8)

    # generate labels
    d = 1. / (1 + exp(-A.dot(x_true))) > rand(m)

    # lambda_max
    lambda_max = max(abs(A.T.dot(0.5 - d)))

    # f(y) = \sum_i -d_i y_i + log(1 + e ^ y_i)
    f = pogs.FunctionVector(m, double_precision=double_precision)
    f.d[:] = -d[:]
    f.h[:] = pogs.FUNCTION["LOGISTIC"]

    # g(x) = \lambda ||x||_1
    g = pogs.FunctionVector(n, double_precision=double_precision)
    g.a[:] = 0.5 * lambda_max
    g.h[:] = pogs.FUNCTION["ABS"]

    # intialize solver
    s = Solver(A)

    # solve
    s.solve(f, g)

    # get solve time
    t = s.info.solvetime

    # tear down solver in C++/CUDA
    s.finish()

    return t
コード例 #7
0
def LassoPath(m, n, gpu=False, double_precision=False, nlambda=50):
    # set solver cpu/gpu according to input args
    if gpu and pogs.SolverGPU is None:
        print "\nGPU solver unavailable, using CPU solver\n"
        gpu = False

    Solver = pogs.SolverGPU if gpu else pogs.SolverCPU

    # random matrix A
    A = randn(m, n)

    # cast A as float/double according to input args
    A = A if double_precision else float32(A)

    # true x vector, ~20% zeros
    x_true = (randn(n) / n) * float64(randn(n) < 0.8)

    # b= A*x_true + v (noise)
    b = A.dot(x_true) + 0.5 * randn(m)

    # lambda_max
    lambda_max = max(abs(A.T.dot(b)))

    # f(Ax) = ||Ax - b||_2^2
    f = pogs.FunctionVector(m, double_precision=double_precision)
    f.b[:] = b[:]
    f.h[:] = pogs.FUNCTION["SQUARE"]

    # g(x) = 0.2*lambda_max*||x||_1
    g = pogs.FunctionVector(n, double_precision=double_precision)
    g.a[:] = 0.2 * lambda_max
    g.h[:] = pogs.FUNCTION["ABS"]

    # store results for comparison
    x_prev = zeros(n)

    # timer
    runtime = 0.

    # use problem data A to create solver
    s = Solver(A)

    for i in xrange(nlambda):
        _lambda = exp(
            (log(lambda_max) *
             (nlambda - 1 - i) + 1e-2 * log(lambda_max) * i) / (nlambda - 1))

        g.c[:] = _lambda

        # solve
        s.solve(f, g)

        # add run time
        runtime += s.info.solvetime

        # copy
        x_curr = s.solution.x

        # check stopping condition
        if max(abs(x_prev - x_curr)) < 1e-3 * sum(abs(x_curr)):
            break

        x_prev[:] = x_curr[:]

    # tear down solver in C++/CUDA
    s.finish()

    return runtime