examples/utils.py
def analysis_dual_iter_algo(x_train, x_test, A, D, L, lbda, all_n_layers,
                            type_, max_iter=300, device=None,
                            net_kwargs=None, verbose=1):
    """ Chambolle solver for analysis TV problem. """
    net_kwargs = dict() if net_kwargs is None else net_kwargs
    Psi_A = np.linalg.pinv(A).dot(D)
    inv_AtA = np.linalg.pinv(A.dot(A.T))

    def _grad(v, x):
        return analysis_dual_grad(v, A, D, x, Psi_A=Psi_A)

    def _obj(v, x):
        v = np.atleast_2d(v)
        u = v_to_u(v, x, A=A, D=D, inv_AtA=inv_AtA)
        return analysis_primal_obj(u, A, D, x, lbda)

    def _prox(v, step_size):
        # XXX step_size is here to homogenize API
        v = np.atleast_2d(v)
        return np.clip(v, -lbda, lbda)

    v0_test, _, _ = init_vuz(A, D, x_test)
    v0_train, _, _ = init_vuz(A, D, x_train)

    max_iter = all_n_layers[-1]
    step_size = 1.0 / np.linalg.norm(Psi_A, ord=2) ** 2
    momentum = None if type_ == 'ista' else 'fista'
    name = 'ISTA' if type_ == 'ista' else 'FISTA'

    print("[ISTA iterative] training loss")
    params = dict(
        grad=lambda v: _grad(v, x_train),
        obj=lambda v: _obj(v, x_train), prox=_prox,
        x0=v0_train, momentum=momentum, restarting=None,
        max_iter=max_iter, step_size=step_size, early_stopping=False,
        debug=True, verbose=verbose,
    )
    _, train_loss = fista(**params)

    print("[ISTA iterative] testing loss")
    params = dict(
        grad=lambda v: _grad(v, x_test),
        obj=lambda v: _obj(v, x_test), prox=_prox,
        x0=v0_test, momentum=momentum, restarting=None,
        max_iter=max_iter, step_size=step_size, early_stopping=False,
        debug=True, verbose=verbose
    )
    _, test_loss = fista(**params)

    train_loss = train_loss[[0] + all_n_layers]
    test_loss = test_loss[[0] + all_n_layers]

    print(f"\r[{name}] iterations finished train-loss={train_loss[-1]:.6e} "
          f"test-loss={test_loss[-1]:.6e}")

    return train_loss, test_loss
 Chambolle solver for analysis TV problem. 
Chambolle solver for analysis TV problem.
Chambolle solver for analysis TV problem .
def analysis_dual_iter_algo(x_train, x_test, A, D, L, lbda, all_n_layers,
                            type_, max_iter=300, device=None,
                            net_kwargs=None, verbose=1):
    net_kwargs = dict() if net_kwargs is None else net_kwargs
    Psi_A = np.linalg.pinv(A).dot(D)
    inv_AtA = np.linalg.pinv(A.dot(A.T))
    def _grad(v, x):
        return analysis_dual_grad(v, A, D, x, Psi_A=Psi_A)
    def _obj(v, x):
        v = np.atleast_2d(v)
        u = v_to_u(v, x, A=A, D=D, inv_AtA=inv_AtA)
        return analysis_primal_obj(u, A, D, x, lbda)
    def _prox(v, step_size):
        v = np.atleast_2d(v)
        return np.clip(v, -lbda, lbda)
    v0_test, _, _ = init_vuz(A, D, x_test)
    v0_train, _, _ = init_vuz(A, D, x_train)
    max_iter = all_n_layers[-1]
    step_size = 1.0 / np.linalg.norm(Psi_A, ord=2) ** 2
    momentum = None if type_ == 'ista' else 'fista'
    name = 'ISTA' if type_ == 'ista' else 'FISTA'
    print("[ISTA iterative] training loss")
    params = dict(
        grad=lambda v: _grad(v, x_train),
        obj=lambda v: _obj(v, x_train), prox=_prox,
        x0=v0_train, momentum=momentum, restarting=None,
        max_iter=max_iter, step_size=step_size, early_stopping=False,
        debug=True, verbose=verbose,
    )
    _, train_loss = fista(**params)
    print("[ISTA iterative] testing loss")
    params = dict(
        grad=lambda v: _grad(v, x_test),
        obj=lambda v: _obj(v, x_test), prox=_prox,
        x0=v0_test, momentum=momentum, restarting=None,
        max_iter=max_iter, step_size=step_size, early_stopping=False,
        debug=True, verbose=verbose
    )
    _, test_loss = fista(**params)
    train_loss = train_loss[[0] + all_n_layers]
    test_loss = test_loss[[0] + all_n_layers]
    print(f"\r[{name}] iterations finished train-loss={train_loss[-1]:.6e} "
          f"test-loss={test_loss[-1]:.6e}")
    return train_loss, test_loss
def analysis_dual_iter_algo ( x_train , x_test , A , D , L , lbda , all_n_layers , type_ , max_iter = 300 , device = None , net_kwargs = None , verbose = 1 ) : net_kwargs = dict ( ) if net_kwargs is None else net_kwargs Psi_A = np . linalg . pinv ( A ) . dot ( D ) inv_AtA = np . linalg . pinv ( A . dot ( A . T ) ) def _grad ( v , x ) : return analysis_dual_grad ( v , A , D , x , Psi_A = Psi_A ) def _obj ( v , x ) : v = np . atleast_2d ( v ) u = v_to_u ( v , x , A = A , D = D , inv_AtA = inv_AtA ) return analysis_primal_obj ( u , A , D , x , lbda ) def _prox ( v , step_size ) : v = np . atleast_2d ( v ) return np . clip ( v , - lbda , lbda ) v0_test , _ , _ = init_vuz ( A , D , x_test ) v0_train , _ , _ = init_vuz ( A , D , x_train ) max_iter = all_n_layers [ - 1 ] step_size = 1.0 / np . linalg . norm ( Psi_A , ord = 2 ) ** 2 momentum = None if type_ == 'ista' else 'fista' name = 'ISTA' if type_ == 'ista' else 'FISTA' print ( "[ISTA iterative] training loss" ) params = dict ( grad = lambda v : _grad ( v , x_train ) , obj = lambda v : _obj ( v , x_train ) , prox = _prox , x0 = v0_train , momentum = momentum , restarting = None , max_iter = max_iter , step_size = step_size , early_stopping = False , debug = True , verbose = verbose , ) _ , train_loss = fista ( ** params ) print ( "[ISTA iterative] testing loss" ) params = dict ( grad = lambda v : _grad ( v , x_test ) , obj = lambda v : _obj ( v , x_test ) , prox = _prox , x0 = v0_test , momentum = momentum , restarting = None , max_iter = max_iter , step_size = step_size , early_stopping = False , debug = True , verbose = verbose ) _ , test_loss = fista ( ** params ) train_loss = train_loss [ [ 0 ] + all_n_layers ] test_loss = test_loss [ [ 0 ] + all_n_layers ] print ( f"\r[{name}] iterations finished train-loss={train_loss[-1]:.6e} " f"test-loss={test_loss[-1]:.6e}" ) return train_loss , test_loss
Chambolle solver for analysis TV problem.
Chambolle solver for analysis TV problem .
""" Chambolle solver for analysis TV problem. """ # XXX step_size is here to homogenize API
x_train
x_test
A
D
L
lbda
all_n_layers
type_
max_iter
device
net_kwargs
verbose