(compute, dataset, k=100,
        batch_size=50, sample_size=None, cachefile=None, **kwargs)
Computes conditional topk, e.g., top k examples for each class.
Computes conditional topk, e.g., top k examples for each class.
Computes conditional topk e . g . top k examples for each class .
def tally_conditional_topk(compute, dataset, k=100,
        batch_size=50, sample_size=None, cachefile=None, **kwargs):
    '''
    Computes conditional topk, e.g., top k examples for each class.
    '''
    with torch.no_grad():
        args = dict(sample_size=sample_size)
        cached_state = load_cached_state(cachefile, args)
        if cached_state is not None:
            return runningstats.RunningConditionalTopK(state=cached_state)
        loader = make_loader(dataset, sample_size, batch_size, **kwargs)
        ctk = runningstats.RunningConditionalTopK(k=k)
        for i, batch in enumerate(pbar(loader)):
            # Add the index to the batch
            first_index = i * loader.batch_size
            index_batch = torch.arange(first_index, first_index + len(batch[0]))
            batch.append(index_batch)
            sample_set = call_compute(compute, batch)
            for cond, data, index in sample_set:
                ctk.add(cond, data, index)
        # At the end, move all to the CPU
        ctk.to_('cpu')
        save_cached_state(cachefile, ctk, args)
        return ctk
def tally_conditional_topk ( compute , dataset , k = 100 , batch_size = 50 , sample_size = None , cachefile = None , * * kwargs ) : with torch . no_grad ( ) : args = dict ( sample_size = sample_size ) cached_state = load_cached_state ( cachefile , args ) if cached_state is not None : return runningstats . RunningConditionalTopK ( state = cached_state ) loader = make_loader ( dataset , sample_size , batch_size , * * kwargs ) ctk = runningstats . RunningConditionalTopK ( k = k ) for i , batch in enumerate ( pbar ( loader ) ) : # Add the index to the batch first_index = i * loader . batch_size index_batch = torch . arange ( first_index , first_index + len ( batch [ 0 ] ) ) batch . append ( index_batch ) sample_set = call_compute ( compute , batch ) for cond , data , index in sample_set : ctk . add ( cond , data , index ) # At the end, move all to the CPU ctk . to_ ( 'cpu' ) save_cached_state ( cachefile , ctk , args ) return ctk