Ejemplo n.º 1
0
def hanso(A, b, maxit, x0_init="random"):
    """
    Runs a flovor HANSO (customizable via the x0_init parameter).

    Parameters
    ----------
    x0_init: string, optional (defautl "random")
        initialization mode for HANSO: Possible values are:
        "fista": run FISTA, and then switch to HANSO once energy starts
        to increase
        "random": initialize as usual (multivariate standard normal)

    Raises
    ------
    ValueError

    """

    from hanso.hanso import hanso
    from hanso.setx0 import setx0

    nstart = 1

    def fg_with_tv_plus_l1_penalty(x):
        z = np.dot(A, x) - b
        return (.5 * linalg.norm(z)**2 + lambd * l1(x),
                np.dot(A.T, z) + lambd * grad_l1(x))

    def fg_with_l1_penalty(x):
        z = np.dot(A, x) - b
        return (.5 * linalg.norm(z)**2 + lambd * l1(x),
                np.dot(A.T, z) + lambd * grad_l1(x))

    def fg_with_tv_penalty(x):
        z = np.dot(A, x) - b
        return (.5 * linalg.norm(z)**2 + lambd * tv(x),
                np.dot(A.T, z) + lambd * grad_tv(x))

    pobj = []
    times = []

    # penalty dependent gradient definition
    if penalty_model == "l1":
        fg = fg_with_l1_penalty
    elif penalty_model == "tv":
        fg = fg_with_tv_penalty
    elif penalty_model == "tv+l1":
        fg = fg_with_tv_plus_l1_penalty
    else:
        raise ValueError("Unknown penalty model: %s" % penalty_model)

    if x0_init == "fista":
        x0, pobj, times = fista(A, b, maxit=maxit, stop_if_energy_rises=True)
    elif x0_init == "random":
        x0 = setx0(A.shape[1], nstart)
    else:
        raise ValueError("Unknown value for x0_init parameter: %s" % x0_init)

    maxit = maxit - len(pobj)
    results = hanso(fg, x0=x0, sampgrad=True, maxit=maxit, nvec=0, verbose=2)
    x = results[0]
    _pobj = results[-1]

    _times, _pobj = map(np.array, zip(*_pobj))
    _times = concat_times(times, _times)
    _pobj = list(pobj) + list(_pobj)

    return x, _pobj, _times
def hanso(A, b, maxit, x0_init="random"):
    """
    Runs a flovor HANSO (customizable via the x0_init parameter).

    Parameters
    ----------
    x0_init: string, optional (defautl "random")
        initialization mode for HANSO: Possible values are:
        "fista": run FISTA, and then switch to HANSO once energy starts
        to increase
        "random": initialize as usual (multivariate standard normal)

    Raises
    ------
    ValueError

    """

    from hanso.hanso import hanso
    from hanso.setx0 import setx0

    nstart = 1

    def fg_with_tv_plus_l1_penalty(x):
        z = np.dot(A, x) - b
        return (.5 * linalg.norm(z) ** 2 + lambd * l1(x),
                 np.dot(A.T, z) + lambd * grad_l1(x))

    def fg_with_l1_penalty(x):
        z = np.dot(A, x) - b
        return (.5 * linalg.norm(z) ** 2 + lambd * l1(x),
                 np.dot(A.T, z) + lambd * grad_l1(x))

    def fg_with_tv_penalty(x):
        z = np.dot(A, x) - b
        return (.5 * linalg.norm(z) ** 2 + lambd * tv(x),
                 np.dot(A.T, z) + lambd * grad_tv(x))

    pobj = []
    times = []

    # penalty dependent gradient definition
    if penalty_model == "l1":
        fg = fg_with_l1_penalty
    elif penalty_model == "tv":
        fg = fg_with_tv_penalty
    elif penalty_model == "tv+l1":
        fg = fg_with_tv_plus_l1_penalty
    else:
        raise ValueError("Unknown penalty model: %s" % penalty_model)

    if x0_init == "fista":
        x0, pobj, times = fista(A, b, maxit=maxit,
                                stop_if_energy_rises=True)
    elif x0_init == "random":
        x0 = setx0(A.shape[1], nstart)
    else:
        raise ValueError("Unknown value for x0_init parameter: %s" % x0_init)

    maxit = maxit - len(pobj)
    results = hanso(fg,
                    x0=x0,
                    sampgrad=True,
                    maxit=maxit,
                    nvec=0,
                    verbose=2
                    )
    x = results[0]
    _pobj = results[-1]

    _times, _pobj = map(np.array, zip(*_pobj))
    _times = concat_times(times, _times)
    _pobj = list(pobj) + list(_pobj)

    return x, _pobj, _times
Ejemplo n.º 3
0
    return x, _pobj, _times


maxit = 1000

# HANSO
hanso_x0_init_modes = [
    "fista",  # uncomment if penalty is l1 only
    'random'
]
pobj_hanso = []
times_hanso = []
for x0_init in hanso_x0_init_modes:
    optimizer = "HANSO (%s x0 init)" % x0_init
    x_hanso, pobj, times = hanso(A, b, maxit, x0_init=x0_init)
    pobj_hanso.append(pobj)
    times_hanso.append(times)
    fmin_hanso = loss_function(A, b, x_hanso)
    print "fmin %s: %s" % (optimizer, fmin_hanso)
    print

if penalty_model == "l1":
    # ISTA
    x_ista, pobj_ista, times_ista = ista(A, b, maxit)
    fmin_ista = loss_function(A, b, x_ista)
    print "xopt ISTA:", x_ista
    print "fmin ISTA:", fmin_hanso
    print

    # FISTA
    return x, _pobj, _times


maxit = 1000

# HANSO
hanso_x0_init_modes = [
    "fista",  # uncomment if penalty is l1 only
    'random'
    ]
pobj_hanso = []
times_hanso = []
for x0_init in hanso_x0_init_modes:
    optimizer = "HANSO (%s x0 init)" % x0_init
    x_hanso, pobj, times = hanso(A, b, maxit, x0_init=x0_init)
    pobj_hanso.append(pobj)
    times_hanso.append(times)
    fmin_hanso = loss_function(A, b, x_hanso)
    print "fmin %s: %s" % (optimizer, fmin_hanso)
    print

if penalty_model == "l1":
    # ISTA
    x_ista, pobj_ista, times_ista = ista(A, b, maxit)
    fmin_ista = loss_function(A, b, x_ista)
    print "xopt ISTA:", x_ista
    print "fmin ISTA:", fmin_hanso
    print

    # FISTA