Пример #1
0
def test_all_adds_together(sparse_type):
    psObj = ps.ProjSplitFit()
    obs = sparse_type([[1, 2, 3], [4, 5, 6]])
    y = [1, 1]
    H = sparse_type([[1, 1, 7, 8], [7, 9, 7, 8], [4, 4, 3, 4]])
    psObj.addData(obs, y, 2, linearOp=H)
    regObj = regularizers.L1()
    G = sparse_type([[1, 1, 1, 11], [7, 7, 11, 42]])
    psObj.addRegularizer(regObj, linearOp=G)
Пример #2
0
        if False:
            outfrb = algo.for_reflect_back(theFunc,proxfstar_4_tseng,proxg,theGrad,init,iter=maxIter,
                                       gamma0=tuned,gamma1=tuned,G=G,Gt=Gt,verbose=False,getFuncVals=False)
            results[(i,'frb')] =  outfrb.finalFuncVal

        if loss == "log":
            loss2use = "logistic"
        else:
            loss2use = 2

        gamma = 1.0
        if True :
            psObj = ps.ProjSplitFit(gamma)
            proc = lp.Forward2Backtrack()
            psObj.addData(X,y,loss2use,linearOp=H,normalize=False,process=proc,
                          embed=regularizers.L1(scaling=(1-mu)*lam))
            (nbeta,ngamma) = H.shape
            shape = (ngamma-1,ngamma)
            G_for_ps = sl.LinearOperator(shape,matvec=lambda x: x[:-1],rmatvec = lambda x : np.concatenate((x,np.array([0]))))
            psObj.addRegularizer(regularizers.L1(scaling = mu*lam,step=tuned),linearOp=G_for_ps)
            psObj.run(nblocks=10,maxIterations=maxIter,verbose=False,keepHistory=False,
                      primalTol=0.0,dualTol=0.0,blockActivation="greedy")
            results[(i,'ps2fembed_g')] = psObj.getObjective()


        if False:
            psObj = ps.ProjSplitFit(gamma)
            psObj.addData(X,y,loss2use,linearOp=H,normalize=False,process=lp.Forward1Backtrack(),
                          embed=regularizers.L1(scaling=(1-mu)*lam))
            (nbeta,ngamma) = H.shape
            shape = (ngamma-1,ngamma)
    embed = False

    if run2f:
        print("2f")
        gamma2f = gamma2fs[i]
        t0 = time.time()
        psObj = ps.ProjSplitFit(gamma2f)

        if embed:
            psObj.addData(X,
                          y,
                          loss2use,
                          linearOp=H,
                          normalize=False,
                          process=lp.Forward2Backtrack(),
                          embed=regularizers.L1(scaling=(1 - mu) * lam))
        else:
            psObj.addData(X,
                          y,
                          loss2use,
                          linearOp=H,
                          normalize=False,
                          process=lp.Forward2Backtrack())
            psObj.addRegularizer(regularizers.L1(scaling=(1 - mu) * lam),
                                 linearOp=H)

        (nbeta, ngamma) = H.shape
        shape = (ngamma - 1, ngamma)
        G_for_ps = sl.LinearOperator(shape,
                                     matvec=lambda x: x[:-1],
                                     rmatvec=lambda x: np.concatenate(
Пример #4
0
mu = 0.5
lam = 1e-4


def applyG(x):
    return x[:-1]


def applyGtranspose(v):
    return np.append(v, 0.0)


(_, nv) = H.shape
shape = (nv - 1, nv)
G = LinearOperator(shape, matvec=applyG, rmatvec=applyGtranspose)
projSplit.addRegularizer(regularizers.L1(scaling=mu * lam), linearOp=G)

### second regularizer
regObj2 = regularizers.L1(scaling=lam * (1 - mu))
projSplit.addRegularizer(regObj2, linearOp=H)

### In some cases there is a random block selection even though we use greedy
### This occurs when no blocks have negative \phi_i
### Fix the random number seed so results are reproducible
np.random.seed(1)

# Set dual scaling parameter
projSplit.setDualScaling(1e-4)

# Problem is for a limited number of iterations since it is very difficult
projSplit.run(nblocks=10, maxIterations=20000, verbose=True, keepHistory=True)
Пример #5
0
def test_ls_with_lin_op(sparse_type, INT, Norm, reg):

    gamma = 1e0

    psObj = ps.ProjSplitFit(gamma)
    m = 25
    d = 15
    d2 = 11
    p = 7
    nu = 1e-4
    if getNewOptVals:
        A = cache.get('Als')
        y = cache.get('yls')
        H = cache.get('Hls')
        Hreg = cache.get('Hregls')
        if A is None:
            A = np.random.normal(0, 1, [m, d])
            y = np.random.normal(0, 1, m)
            H = np.random.normal(0, 1, [d, d2])
            Hreg = np.random.normal(0, 1, [p, d2])
            cache['Als'] = A
            cache['yls'] = y
            cache['Hls'] = H
            cache['Hregls'] = Hreg
    else:
        A = cache.get('Als')
        y = cache.get('yls')
        H = cache.get('Hls')
        Hreg = cache.get('Hregls')

    H = sparse_type(H)
    Hreg = sparse_type(Hreg)
    psObj.addData(A, y, 2, linearOp=H, intercept=INT, normalize=Norm)
    if reg:
        psObj.addRegularizer(regularizers.L1(scaling=nu), linearOp=Hreg)

    psObj.run(nblocks=10, maxIterations=1000)
    psOpt = psObj.getObjective()

    if getNewOptVals:
        opt = cache.get(('lsOpt', INT, Norm, reg))
        if opt is None:
            xcvx = cvx.Variable(d2 + 1)
            H = H.toarray()
            if Norm:
                n = A.shape[0]
                scaling = np.linalg.norm(A, axis=0)
                scaling += 1.0 * (scaling < 1e-10)
                A = np.sqrt(n) * A / scaling
            col2Add = int(INT) * np.ones((m, 1))
            A = np.concatenate((col2Add, A), axis=1)

            zeros2add = np.zeros((d, 1))
            H = np.concatenate((zeros2add, H), axis=1)
            zeros2add = np.zeros((1, d2 + 1))
            H = np.concatenate((zeros2add, H), axis=0)
            H[0, 0] = 1

            f = (1 / (2 * m)) * cvx.sum_squares(A @ H @ xcvx - y)
            if reg:
                Hreg = Hreg.toarray()
                zeros2add = np.zeros((p, 1))
                Hreg = np.concatenate((zeros2add, Hreg), axis=1)

                f += nu * cvx.norm(Hreg @ xcvx, 1)

            prob = cvx.Problem(cvx.Minimize(f))
            prob.solve(verbose=False)
            opt = prob.value
            cache[('lsOpt', INT, Norm)] = opt
    else:
        opt = cache.get(('lsOpt', INT, Norm))

    print(f"psOpt = {psOpt}")
    print(f"cvxOpt = {opt}")
    assert psOpt - opt < 1e-2
Пример #6
0
def test_add_regularizer_works_with_sparse_linearOp(sparse_type):
    psObj = ps.ProjSplitFit()
    regObj = regularizers.L1()
    H = sparse_type([[1, 1, 7, 8], [7, 9, 7, 8], [4, 4, 3, 4]])
    psObj.addRegularizer(regObj, linearOp=H)
Пример #7
0
m = 500
d = 200
groupSize = 10

np.random.seed(1)
A = np.random.normal(0, 1, [m, d])
trueCoefs = 0.01 * np.array(range(d))
r = A @ trueCoefs + np.random.normal(0, 1, m)

ngroups = d // groupSize
groups = [range(i * groupSize, (i + 1) * groupSize) for i in range(ngroups)]
print(groups)

projSplit = ps.ProjSplitFit()
projSplit.addData(A, r, loss=2, intercept=False)
projSplit.addRegularizer(rg.L1(scaling=1.0))
projSplit.addRegularizer(rg.groupL2(d, groups, scaling=1.0))

projSplit.run(verbose=True)

print(f"Objective value = {projSplit.getObjective()}")
print()

try:
    import matplotlib.pyplot as plt
    sol = projSplit.getSolution()
    plt.style.use('ggplot')
    plt.bar(range(d), sol)
    plt.show()
except:
    print(
Пример #8
0
print("================")

print("running ProjSplitFit")
X = sp.load_npz('data/trip_advisor/S_train.npz') # training matrix
H = sp.load_npz('data/trip_advisor/S_A.npz')     # this matrix is called H
y = np.load('data/trip_advisor/y_train.npy')     # training labels

f_ps2fg = []
t_ps2fg = []
f_psbg = []
t_psbg = []
for erg in [False,'simple','weighted']:
    t0 = time.time()
    psObj = ps.ProjSplitFit(gamma2fg)
    psObj.addData(X,y,2,linearOp=H,normalize=False,process=lp.Forward2Backtrack())
    psObj.addRegularizer(regularizers.L1(scaling=(1-mu)*lam),linearOp=H)
    (nbeta,ngamma) = H.shape
    shape = (ngamma-1,ngamma)
    G_for_ps = sl.LinearOperator(shape,matvec=lambda x: x[:-1],rmatvec = lambda x : np.concatenate((x,np.array([0]))))
    psObj.addRegularizer(regularizers.L1(scaling = mu*lam),linearOp=G_for_ps)
    psObj.run(nblocks=10,maxIterations=iterOverwrite,verbose=False,keepHistory=True,historyFreq=1,
                      primalTol=0.0,dualTol=0.0,ergodic=erg)
    f_ps2fg.append(psObj.getHistory()[0])
    t_ps2fg.append(psObj.getHistory()[1])
    t1 = time.time()
    print(f"ps2fbt_g total running time {t1-t0}")

    t0 = time.time()
    psObj = ps.ProjSplitFit(gammabg)
    psObj.addData(X,y,2,linearOp=H,normalize=False,process=lp.BackwardCG())
    psObj.addRegularizer(regularizers.L1(scaling=(1-mu)*lam),linearOp=H)