examples/utils.py
def synthesis_learned_algo(x_train, x_test, A, D, L, lbda, all_n_layers,
                           type_, max_iter=300, device=None, net_kwargs=None,
                           verbose=1):
    """ NN-algo solver for synthesis TV problem. """
    net_kwargs = dict() if net_kwargs is None else net_kwargs
    params = None

    _, _, z0_test = init_vuz(A, D, x_test)
    _, _, z0_train = init_vuz(A, D, x_train)

    train_loss_init = synthesis_primal_obj(z0_train, A, L, x_train, lbda)
    test_loss_init = synthesis_primal_obj(z0_test, A, L, x_test, lbda)
    train_loss, test_loss = [train_loss_init], [test_loss_init]

    for n_layers in all_n_layers:

        # declare network
        algo = LearnTVAlgo(algo_type=type_, A=A, n_layers=n_layers,
                           max_iter=max_iter, device=device,
                           initial_parameters=params, verbose=verbose,
                           **net_kwargs)

        t0_ = time.time()
        algo.fit(x_train, lbda=lbda)
        delta_ = time.time() - t0_

        # save parameters
        params = algo.export_parameters()

        # get train and test error
        z_train = algo.transform(x_train, lbda, output_layer=n_layers)
        train_loss_ = synthesis_primal_obj(z_train, A, L, x_train, lbda)
        train_loss.append(train_loss_)

        z_test = algo.transform(x_test, lbda, output_layer=n_layers)
        test_loss_ = synthesis_primal_obj(z_test, A, L, x_test, lbda)
        test_loss.append(test_loss_)

        if verbose > 0:
            print(f"\r[{algo.name}|layers#{n_layers:3d}] model fitted "
                  f"{delta_:4.1f}s train-loss={train_loss_:.4e} "
                  f"test-loss={test_loss_:.4e}")

    to_return = (np.array(train_loss), np.array(test_loss))

    return to_return
 NN-algo solver for synthesis TV problem. 
NN-algo solver for synthesis TV problem.
NN - algo solver for synthesis TV problem .
def synthesis_learned_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
    params = None
    _, _, z0_test = init_vuz(A, D, x_test)
    _, _, z0_train = init_vuz(A, D, x_train)
    train_loss_init = synthesis_primal_obj(z0_train, A, L, x_train, lbda)
    test_loss_init = synthesis_primal_obj(z0_test, A, L, x_test, lbda)
    train_loss, test_loss = [train_loss_init], [test_loss_init]
    for n_layers in all_n_layers:
        algo = LearnTVAlgo(algo_type=type_, A=A, n_layers=n_layers,
                           max_iter=max_iter, device=device,
                           initial_parameters=params, verbose=verbose,
                           **net_kwargs)
        t0_ = time.time()
        algo.fit(x_train, lbda=lbda)
        delta_ = time.time() - t0_
        params = algo.export_parameters()
        z_train = algo.transform(x_train, lbda, output_layer=n_layers)
        train_loss_ = synthesis_primal_obj(z_train, A, L, x_train, lbda)
        train_loss.append(train_loss_)
        z_test = algo.transform(x_test, lbda, output_layer=n_layers)
        test_loss_ = synthesis_primal_obj(z_test, A, L, x_test, lbda)
        test_loss.append(test_loss_)
        if verbose > 0:
            print(f"\r[{algo.name}|layers#{n_layers:3d}] model fitted "
                  f"{delta_:4.1f}s train-loss={train_loss_:.4e} "
                  f"test-loss={test_loss_:.4e}")
    to_return = (np.array(train_loss), np.array(test_loss))
    return to_return
def synthesis_learned_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 params = None _ , _ , z0_test = init_vuz ( A , D , x_test ) _ , _ , z0_train = init_vuz ( A , D , x_train ) train_loss_init = synthesis_primal_obj ( z0_train , A , L , x_train , lbda ) test_loss_init = synthesis_primal_obj ( z0_test , A , L , x_test , lbda ) train_loss , test_loss = [ train_loss_init ] , [ test_loss_init ] for n_layers in all_n_layers : algo = LearnTVAlgo ( algo_type = type_ , A = A , n_layers = n_layers , max_iter = max_iter , device = device , initial_parameters = params , verbose = verbose , ** net_kwargs ) t0_ = time . time ( ) algo . fit ( x_train , lbda = lbda ) delta_ = time . time ( ) - t0_ params = algo . export_parameters ( ) z_train = algo . transform ( x_train , lbda , output_layer = n_layers ) train_loss_ = synthesis_primal_obj ( z_train , A , L , x_train , lbda ) train_loss . append ( train_loss_ ) z_test = algo . transform ( x_test , lbda , output_layer = n_layers ) test_loss_ = synthesis_primal_obj ( z_test , A , L , x_test , lbda ) test_loss . append ( test_loss_ ) if verbose > 0 : print ( f"\r[{algo.name}|layers#{n_layers:3d}] model fitted " f"{delta_:4.1f}s train-loss={train_loss_:.4e} " f"test-loss={test_loss_:.4e}" ) to_return = ( np . array ( train_loss ) , np . array ( test_loss ) ) return to_return
NN-algo solver for synthesis TV problem.
NN - algo solver for synthesis TV problem .
""" NN-algo solver for synthesis TV problem. """ # declare network # save parameters # get train and test error
x_train
x_test
A
D
L
lbda
all_n_layers
type_
max_iter
device
net_kwargs
verbose