def _objective(X,D,param,imgname = None):
    print('Evaluating cost function...')
    lparam = _extract_lasso_param(param)
    alpha = spams.lasso(X,D = D,**lparam)
    # NB : as alpha is sparse, D*alpha is the dot product
    xd = X - D * alpha
    R = np.mean(0.5 * (xd * xd).sum(axis=0) + param['lambda1'] * np.abs(alpha).sum(axis=0))
    print("objective function: %f" %R)
    #* display ?
    if imgname != None:
        img = spams.displayPatches(D)
        print("IMG %s" %str(img.shape))
        x = np.uint8(img[:,:,0] * 255.)
        image = Image.fromarray(x,mode = 'L')
        image.save("%s.png" %imgname)
Example #2
0
def _objective(X,D,param,imgname = None):
    print 'Evaluating cost function...'
    lparam = _extract_lasso_param(param)
    alpha = spams.lasso(X,D = D,**lparam)
    # NB : as alpha is sparse, D*alpha is the dot product
    xd = X - D * alpha
    R = np.mean(0.5 * (xd * xd).sum(axis=0) + param['lambda1'] * np.abs(alpha).sum(axis=0))
    print "objective function: %f" %R
    #* display ?
    if imgname != None:
        img = spams.displayPatches(D)
        print "IMG %s" %str(img.shape)
        x = np.uint8(img[:,:,0] * 255.)
        image = Image.fromarray(x,mode = 'L')
        image.save("%s.png" %imgname)
                            dtype=np.float64)

    if m == 'pca':
        svd = TruncatedSVD(n_components=D, n_iter=7)
        W = svd.fit_transform(X_m)
    else:
        param = {'K': D, 'numThreads': -1, 'batchsize': 256, 'iter': 1000}

        if m == 'nmf':
            W = nmf(X_m, **param)
        else:
            param['verbose'] = False
            if m == 'spca':
                param['lambda1'] = 0.1
                param['gamma1'] = 0.1
                param['modeD'] = 1
                W = trainDL(X_m, **param)
            elif m == 'dl':
                param['lambda1'] = 0.1
                param['mode'] = 2
                W = trainDL(X_m, **param)

    # resulting basisvectors will be visualized as image patches.
    img = displayPatches(W)
    img = np.uint8(img[:, :, 0] * 255.)
    img = Image.fromarray(img, mode='L')
    plt.title('sparse_dict_demo_{}'.format(m))
    plt.imshow(img, cmap='gray')
    pml.savefig('sparse_dict_demo_{}.pdf'.format(m))
    plt.show()
#Da = spams.calcXAt(D,ssp.csc_matrix(alpha.T))
a = alpha.todense()
print "XXa %s" %str(a.shape)
Da = np.dot(D,a)
#Da = D * alpha
xd = y - Da
print "YY D %s Da %s y %s %s alpah %s xd %s %s" %(str(D.shape),str(Da.shape),str(y.shape),type(y),str(alpha.shape),str(xd.shape),type(xd))
#R = np.mean(0.5 * (xd * xd).sum(axis=0) + param['lambda1'] * np.abs(alpha).sum(axis=0))
#R = 0.5 * (xd * xd).sum(axis=0)
R = np.multiply(xd,xd)
R = R.sum(axis=0)
R += param['lambda1'] * np.abs(alpha).sum(axis=0)
R = np.mean(R)
print "objective function: %f" %R
# display ????
img = spams.displayPatches(D)
print "IMG %s" %str(img.shape)
x = np.uint8(img[:,:,0] * 255.)
#image = Image.fromarray(x,mode = 'L')
image = Image.fromarray(x,mode = 'L')
image.save('xx.png')
#### SECOND EXPERIMENT ####
print "*********** SECOND EXPERIMENT ***********"

X1 = X[:,0:X.shape[1]/2]
X2 = X[:,X.shape[1]/2 -1:]
param['iter'] = 500
tic = time.time()
(D,model) = spams.trainDL(X1,return_model = True,**param)
tac = time.time()
t = tac - tic