예제 #1
0
 def test_beale(self):
     x0 = np.zeros(2)
     x, iter = gd(of.beale, fd_complex_step, x0)
     self.assertTrue(np.allclose(x, np.array([3, 0.5])))
예제 #2
0
 def test_bohachevsky3(self):
     x0 = np.ones(2) * 200
     x, iter = gd(of.bohachevsky3, of.grad_bohachevsky3, x0)
     self.assertTrue(np.allclose(x, np.zeros(x.shape)))
예제 #3
0
 def test_ackley(self):
     # This has many minima, make sure we only get the closest one
     x0 = np.zeros(10)
     x, iter = gd(of.ackley, of.grad_ackley, x0)
     self.assertTrue(np.allclose(x, np.zeros(x.shape)))
예제 #4
0
 def test_sphere(self):
     x0 = np.random.random(10)
     x, iter = gd(of.sphere, fd_complex_step, x0)
     self.assertTrue(np.allclose(x, np.zeros(x.shape)))
예제 #5
0
 def test_maxiter(self):
     x0 = 6
     num_iter = 5
     with self.assertWarnsRegex(Warning, 'GD hit maxiters!'):
         x, iter = gd(of.quadratic, fd_complex_step, x0, iter=num_iter)
     self.assertEqual(iter, num_iter)
예제 #6
0
 def test_rastrigin(self):
     x0 = np.ones(10)
     x, iter = gd(of.rastrigin, fd_complex_step, x0)
     self.assertTrue(np.allclose(x, np.zeros(x.shape)))
예제 #7
0
def cosamp(A,y,k,lstsq='exact',tol=1e-8,maxiter=500,x=None,disp=False):
    '''Compressive sampling matching pursuit (CoSaMP) algorithm.

    A -- Measurement matrix.
    y -- Measurements (i.e., y = Ax).
    k -- Number of expected nonzero coefficients.
    lstsq -- How to solve intermediate least squares problem.
    tol -- Stopping criteria.
    maxiter -- Maximum number of iterations.
    x -- True signal we are trying to estimate.
    disp -- Whether or not to display iterations.

    lstsq function:
        lstsq = { 'exact', 'lm', 'gd' }.

        'exact' solves it using numpy's linalg.lstsq method.
        'lm' uses solves with the Levenberg-Marquardt algorithm.
        'gd' uses 3 iterations of a gradient descent solver.

    Implements Algorithm 8.7 from:
        Eldar, Yonina C., and Gitta Kutyniok, eds. Compressed sensing: theory
        and applications. Cambridge University Press, 2012.
    '''

    # length of measurement vector and original signal
    n,N = A.shape[:]

    # Initializations
    x_hat = np.zeros(N,dtype=y.dtype)
    r = y.copy()

    # Decide how we want to solve the intermediate least squares problem
    if lstsq == 'exact':
        lstsq_fun = lambda A0,y: np.linalg.lstsq(A0,y,rcond=None)[0]
    elif lstsq == 'lm':
        from scipy.optimize import least_squares
        lstsq_fun = lambda A0,y: least_squares(lambda x: np.linalg.norm(y - np.dot(A0,x)),np.zeros(A0.shape[1],dtype=y.dtype))['x']
    elif lstsq == 'gd':
        # This doesn't work very well...
        from mr_utils.optimization import gd,fd_complex_step
        lstsq_fun = lambda A0,y: gd(lambda x: np.linalg.norm(y - np.dot(A0,x)),fd_complex_step,np.zeros(A0.shape[1],dtype=y.dtype),iter=3)[0]
    else:
        raise NotImplementedError()

    # Start up a table
    if disp:
        table = Table([ 'iter','residual','MSE' ],[ len(repr(maxiter)),8,8 ],[ 'd','e','e' ])
        hdr = table.header()
        for line in hdr.split('\n'):
            logging.info(line)

    for ii in range(maxiter):

        # Get step direction
        g = np.dot(A.T,r)

        # Add 2*k largest elements of g to support set
        Tn = np.union1d(x_hat.nonzero()[0],np.argsort(np.abs(g))[-(2*k):])

        # Solve the least squares problem
        xn = np.zeros(N)
        xn[Tn] = lstsq_fun(A[:,Tn],y)

        xn[np.argsort(np.abs(xn))[:-k]] = 0
        x_hat = xn.copy()

        # Compute new residual
        r = y - np.dot(A,x_hat)

        # Compute stopping criteria
        stop_criteria = np.linalg.norm(r)/np.linalg.norm(y)

        # Show MSE at current iteration if we wanted it
        if disp:
            logging.info(table.row([ ii,stop_criteria,np.mean((np.abs(x - x_hat)**2)) ]))

        # Check stopping criteria
        if stop_criteria < tol:
            break

    return(x_hat)
예제 #8
0
 def test_quadratic(self):
     x0 = 6
     x, iter = gd(of.quadratic, fd_complex_step, x0, alpha=None)
     self.assertTrue(np.allclose(x, 9 / 4))
예제 #9
0
파일: test_gd.py 프로젝트: zongjg/mr_utils
 def test_beale(self):
     '''Beale function with min at (3, 1/2).'''
     x0 = np.zeros(2)
     x, _iter = gd(of.beale, fd_complex_step, x0)
     self.assertTrue(np.allclose(x, np.array([3, 0.5])))
예제 #10
0
파일: test_gd.py 프로젝트: zongjg/mr_utils
 def test_bohachevsky3(self):
     '''Bohachevsky function 3 with min at origin.'''
     x0 = np.ones(2) * 200
     x, _iter = gd(of.bohachevsky3, of.grad_bohachevsky3, x0)
     self.assertTrue(np.allclose(x, np.zeros(x.shape)))
예제 #11
0
파일: test_gd.py 프로젝트: zongjg/mr_utils
 def test_sphere(self):
     '''Sphere function with min at origin.'''
     x0 = np.random.random(10)
     x, _iter = gd(of.sphere, fd_complex_step, x0)
     self.assertTrue(np.allclose(x, np.zeros(x.shape)))
예제 #12
0
파일: test_gd.py 프로젝트: zongjg/mr_utils
 def test_ackley(self):
     '''Ackley function with min at 0.'''
     # This has many minima, make sure we only get the closest one
     x0 = np.zeros(10)
     x, _iter = gd(of.ackley, of.grad_ackley, x0)
     self.assertTrue(np.allclose(x, np.zeros(x.shape)))
예제 #13
0
파일: test_gd.py 프로젝트: zongjg/mr_utils
 def test_rastrigin(self):
     '''Rastrigin function with min at 0.'''
     x0 = np.ones(10)
     x, _iter = gd(of.rastrigin, fd_complex_step, x0)
     self.assertTrue(np.allclose(x, np.zeros(x.shape)))
예제 #14
0
파일: test_gd.py 프로젝트: zongjg/mr_utils
 def test_maxiter(self):
     '''Make sure that hitting maxiter raises a warning.'''
     x0 = 6
     num_iter = 5
     with self.assertWarnsRegex(Warning, 'GD hit maxiters!'):
         _x, _iter = gd(of.quadratic, fd_complex_step, x0, maxiter=num_iter)
예제 #15
0
파일: test_gd.py 프로젝트: zongjg/mr_utils
 def test_quadratic(self):
     '''Simple quadratic function with min at 9/4.'''
     x0 = 6
     x, _iter = gd(of.quadratic, fd_complex_step, x0, alpha=None)
     self.assertTrue(np.allclose(x, 9 / 4))