Beispiel #1
0
def test_f1backtrack(gf):

    projSplit = ps.ProjSplitFit()
    m = 10
    d = 20
    if getNewOptVals and (gf == 1.0):
        A = np.random.normal(0, 1, [m, d])
        y = np.random.normal(0, 1, m)
        cache['Af1bt'] = A
        cache['yf1bt'] = y
    else:
        A = cache['Af1bt']
        y = cache['yf1bt']

    processor = lp.Forward1Backtrack(growFactor=gf, growFreq=10)

    projSplit.setDualScaling(1e-1)
    projSplit.addData(A, y, 2, processor, intercept=True, normalize=True)

    vec = projSplit.getScaling()
    assert len(vec) == d

    projSplit.run(maxIterations=None,
                  keepHistory=True,
                  primalTol=1e-3,
                  dualTol=1e-3,
                  nblocks=5)
    ps_val = projSplit.getObjective()

    if getNewOptVals and (gf == 1.0):
        AwithIntercept = np.zeros((m, d + 1))
        AwithIntercept[:, 0] = np.ones(m)
        AwithIntercept[:, 1:(d + 1)] = A
        result = np.linalg.lstsq(AwithIntercept, y, rcond=None)
        xhat = result[0]
        LSval = 0.5 * np.linalg.norm(AwithIntercept.dot(xhat) - y, 2)**2 / m
        cache['optf1bt'] = LSval
    else:
        LSval = cache['optf1bt']

    assert ps_val - LSval < 1e-2
Beispiel #2
0
    H = np.random.normal(0,1,[d2,p])
    try:
        projSplit.addData(A,y,2,processor,normalize=False,intercept=False,
                      linearOp = aslinearoperator(H))
        notExcept = True

    except:
        notExcept = False

    assert notExcept == False


f2fix = lp.Forward2Fixed()
back2exact = lp.BackwardExact()
backCG = lp.BackwardCG()
f1bt = lp.Forward1Backtrack()
backLB = lp.BackwardLBFGS()
TryAll = []
testNumber = 0
for i in [False,True]:
    for j in [False,True]:
        for k in [False,True]:
            for l in [False,True]:
                for p in [backLB,f2fix,back2exact,f1bt,backCG]:
                    TryAll.append((i,j,k,l,p,testNumber))
                    testNumber += 1

@pytest.mark.parametrize("norm,inter,addL1,add2L1,processor,testNumber",TryAll)
def test_linear_op_data_term(norm,inter,addL1,add2L1,processor,testNumber):

            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)
            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=1,maxIterations=maxIter,verbose=False,keepHistory=False,
                      primalTol=0.0,dualTol=0.0)
            results[(i,'ps1fembed')] = psObj.getObjective()

        if False:
            psObj = ps.ProjSplitFit(gamma)
            psObj.addData(X,y,loss2use,linearOp=H,normalize=False,process=lp.BackwardLBFGS())
            psObj.addRegularizer(regularizers.L1(scaling=(1-mu)*lam),linearOp=H)
            (nbeta,ngamma) = H.shape
            shape = (ngamma-1,ngamma)
        t_ps2f = psObj.getHistory()[1]
        t1 = time.time()
        print(f"ps2f total running time {t1-t0}")

    if run1f:
        print("1f")
        gamma1f = gamma1fs[i]
        t0 = time.time()
        psObj = ps.ProjSplitFit(gamma1f)
        if embed:
            psObj.addData(X,
                          y,
                          loss2use,
                          linearOp=H,
                          normalize=False,
                          process=lp.Forward1Backtrack(),
                          embed=regularizers.L1(scaling=(1 - mu) * lam))
        else:
            psObj.addData(X,
                          y,
                          loss2use,
                          linearOp=H,
                          normalize=False,
                          process=lp.Forward1Backtrack())
            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],