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

    def record_loss(l_loss, u_train, u_test):
        l_loss.append(dict(
            train_loss=analysis_primal_obj(u_train, A, D, x_train, lbda),
            test_loss=analysis_primal_obj(u_test, A, D, x_test, lbda),
        ))
        return l_loss

    _, u0_train, _ = init_vuz(A, D, x_train)
    _, u0_test, _ = init_vuz(A, D, x_test)
    record_loss(l_loss, u0_train, u0_test)

    for n, n_layers in enumerate(all_n_layers):

        # Declare network for the given number of layers. Warm-init the first
        # layers with parameters learned with previous networks if any.
        algo = LearnTVAlgo(algo_type='lpgd_taut_string', A=A,
                           n_layers=n_layers, max_iter=max_iter, device=device,
                           initial_parameters=params, verbose=verbose,
                           **net_kwargs)

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

        # save parameters
        params = algo.export_parameters()

        # get train and test error
        u_train = algo.transform(x_train, lbda, output_layer=n_layers)
        u_test = algo.transform(x_test, lbda, output_layer=n_layers)
        l_loss = record_loss(l_loss, u_train, u_test)

        if verbose > 0:
            train_loss = l_loss[n]['train_loss']
            test_loss = l_loss[n]['test_loss']
            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}")

    df = pd.DataFrame(l_loss)
    to_return = (df['train_loss'].values, df['test_loss'].values)

    return to_return
 NN-algo solver for analysis TV problem. 
NN-algo solver for analysis TV problem.
NN - algo solver for analysis TV problem .
def analysis_learned_taut_string(x_train, x_test, A, D, L, lbda, all_n_layers,
                                 type_=None, max_iter=300, device=None,
                                 net_kwargs=None, verbose=1):
    net_kwargs = dict() if net_kwargs is None else net_kwargs
    params = None
    l_loss = []
    def record_loss(l_loss, u_train, u_test):
        l_loss.append(dict(
            train_loss=analysis_primal_obj(u_train, A, D, x_train, lbda),
            test_loss=analysis_primal_obj(u_test, A, D, x_test, lbda),
        ))
        return l_loss
    _, u0_train, _ = init_vuz(A, D, x_train)
    _, u0_test, _ = init_vuz(A, D, x_test)
    record_loss(l_loss, u0_train, u0_test)
    for n, n_layers in enumerate(all_n_layers):
        algo = LearnTVAlgo(algo_type='lpgd_taut_string', 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()
        u_train = algo.transform(x_train, lbda, output_layer=n_layers)
        u_test = algo.transform(x_test, lbda, output_layer=n_layers)
        l_loss = record_loss(l_loss, u_train, u_test)
        if verbose > 0:
            train_loss = l_loss[n]['train_loss']
            test_loss = l_loss[n]['test_loss']
            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}")
    df = pd.DataFrame(l_loss)
    to_return = (df['train_loss'].values, df['test_loss'].values)
    return to_return
def analysis_learned_taut_string ( x_train , x_test , A , D , L , lbda , all_n_layers , type_ = None , max_iter = 300 , device = None , net_kwargs = None , verbose = 1 ) : net_kwargs = dict ( ) if net_kwargs is None else net_kwargs params = None l_loss = [ ] def record_loss ( l_loss , u_train , u_test ) : l_loss . append ( dict ( train_loss = analysis_primal_obj ( u_train , A , D , x_train , lbda ) , test_loss = analysis_primal_obj ( u_test , A , D , x_test , lbda ) , ) ) return l_loss _ , u0_train , _ = init_vuz ( A , D , x_train ) _ , u0_test , _ = init_vuz ( A , D , x_test ) record_loss ( l_loss , u0_train , u0_test ) for n , n_layers in enumerate ( all_n_layers ) : algo = LearnTVAlgo ( algo_type = 'lpgd_taut_string' , 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 ( ) u_train = algo . transform ( x_train , lbda , output_layer = n_layers ) u_test = algo . transform ( x_test , lbda , output_layer = n_layers ) l_loss = record_loss ( l_loss , u_train , u_test ) if verbose > 0 : train_loss = l_loss [ n ] [ 'train_loss' ] test_loss = l_loss [ n ] [ 'test_loss' ] 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}" ) df = pd . DataFrame ( l_loss ) to_return = ( df [ 'train_loss' ] . values , df [ 'test_loss' ] . values ) return to_return
NN-algo solver for analysis TV problem.
NN - algo solver for analysis TV problem .
""" NN-algo solver for analysis TV problem. """ # Declare network for the given number of layers. Warm-init the first # layers with parameters learned with previous networks if any. # train # 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