(inv, gen, sampler, inv_layer=None, gen_layer=None,
        z_weight=1.0, x_weight=1.0, r_weight=0.0, ir_weight=0.0, reg_weight=0.0,
        adjust_z=None, regularize_z=None,
        test_sampler=None, lr=0.01, lr_milestones=None,
        epoch_batches=100, num_epochs=100, save_every=50,
        logfile=None,
        checkpoint_dir=None, checkpoint_selector=None, resume_from=None)
To set this up:
    inv and gen should both be instrumented models,
    and inv layer and gen layer should be retained on both of them
    without detach.
To set this up:
    inv and gen should both be instrumented models,
    and inv layer and gen layer should be retained on both of them
    without detach.
To set this up : inv and gen should both be instrumented models and inv layer and gen layer should be retained on both of them without detach .
def train_inverse(inv, gen, sampler, inv_layer=None, gen_layer=None,
        z_weight=1.0, x_weight=1.0, r_weight=0.0, ir_weight=0.0, reg_weight=0.0,
        adjust_z=None, regularize_z=None,
        test_sampler=None, lr=0.01, lr_milestones=None,
        epoch_batches=100, num_epochs=100, save_every=50,
        logfile=None,
        checkpoint_dir=None, checkpoint_selector=None, resume_from=None):
    '''
    To set this up:
    inv and gen should both be instrumented models,
    and inv layer and gen layer should be retained on both of them
    without detach.
    '''
    if lr_milestones is None:
        lr_milestones = []
    optimizer = torch.optim.Adam(inv.parameters(), lr=lr)
    scheduler = torch.optim.lr_scheduler.MultiStepLR(
            optimizer, milestones=lr_milestones, gamma=0.1)

    ckpts = checkpoints.CheckpointIO(checkpoint_dir)
    ckpts.register_modules(optimizer=optimizer, scheduler=scheduler,
            inv=inv if not checkpoint_selector else checkpoint_selector(inv))
    if resume_from is not None:
        ckpts.load('ckpt_%d.pt' % resume_from)
        start_epoch = resume_from + 1
    else:
        start_epoch = 0

    def inv_loss(z):
        return sum(loss * weight
                for loss, weight in component_loss(z).values())

    def component_loss(true_z):
        observed_x = gen(true_z)
        if gen_layer:
            true_r = gen.retained_layer(gen_layer, clear=True)
        estimated_z = inv(observed_x)
        if inv_layer:
            inverted_r = inv.retained_layer(inv_layer, clear=True)
        if adjust_z:
            fixed_z = adjust_z(estimated_z, true_z)
        else:
            fixed_z = estimated_z
        if x_weight or r_weight:
            reconstructed_x = gen(fixed_z)
        if gen_layer:
            reconstructed_r = gen.retained_layer(gen_layer, clear=True)
        losses = OrderedDict()
        if reg_weight:
            losses['reg'] = (regularize_z(estimated_z, true_z), reg_weight)
        if z_weight:
            losses['z'] = (mse_loss(true_z, estimated_z), z_weight)
        if ir_weight:
            losses['ir'] = (cor_square_error(true_r, inverted_r), ir_weight)
        if x_weight:
            losses['x'] = (mse_loss(observed_x, reconstructed_x), x_weight)
        if r_weight:
            losses['r'] = (cor_square_error(true_r, reconstructed_r), r_weight)
        return losses

    with torch.no_grad():
        for epoch, epoch_loader in pbar(
                epoch_grouper(sampler, epoch_batches, num_epochs=1+num_epochs,
                    start_epoch=start_epoch),
                total=(1+num_epochs-start_epoch)):
            if epoch > 0:
                for (z_batch,) in epoch_loader:
                    with torch.enable_grad():
                        optimizer.zero_grad()
                        loss = inv_loss(z_batch)
                        loss.backward()
                        pbar.post(l=loss.item())
                        optimizer.step()
                scheduler.step()
            if test_sampler is not None:
                stats = MeanStats()
                for (z_batch,) in test_sampler:
                    stats.add(component_loss(z_batch), len(z_batch))
                logline = stats.logline(epoch)
                pbar.print(logline)
                if logfile is not None:
                    with open(logfile, 'a') as f:
                        f.write(logline + '\n')
            elif epoch > 0:
                pbar.print('%d: loss=%4g' % (epoch, loss.item()))
            if epoch % save_every == 0 or epoch == num_epochs:
                ckpts.save(epoch, 'ckpt_%d.pt' % epoch)
