Example #1
0
def test():
    ''' test against matlab routine. double check! because there ain't no CVX for this'''
    
    import scipy.io as spio
    import matplotlib.pyplot as plt
    import designerConv
    import convFourier
    
    # load data from a fakel1 data set created by 'testL1.m'
    D = spio.loadmat('fakeL1.mat')
    m = D['m'].astype('int64').flatten()
    p = D['p'].astype('int64').flatten()
    q = D['q'].astype('int64').flatten()
    wTrue = D['wTrue']
    y = D['sig'].flatten()
    
    print 'size of m,p,q ' + repr(m) + ' ' + repr(p) + ' ' + repr(q)
    print 'and thes is mp ' + repr(m*p)
    rho = 1
    lmb = 0.2
    W = lasso(m,(p+1)*m,rho,lmb)
    
#    A = designerConv.convOperator(m,p,q)
    A = convFourier.convFFT(m,p,q)
    A.changeWeights(wTrue)
    
    
    print wTrue.shape
    print y.shape
    
    z = W.solveL1(y, A)
    
    zTrue = D['zTrue'].flatten()
    zM = D['zd'].flatten()
    
    print np.linalg.norm(zM-z)
    
    plt.figure(83)
    plt.plot(range(z.size), np.abs(z), range(zTrue.size), np.abs(zTrue),
             range(zM.size), np.abs(zM))
    
    plt.show()
Example #2
0
def test():
    ''' test against matlab routine. double check! because there ain't no CVX for this'''

    import scipy.io as spio
    import matplotlib.pyplot as plt
    import designerConv
    import convFourier

    # load data from a fakel1 data set created by 'testL1.m'
    D = spio.loadmat('fakeL1.mat')
    m = D['m'].astype('int64').flatten()
    p = D['p'].astype('int64').flatten()
    q = D['q'].astype('int64').flatten()
    wTrue = D['wTrue']
    y = D['sig'].flatten()

    print 'size of m,p,q ' + repr(m) + ' ' + repr(p) + ' ' + repr(q)
    print 'and thes is mp ' + repr(m * p)
    rho = 1
    lmb = 0.2
    W = lasso(m, (p + 1) * m, rho, lmb)

    #    A = designerConv.convOperator(m,p,q)
    A = convFourier.convFFT(m, p, q)
    A.changeWeights(wTrue)

    print wTrue.shape
    print y.shape

    z = W.solveL1(y, A)

    zTrue = D['zTrue'].flatten()
    zM = D['zd'].flatten()

    print np.linalg.norm(zM - z)

    plt.figure(83)
    plt.plot(range(z.size), np.abs(z), range(zTrue.size), np.abs(zTrue),
             range(zM.size), np.abs(zM))

    plt.show()
Example #3
0
def test():
    import convFourier
    import time
    import matplotlib.pyplot as plt
    p = 5
    q = 10
    m = 1000
    ''' create the operator, initialize '''
    A = convFourier.convFFT(m, p, q)
    w = [
        np.random.randn(q, p) / np.sqrt(q),
        np.random.randn(q, p) / np.sqrt(q)
    ]
    A.changeWeights(w[0])

    C = convFourier.convFFT(m, p, q)
    C.changeWeights(w[1])
    '''create random x'''
    x = [
        np.random.randn(m * (p + 1)).astype('complex128'),
        np.random.randn(m * (p + 1)).astype('complex128')
    ]

    B = [dtm(m, p, q, fourier=True) for ix in xrange(2)]
    for Q, wl in zip(B, w):
        Q.changeWeights(wl)
    ''' apply functional operator and its transpose '''
    tm = time.time()
    yf = [A.mtx(x[0]), C.mtx(x[1])]
    print 'single itme ' + repr(time.time() - tm)

    tm = time.time()
    yp = [Q.mtx(xl) for Q, xl in zip(B, x)]
    print 'new time ' + repr(time.time() - tm)

    print 'yf s ' + repr(yf[0].shape)
    print 'yp s ' + repr(yp[0].shape)

    for a, b in zip(yf, yp):
        print 'diff ' + repr(np.linalg.norm(a - b))

    tm = time.time()
    g = [A.mtxT(yf[0]), C.mtxT(yf[1])]
    print 'making list time ' + repr(time.time() - tm)

    tm = time.time()
    h = [Q.mtxT(yl) for Q, yl in zip(B, yp)]
    print 'new time ' + repr(time.time() - tm)

    for a, b in zip(g, h):
        print 'diff ' + repr(np.linalg.norm(a - b))

    plt.figure(1)
    plt.subplot(2, 1, 1)
    plt.plot(range(yf[0].size), yf[0].real, np.arange(yp[0].size), yp[0].real)

    plt.subplot(2, 1, 2)
    plt.plot(range(yf[1].size), yf[1].real, np.arange(yp[1].size), yp[1].real)

    plt.figure(2)
    plt.subplot(2, 1, 1)
    plt.plot(range(g[0].size), g[0].real, range(h[0].size), h[0].real)

    plt.subplot(2, 1, 2)
    plt.plot(range(g[1].size), g[1].real, range(h[1].size), h[1].real)

    plt.show()

    return yp
