(init_z, G, target_x, F=None, lambda_f=0,
        R=None, lambda_r=0, num_steps=1000, quiet=False,
        show=None, show_every=100)
return z
Starting at init_z, uss LBFGS to find a z for which G(z) -> target_x.
    By default uses l1_loss, but can also mse(F(G(z)), F(target_x))
Starting at init_z, uss LBFGS to find a z for which G(z) -> target_x.
    By default uses l1_loss, but can also mse(F(G(z)), F(target_x))
Starting at init_z uss LBFGS to find a z for which G ( z ) - > target_x . By default uses l1_loss but can also mse ( F ( G ( z )) F ( target_x ))
def refine_z_lbfgs(init_z, G, target_x, F=None, lambda_f=0,
        R=None, lambda_r=0, num_steps=1000, quiet=False,
        show=None, show_every=100):
    '''
    Starting at init_z, uss LBFGS to find a z for which G(z) -> target_x.
    By default uses l1_loss, but can also mse(F(G(z)), F(target_x))
    '''
    z = init_z.clone()
    parameters = [z]
    nethook.set_requires_grad(False, G)
    nethook.set_requires_grad(True, *parameters)
    if lambda_f:
        with torch.no_grad():
            target_f = F(target_x)
            nethook.set_requires_grad(False, F)

    optimizer = LBFGS.FullBatchLBFGS(parameters)

    def closure():
        optimizer.zero_grad()
        current_x = G(z)
        loss = torch.nn.functional.l1_loss(target_x, current_x)
        if lambda_f:
            loss += torch.nn.functional.mse_loss(target_f, F(current_x)
                    ) * lambda_f
        if lambda_r:
            loss += R(z) * lambda_r
        return loss

    pb = (lambda x: x) if quiet else pbar

    with torch.enable_grad():
        for step_num in pb(range(num_steps + 1)):
            if step_num == 0:
                loss = closure()
                loss.backward()
                lr, F_eval, G_eval = 0, 0, 0
            else:
                options = {'closure': closure, 'current_loss': loss,
                        'max_ls': 10}
                loss, _, lr, _, _, _, _, _ = optimizer.step(options)
            if show and (step_num % show_every == 0):
                with torch.no_grad():
                    show(x=G(z), z=z, loss=loss, it=step_num)

    return z
def refine_z_lbfgs ( init_z , G , target_x , F = None , lambda_f = 0 , R = None , lambda_r = 0 , num_steps = 1000 , quiet = False , show = None , show_every = 100 ) : z = init_z . clone ( ) parameters = [ z ] nethook . set_requires_grad ( False , G ) nethook . set_requires_grad ( True , * parameters ) if lambda_f : with torch . no_grad ( ) : target_f = F ( target_x ) nethook . set_requires_grad ( False , F ) optimizer = LBFGS . FullBatchLBFGS ( parameters ) def closure ( ) : optimizer . zero_grad ( ) current_x = G ( z ) loss = torch . nn . functional . l1_loss ( target_x , current_x ) if lambda_f : loss += torch . nn . functional . mse_loss ( target_f , F ( current_x ) ) * lambda_f if lambda_r : loss += R ( z ) * lambda_r return loss pb = ( lambda x : x ) if quiet else pbar with torch . enable_grad ( ) : for step_num in pb ( range ( num_steps + 1 ) ) : if step_num == 0 : loss = closure ( ) loss . backward ( ) lr , F_eval , G_eval = 0 , 0 , 0 else : options = { 'closure' : closure , 'current_loss' : loss , 'max_ls' : 10 } loss , _ , lr , _ , _ , _ , _ , _ = optimizer . step ( options ) if show and ( step_num % show_every == 0 ) : with torch . no_grad ( ) : show ( x = G ( z ) , z = z , loss = loss , it = step_num ) return z