neuroprob/utils/decoding.py
def fit_decoder(decoding_triplets, batches, timesteps, optimizer, max_epochs, loss_margin=0.0, 
                margin_epochs=10, scheduler=None, sch_st=None, dev='cpu'):
    """
    Fit the optimal probabilistic decoder with cross entropy loss.
    Optimizes a list of decoders for different dimensions/topologies.
    """
    loss_tracker = []
    minloss = np.inf
    cnt = 0
        
    iterator = tqdm(range(max_epochs))
    for epoch in iterator:
        sloss = 0
        for b in range(batches):
            optimizer.zero_grad()
            
            loss = 0
            for trp in decoding_triplets:
                decoder, input_batched, target_batched = trp
                ib = input_batched[b]
                tb = target_batched[b]
                
                batch_size = tb.shape[0]
                x_dist = decoder(ib.to(dev))

                subsample_fac = timesteps/batch_size
                loss_ = -x_dist.log_prob(tb.to(dev)).sum() * subsample_fac
                
                loss += loss_
                sloss += loss_.item()/batches
                
            loss.backward()
            optimizer.step()

        iterator.set_postfix(loss=sloss)
        loss_tracker.append(sloss)

        if scheduler is not None and epoch % sch_st == sch_st-1:
            scheduler.step()
            
        if sloss <= minloss + loss_margin:
            cnt = 0
        else:
            cnt += 1

        if sloss < minloss:
            minloss = sloss

        if cnt > margin_epochs:
            print("\nStopped at epoch {}.".format(epoch+1))
            break

    return loss_tracker

    Fit the optimal probabilistic decoder with cross entropy loss.
    Optimizes a list of decoders for different dimensions/topologies.
    
Fit the optimal probabilistic decoder with cross entropy loss.
Optimizes a list of decoders for different dimensions/topologies.
Fit the optimal probabilistic decoder with cross entropy loss . Optimizes a list of decoders for different dimensions / topologies .
def fit_decoder(decoding_triplets, batches, timesteps, optimizer, max_epochs, loss_margin=0.0, 
                margin_epochs=10, scheduler=None, sch_st=None, dev='cpu'):
    loss_tracker = []
    minloss = np.inf
    cnt = 0
    iterator = tqdm(range(max_epochs))
    for epoch in iterator:
        sloss = 0
        for b in range(batches):
            optimizer.zero_grad()
            loss = 0
            for trp in decoding_triplets:
                decoder, input_batched, target_batched = trp
                ib = input_batched[b]
                tb = target_batched[b]
                batch_size = tb.shape[0]
                x_dist = decoder(ib.to(dev))
                subsample_fac = timesteps/batch_size
                loss_ = -x_dist.log_prob(tb.to(dev)).sum() * subsample_fac
                loss += loss_
                sloss += loss_.item()/batches
            loss.backward()
            optimizer.step()
        iterator.set_postfix(loss=sloss)
        loss_tracker.append(sloss)
        if scheduler is not None and epoch % sch_st == sch_st-1:
            scheduler.step()
        if sloss <= minloss + loss_margin:
            cnt = 0
        else:
            cnt += 1
        if sloss < minloss:
            minloss = sloss
        if cnt > margin_epochs:
            print("\nStopped at epoch {}.".format(epoch+1))
            break
    return loss_tracker
def fit_decoder ( decoding_triplets , batches , timesteps , optimizer , max_epochs , loss_margin = 0.0 , margin_epochs = 10 , scheduler = None , sch_st = None , dev = 'cpu' ) : loss_tracker = [ ] minloss = np . inf cnt = 0 iterator = tqdm ( range ( max_epochs ) ) for epoch in iterator : sloss = 0 for b in range ( batches ) : optimizer . zero_grad ( ) loss = 0 for trp in decoding_triplets : decoder , input_batched , target_batched = trp ib = input_batched [ b ] tb = target_batched [ b ] batch_size = tb . shape [ 0 ] x_dist = decoder ( ib . to ( dev ) ) subsample_fac = timesteps / batch_size loss_ = - x_dist . log_prob ( tb . to ( dev ) ) . sum ( ) * subsample_fac loss += loss_ sloss += loss_ . item ( ) / batches loss . backward ( ) optimizer . step ( ) iterator . set_postfix ( loss = sloss ) loss_tracker . append ( sloss ) if scheduler is not None and epoch % sch_st == sch_st - 1 : scheduler . step ( ) if sloss <= minloss + loss_margin : cnt = 0 else : cnt += 1 if sloss < minloss : minloss = sloss if cnt > margin_epochs : print ( "\nStopped at epoch {}." . format ( epoch + 1 ) ) break return loss_tracker
Fit the optimal probabilistic decoder with cross entropy loss.
Fit the optimal probabilistic decoder with cross entropy loss .
"""
    Fit the optimal probabilistic decoder with cross entropy loss.
    Optimizes a list of decoders for different dimensions/topologies.
    """
decoding_triplets
batches
timesteps
optimizer
max_epochs
loss_margin
margin_epochs
scheduler
sch_st
dev