def train_inverse ( inv , gen , sampler , inv_layer = None , gen_layer = None , z_weight = 1.0 , x_weight = 1.0 , r_weight = 0.0 , ir_weight = 0.0 , reg_weight = 0.0 , adjust_z = None , regularize_z = None , test_sampler = None , lr = 0.01 , lr_milestones = None , epoch_batches = 100 , num_epochs = 100 , save_every = 50 , logfile = None , checkpoint_dir = None , checkpoint_selector = None , resume_from = None ) : if lr_milestones is None : lr_milestones = [ ] optimizer = torch . optim . Adam ( inv . parameters ( ) , lr = lr ) scheduler = torch . optim . lr_scheduler . MultiStepLR ( optimizer , milestones = lr_milestones , gamma = 0.1 ) ckpts = checkpoints . CheckpointIO ( checkpoint_dir ) ckpts . register_modules ( optimizer = optimizer , scheduler = scheduler , inv = inv if not checkpoint_selector else checkpoint_selector ( inv ) ) if resume_from is not None : ckpts . load ( 'ckpt_%d.pt' % resume_from ) start_epoch = resume_from + 1 else : start_epoch = 0 def inv_loss ( z ) : return sum ( loss * weight for loss , weight in component_loss ( z ) . values ( ) ) def component_loss ( true_z ) : observed_x = gen ( true_z ) if gen_layer : true_r = gen . retained_layer ( gen_layer , clear = True ) estimated_z = inv ( observed_x ) if inv_layer : inverted_r = inv . retained_layer ( inv_layer , clear = True ) if adjust_z : fixed_z = adjust_z ( estimated_z , true_z ) else : fixed_z = estimated_z if x_weight or r_weight : reconstructed_x = gen ( fixed_z ) if gen_layer : reconstructed_r = gen . retained_layer ( gen_layer , clear = True ) losses = OrderedDict ( ) if reg_weight : losses [ 'reg' ] = ( regularize_z ( estimated_z , true_z ) , reg_weight ) if z_weight : losses [ 'z' ] = ( mse_loss ( true_z , estimated_z ) , z_weight ) if ir_weight : losses [ 'ir' ] = ( cor_square_error ( true_r , inverted_r ) , ir_weight ) if x_weight : losses [ 'x' ] = ( mse_loss ( observed_x , reconstructed_x ) , x_weight ) if r_weight : losses [ 'r' ] = ( cor_square_error ( true_r , reconstructed_r ) , r_weight ) return losses with torch . no_grad ( ) : for epoch , epoch_loader in pbar ( epoch_grouper ( sampler , epoch_batches , num_epochs = 1 + num_epochs , start_epoch = start_epoch ) , total = ( 1 + num_epochs - start_epoch ) ) : if epoch > 0 : for ( z_batch , ) in epoch_loader : with torch . enable_grad ( ) : optimizer . zero_grad ( ) loss = inv_loss ( z_batch ) loss . backward ( ) pbar . post ( l = loss . item ( ) ) optimizer . step ( ) scheduler . step ( ) if test_sampler is not None : stats = MeanStats ( ) for ( z_batch , ) in test_sampler : stats . add ( component_loss ( z_batch ) , len ( z_batch ) ) logline = stats . logline ( epoch ) pbar . print ( logline ) if logfile is not None : with open ( logfile , 'a' ) as f : f . write ( logline + '\n' ) elif epoch > 0 : pbar . print ( '%d: loss=%4g' % ( epoch , loss . item ( ) ) ) if epoch % save_every == 0 or epoch == num_epochs : ckpts . save ( epoch , 'ckpt_%d.pt' % epoch )