Example #4
0
def test():
    ''' a testing routine to make sure that these objects behave as I expect '''
    import convFourier
    import time
    import matplotlib.pyplot as plt
    p = 5
    q = 10
    m = 1000
    
    ''' create the operator, initialize '''
    A = convFourier.convFFT(m,p,q)
    w = [np.random.randn(q,p)/np.sqrt(q), np.random.randn(q,p)/np.sqrt(q)]
    A.changeWeights(w[0])
    
    C = convFourier.convFFT(m,p,q)
    C.changeWeights(w[1])
    '''create random x'''
    x = [np.random.randn(m*(p+1)).astype('complex128'), np.random.randn(m*(p+1)).astype('complex128')]
    
    
    B = [dtm(m,p,q,fourier=True) for ix in xrange(2)]
    for Q,wl in zip(B,w):
        Q.changeWeights(wl)

        
    ''' apply functional operator and its transpose '''
    tm = time.time()
    yf = [A.mtx(x[0]), C.mtx(x[1])]
    print 'single itme ' + repr(time.time()-tm)
    
    tm = time.time()
    yp = [Q.mtx(xl) for Q,xl in zip(B,x)] 
    print 'new time ' + repr(time.time()-tm)
    
    print 'yf s ' + repr(yf[0].shape)
    print 'yp s ' + repr(yp[0].shape)
    
    for a,b in zip(yf,yp):
        print 'diff ' + repr(np.linalg.norm(a-b))
        
    tm = time.time()
    g = [A.mtxT(yf[0]), C.mtxT(yf[1])]
    print 'making list time ' + repr(time.time()-tm)
    
    tm = time.time()
    h = [Q.mtxT(yl) for Q,yl in zip(B,yp)]
    print 'new time ' + repr(time.time()-tm)
    
    for a,b in zip(g,h):
        print 'diff ' + repr(np.linalg.norm(a-b))
    
    plt.figure(1)
    plt.subplot(2,1,1)
    plt.plot(range(yf[0].size), yf[0].real, np.arange(yp[0].size), yp[0].real)
    
    plt.subplot(2,1,2)
    plt.plot(range(yf[1].size), yf[1].real, np.arange(yp[1].size), yp[1].real)

    plt.figure(2)
    plt.subplot(2,1,1)
    plt.plot(range(g[0].size), g[0].real, range(h[0].size), h[0].real)
    
    plt.subplot(2,1,2)
    plt.plot(range(g[1].size), g[1].real, range(h[1].size), h[1].real)
    
    plt.show()
    
    return yp
Example #5
0
def main():
    ''' main routine '''
    comm = MPI.COMM_WORLD
    rk = comm.Get_rank()
    nProc = comm.Get_size()

    plain = True
    if len(sys.argv) >= 2:
        if sys.argv[1] == 'plain':
            plain = False

    if len(sys.argv) >= 3:
        dts = sys.argv[2]
    else:
        dts = 'plmr'

    m = 50000  # size of data
    p = 25  # number of filters
    q = 300  # length of filters

    if dts == 'plmr':
        rho = 1
        lmb = 0.5
        xi = 0.2
    elif dts == 'mpk':
        rho = 0.1
        lmb = 1e-6
        xi = 0.2

    fac = 1.0
    # np.sqrt((m/q)/2.0)
    ''' initialize MPI routine '''
    ''' load data, given rank '''
    y = getData(m, dts, rank=rk)
    ''' initialize weights '''
    # D = spio.loadmat('fakew.mat')
    # wt = D['wini']
    #    wt = np.random.randn(q,p)/np.sqrt(q) #D['w']

    wt = weightInit(p, q, comm)

    if plain:
        A = convFFT(m, p, q, fct=fac)
        optl1 = lasso(m, m * (p + 1), rho, lmb)

    else:
        print 'Doing plain!'
        A = convOperator(m, p, q)
        optl1 = lasso(m, m * (p), rho, lmb)

    newWW = weightsUpdate.weightsUpdate(m, p, q, xi, fct=fac)
    newWW.wp = wt

    rrz = list()
    gap = list()
    ''' begin loop '''
    for itz in range(1000):
        ws = newWW.wp
        A.changeWeights(newWW.wp)
        tm = time()
        z = optl1.solveL1(y, A)
        rrz = optl1.rrz  # .append(optl1.rrz.pop())
        gap = optl1.gap  #.append(optl1.gap.pop())
        print 'rank ' + repr(rk) + ' of ' + repr(
            nProc) + ' solved l1 itr: ' + repr(itz) + ' time: ' + repr(time() -
                                                                       tm)
        tm = time()

        if plain:
            wmx = newWW.updateFourier(y, wt, z)
        else:
            wmx = newWW.updatePlain(y, wt, z)

        print 'rank ' + repr(rk) + ' of ' + repr(
            nProc) + ' solved w update itr: ' + repr(itz) + ' time: ' + repr(
                time() - tm)
        tm = time()
        wt = weightAgg(wmx, p, q, comm)
        wp = newWW.wp
        print 'rank ' + repr(rk) + ' of ' + repr(
            nProc) + ' have new weights itr: ' + repr(itz) + ' time: ' + repr(
                time() - tm)
        outd = {
            'y': y,
            'z': z,
            'wt': wt,
            'wp': wp,
            'm': m,
            'p': p,
            'q': q,
            'rho': rho,
            'lmb': lmb,
            'xi': xi,
            'rrz': rrz,
            'gap': gap,
            'ws': ws
        }

        if plain & (dts == 'plmr'):
            spio.savemat(
                'miniOut_' + repr(p) + '_' + repr(nProc) + '_' + repr(rk),
                outd)
        elif plain & (dts == 'mpk'):
            spio.savemat('miniOutMPK_' + repr(nProc) + '_' + repr(rk), outd)
        else:
            spio.savemat('plainOut_' + repr(nProc) + '_' + repr(rk), outd)

    return y, z, optl1, A, wt
    ''' for n iterations '''
    ''' calculate local weights '''
    ''' all reduce '''
