Example #1
0
def test_cd():
    np.random.seed(0)
    X = np.asfortranarray(np.random.normal(size = (64,100)))
    X = np.asfortranarray(X / np.tile(np.sqrt((X*X).sum(axis=0)),(X.shape[0],1)),dtype=myfloat)
    D = np.asfortranarray(np.random.normal(size = (64,100)))
    D = np.asfortranarray(D / np.tile(np.sqrt((D*D).sum(axis=0)),(D.shape[0],1)),dtype=myfloat)
    # parameter of the optimization procedure are chosen
    lambda1 = 0.015
    mode = spams.PENALTY
    tic = time.time()
    alpha = spams.lasso(X,D,lambda1 = lambda1,mode = mode,numThreads = 4)
    tac = time.time()
    t = tac - tic
    xd = X - D * alpha
    E = np.mean(0.5 * (xd * xd).sum(axis=0) + lambda1 * np.abs(alpha).sum(axis=0))
    print("%f signals processed per second for LARS" %(X.shape[1] / t))
    print('Objective function for LARS: %g' %E)
    tol = 0.001
    itermax = 1000
    tic = time.time()
#    A0 = ssp.csc_matrix(np.empty((alpha.shape[0],alpha.shape[1])))
    A0 = ssp.csc_matrix((alpha.shape[0],alpha.shape[1]),dtype=myfloat)
    alpha2 = spams.cd(X,D,A0,lambda1 = lambda1,mode = mode,tol = tol, itermax = itermax,numThreads = 4)
    tac = time.time()
    t = tac - tic
    print("%f signals processed per second for CD" %(X.shape[1] / t))
    xd = X - D * alpha2
    E = np.mean(0.5 * (xd * xd).sum(axis=0) + lambda1 * np.abs(alpha).sum(axis=0))
    print('Objective function for CD: %g' %E)
    print('With Random Design, CD can be much faster than LARS')

    return None
def test_cd():
    np.random.seed(0)
    X = np.asfortranarray(np.random.normal(size=(64, 100)))
    X = np.asfortranarray(X / np.tile(np.sqrt((X * X).sum(axis=0)),
                                      (X.shape[0], 1)),
                          dtype=myfloat)
    D = np.asfortranarray(np.random.normal(size=(64, 100)))
    D = np.asfortranarray(D / np.tile(np.sqrt((D * D).sum(axis=0)),
                                      (D.shape[0], 1)),
                          dtype=myfloat)
    # parameter of the optimization procedure are chosen
    lambda1 = 0.015
    mode = spams.PENALTY
    tic = time.time()
    alpha = spams.lasso(X, D, lambda1=lambda1, mode=mode, numThreads=4)
    tac = time.time()
    t = tac - tic
    xd = X - D * alpha
    E = np.mean(0.5 * (xd * xd).sum(axis=0) +
                lambda1 * np.abs(alpha).sum(axis=0))
    print("%f signals processed per second for LARS" % (X.shape[1] / t))
    print('Objective function for LARS: %g' % E)
    tol = 0.001
    itermax = 1000
    tic = time.time()
    #    A0 = ssp.csc_matrix(np.empty((alpha.shape[0],alpha.shape[1])))
    A0 = ssp.csc_matrix((alpha.shape[0], alpha.shape[1]), dtype=myfloat)
    alpha2 = spams.cd(X,
                      D,
                      A0,
                      lambda1=lambda1,
                      mode=mode,
                      tol=tol,
                      itermax=itermax,
                      numThreads=4)
    tac = time.time()
    t = tac - tic
    print("%f signals processed per second for CD" % (X.shape[1] / t))
    xd = X - D * alpha2
    E = np.mean(0.5 * (xd * xd).sum(axis=0) +
                lambda1 * np.abs(alpha).sum(axis=0))
    print('Objective function for CD: %g' % E)
    print('With Random Design, CD can be much faster than LARS')

    return None
def sparse_codes(X, D, lamda):
  """ Map input data in X to sparse codes using dictionary D using
     Coordinate Descent from SPAM package

     Parameters:
      X:     Input data as (num_patches, input_dim) matrix
      D:     Distonary of features
  """
  # Map X and D to fortran array format
  X0 = np.asfortranarray(np.transpose(X))
  D0 = np.asfortranarray(np.transpose(D))

  # Get a sparse matrix
  A0 = csc_matrix(np.zeros((D.shape[0], X.shape[0])))
  out = spams.cd(X0, D0, A0,
                 lambda1 = lamda, mode = 2, itermax = 1000, tol = 0.001,
                 numThreads = -1)
  return np.transpose(out).todense()
def sparse_codes(X, D, lamda):
    """ Map input data in X to sparse codes using dictionary D using
     Coordinate Descent from SPAM package

     Parameters:
      X:     Input data as (num_patches, input_dim) matrix
      D:     Distonary of features
  """
    # Map X and D to fortran array format
    X0 = np.asfortranarray(np.transpose(X))
    D0 = np.asfortranarray(np.transpose(D))

    # Get a sparse matrix
    A0 = csc_matrix(np.zeros((D.shape[0], X.shape[0])))
    out = spams.cd(X0,
                   D0,
                   A0,
                   lambda1=lamda,
                   mode=2,
                   itermax=1000,
                   tol=0.001,
                   numThreads=-1)
    return np.transpose(out).todense()