Example #6
0
def main():
    ''' main routine '''
    comm = MPI.COMM_WORLD
    rk = comm.Get_rank()
    nProc = comm.Get_size()
    
    plain = True
    if len(sys.argv) >= 2:
        if sys.argv[1] == 'plain':
            plain = False
    
    if len(sys.argv) >= 3:
        dts = sys.argv[2]
    else:
        dts = 'plmr'
        
        
        
    m = 50000 # size of data
    p = 25 # number of filters
    q = 300 # length of filters
    
    if dts == 'plmr':
        rho = 1
        lmb = 0.5
        xi = 0.2
    elif dts == 'mpk':
        rho = 0.1
        lmb = 1e-6
        xi = 0.2
        
    fac = 1.0; # np.sqrt((m/q)/2.0)
    ''' initialize MPI routine '''
    
    
    ''' load data, given rank '''
    y = getData(m,dts,rank=rk)

    
    ''' initialize weights '''
    # D = spio.loadmat('fakew.mat')
    # wt = D['wini']
#    wt = np.random.randn(q,p)/np.sqrt(q) #D['w']
    
    
    wt = weightInit(p,q,comm)
    
    if plain:
        A = convFFT(m,p,q,fct=fac)
        optl1 = lasso(m,m*(p+1),rho,lmb)
        
    else:
        print 'Doing plain!'
        A = convOperator(m,p,q)
        optl1 = lasso(m,m*(p),rho,lmb)
        
        
    newWW = weightsUpdate.weightsUpdate(m,p,q,xi,fct=fac)
    newWW.wp = wt
    
    rrz = list()
    gap = list()
    ''' begin loop '''
    for itz in range(1000):
        ws = newWW.wp
        A.changeWeights(newWW.wp)
        tm = time()
        z = optl1.solveL1(y, A)
        rrz = optl1.rrz # .append(optl1.rrz.pop())
        gap = optl1.gap #.append(optl1.gap.pop())
        print 'rank ' + repr(rk) + ' of ' + repr(nProc) +  ' solved l1 itr: ' + repr(itz) + ' time: ' + repr(time()-tm)
        tm = time()
        
        if plain:
            wmx = newWW.updateFourier(y, wt, z)
        else:
            wmx = newWW.updatePlain(y, wt, z)
            
        print 'rank ' + repr(rk) + ' of ' + repr(nProc) +  ' solved w update itr: ' + repr(itz) + ' time: ' + repr(time()-tm)
        tm = time()
        wt = weightAgg(wmx,p,q,comm)
        wp = newWW.wp
        print 'rank ' + repr(rk) + ' of ' + repr(nProc) +  ' have new weights itr: ' + repr(itz) + ' time: ' + repr(time()-tm)
        outd = {'y':y, 'z':z, 'wt':wt,'wp':wp,'m':m,'p':p,'q':q, 'rho':rho,'lmb':lmb, 'xi':xi, 'rrz':rrz,'gap':gap, 'ws':ws }
        
        if plain & (dts == 'plmr'):
            spio.savemat('miniOut_' + repr(p) + '_' + repr(nProc) + '_' + repr(rk), outd)
        elif plain & (dts == 'mpk'):
            spio.savemat('miniOutMPK_' + repr(nProc) + '_' + repr(rk), outd)
        else:
            spio.savemat('plainOut_' + repr(nProc) + '_' + repr(rk), outd) 
    
    return y,z,optl1,A,wt
    
    ''' for n iterations '''
    
    ''' calculate local weights '''
    
    ''' all reduce '''